create contact information in X++

 

Purpose:

The purpose of this blog post is to demonstrate how to create contact information in X++

Product:

Dynamics AX 2012

Description:

The code create contact information with purpose “Collection” on the customer record.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
static void MAKCustContactCreate(Args _args)
{
    #define.LocationRoleName('Collection')
 
    CustTable                  custTable;
    DirPartyTable              dirPartyTable;
    LogisticsElectronicAddress logisticsElectronicAddress;
    LogisticsLocationRole      locationRole;
    Map                        locationRoleMap;
    container                  locationRoleCon;
    ;
 
    custTable = CustTable::find('C000115');
    dirPartyTable = DirPartyTable::findRec(custTable.Party);
 
    if (!dirPartyTable.existElectronicAddrbyRole(#LocationRoleName))
    {
        locationRoleMap = new Map(Types::Int64, Types::String);
        locationRole = LogisticsLocationRole::findByName(#LocationRoleName);
 
        if (locationRole)
        {
            locationRoleMap.insert(locationRole.RecId, locationRole.description());
            locationRoleCon = map2Con(locationRoleMap);
 
            ttsBegin;
            logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Email;
            logisticsElectronicAddress.Locator = 'testUserAccount2@domain.com';
            logisticsElectronicAddress.Location = DirPartyLocation::findOrCreate(CustTable.Party, 0).Location;
            logisticsElectronicAddress = LogisticsElectronicAddress::findOrCreate(logisticsElectronicAddress);
            logisticsElectronicAddress = LogisticsElectronicAddress::findRecId(logisticsElectronicAddress.RecId, true);
            logisticsElectronicAddress.Description = "Email from X++";
            logisticsElectronicAddress.IsPrimary = NoYes::No;
            logisticsElectronicAddress.update();
            LogisticsEntityLocationRoleMap::createEntityLocationRoles(tableNum(LogisticsElectronicAddressRole), logisticsElectronicAddress.RecId, conPeek(locationRoleCon, 1), true);
            ttsCommit;
 
            info(strFmt("Created/updated email address %1 for customer %2.", logisticsElectronicAddress.Locator, custTable.AccountNum));
        }
    }
}

Comments