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

Recommended Reading:

5.25.22 Financials

Rounding Numbers in Power Automate

  If you’ve ever tried to round numbers in Power Automate, you have probably already run into the issue there […]

Read the Article

Manage U.S. Use Tax on Purchase Orders in Dynamics 365 Finance and Operations

  Managing sales tax requirements on your business purchase can be complicated, but Dynamics 365 Finance and Operations can help […]

Read the Article
5.19.22 Dynamics CRM

How to Write a Great Support Ticket in the Stoneridge Support Portal

Submitting a support ticket through the Stoneridge Support Portal is a quick and effective way to get assistance for any […]

Read the Article

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article
5.5.22 Dynamics GP

The Real Story about the Long-Term Future of Dynamics GP Support

I’ve seen a number of people put forward comment that Dynamics GP is going away and you have to get […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Financial Enhancements

The Dynamics 365 Businses Central 2022 Wave 1 Release has a lot of new and exciting features to help your […]

Read the Article

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!

X