Creating a Prospect Via a Service Call in AX 2012

By Leah Baker | November 8, 2013

In AX 2012, you can create prospects via service calls.  The service to use is the smmBusRelService.  There is a small bug in the AX code in which you need to comment out an assert in the class and method DimensionEnabledType\constructForSystemDefinedByTableId.  On line 200 the line Debug::assert(false) throws an exception when it is executed via a service call so first comment out that line.  That change will have no other impacts on how your AX code is executed, it is used for debugging purposes and arguably should not be in the code to begin with.  Having said that, the code below demonstrates how to create, read and delete a prospect via the smmBusRelService:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CreateProspects.Prospects;

namespace CreateProspects
{
class Program
{
static void Main(string[] args)
{
string prospectId = Program.create();
Program.delete("ABC-000123");
}

public static string create()
{
smmBusRelServiceClient client = new smmBusRelServiceClient();
CallContext context = new CallContext();
context.Company = "dat";

AxdsmmBusRel smmBusRel = new AxdsmmBusRel();
AxdEntity_smmBusRelTable[] relTables = new AxdEntity_smmBusRelTable[1];
AxdEntity_smmBusRelTable relTable = new AxdEntity_smmBusRelTable();
relTable.Currency = "USD";
relTable.BusRelTypeId = "Prospect";
relTable.MainContactPersonnelNumber = "000096";

//Create the person party record
AxdEntity_DirParty_DirPerson person = new AxdEntity_DirParty_DirPerson();
person.NameAlias = "bnewell";
person.NameSequence = "FirstMiddleLast";

//Set the name fields
AxdEntity_PersonName personName = new AxdEntity_PersonName();
personName.FirstName = "Becky";
personName.MiddleName = "Kay";
personName.LastName = "Newell";

person.PersonName = new AxdEntity_PersonName[1] { personName };

/*
//Create the organization party record (if not wanting a person record)
AxdEntity_DirParty_DirOrganization organization = new AxdEntity_DirParty_DirOrganization();
organization.Name = "Stoneridge Software";
*/

AxdEntity_DirPartyPostalAddressView address = new AxdEntity_DirPartyPostalAddressView();
address.LocationName = "Location Name";
address.Street = "111 NE 5th St";
address.City = "Beverly Hills";
address.State = "CA";
address.CountryRegionId = "USA";
address.ZipCode = "90210";
address.Roles = "Home;Delivery";
AxdExtType_EffectiveDateTime myDateTime = new AxdExtType_EffectiveDateTime();
myDateTime.localDateTime = Convert.ToDateTime("11/8/2013");
myDateTime.localDateTimeSpecified = true;
myDateTime.timezone = AxdEnum_Timezone.GMTMINUS0600CENTRALTIME;
myDateTime.timezoneSpecified = true;
address.ValidFrom = myDateTime;
AxdExtType_ExpirationDateTime expireDateTime = new AxdExtType_ExpirationDateTime();
expireDateTime.localDateTime = Convert.ToDateTime("12/20/2020");
expireDateTime.localDateTimeSpecified = true;
expireDateTime.timezone = AxdEnum_Timezone.GMTMINUS0600CENTRALTIME;
expireDateTime.timezoneSpecified = true;
address.ValidTo = expireDateTime;

//Create an email address
AxdEntity_DirPartyContactInfoView email = new AxdEntity_DirPartyContactInfoView();
email.LocationName = "Prospect Email";
email.Type = AxdEnum_LogisticsElectronicAddressMethodType.Email;
email.TypeSpecified = true;
email.Locator = "whatever@hotmail.com";
email.Roles = "Home";

//Create a phone number
AxdEntity_DirPartyContactInfoView cellPhone = new AxdEntity_DirPartyContactInfoView();
cellPhone.LocationName = "Cell phone";
cellPhone.Type = AxdEnum_LogisticsElectronicAddressMethodType.Phone;
cellPhone.TypeSpecified = true;
cellPhone.Locator = "555-234-2234";
cellPhone.Roles = "Home";

//Add the names to the party record and set the name sequence
//person.PersonName = new AxdEntity_PersonName[1] { personName };
relTable.DirParty = new AxdEntity_DirParty_DirPartyTable[1] { person };
//Add the addresses to the party record
relTable.DirParty[0].DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[1] { address };
relTable.DirParty[0].DirPartyContactInfoView = new AxdEntity_DirPartyContactInfoView[2] { email, cellPhone };
relTables[0] = relTable;
smmBusRel.smmBusRelTable = relTables;

try
{
EntityKey[] ret = client.create(context, smmBusRel);
string prospectId = ret[0].KeyData[0].Value.ToString();
Console.WriteLine("Worked");
return prospectId;
}
catch (Exception e)
{
return e.Message;
}
}

public static void read(string prospectIdToFind)
{
smmBusRelServiceClient proxy = new smmBusRelServiceClient();
CallContext context = new CallContext();
context.Company = "dat";

AxdEntity_smmBusRelTable smmBusRelTable = new AxdEntity_smmBusRelTable();
EntityKey[] entityKeyList = new EntityKey[1];
EntityKey key = new EntityKey();
KeyField[] keyFields = new KeyField[1];
KeyField keyField = new KeyField();
keyField.Field = "BusRelAccount";
keyField.Value = prospectIdToFind;
keyFields[0] = keyField;
key.KeyData = keyFields;
entityKeyList[0] = key;

AxdsmmBusRel response = proxy.read(context, entityKeyList);
}

public static void delete(string prospectIdToDelete)
{
smmBusRelServiceClient proxy = new smmBusRelServiceClient();
CallContext context = new CallContext();
context.Company = "dat";

AxdEntity_smmBusRelTable smmBusRelTable = new AxdEntity_smmBusRelTable();
EntityKey[] entityKeyList = new EntityKey[1];
EntityKey key = new EntityKey();
KeyField[] keyFields = new KeyField[1];
KeyField keyField = new KeyField();
keyField.Field = "BusRelAccount";
keyField.Value = prospectIdToDelete;
keyFields[0] = keyField;
key.KeyData = keyFields;
entityKeyList[0] = key;

proxy.delete(context, entityKeyList);
}
}
}

Becky Newell

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!