Tuesday 20 October 2015

How to Create/Update/Find a phone number for a customer/vendor in X++ [AX 2012]

As a follow-up to my last post about finding phone numbers, here is sample code of how you can properly find/create/update a phone number for a customer/vendor.

Case- 1: Creating or updating phone number for a customer/vendor


static void CreatePhoneExample(Args _args)
{
    CustTable                           custTable = CustTable::find('100013'); // TODO - Change to your customer
    LogisticsElectronicAddress          logisticsElectronicAddress;
    container                           defaultRole = map2Con(LogisticsLocationEntity::getDefaultLocationRoleFromEntity(tableNum(DirPartyTable)));
    
    setPrefix(strFmt("Creating/Updating number for customer %1", custTable.AccountNum));
    
    // This will find/create a number for a customer
    ttsBegin;
    logisticsElectronicAddress.Type = LogisticsElectronicAddressMethodType::Phone;
    logisticsElectronicAddress.Locator = '555-555-5555';
    
    logisticsElectronicAddress.Location = DirPartyLocation::findOrCreate(custTable.Party, 0).Location;

    // This will find or create the new logisticsElectronicAddress
    // If it does not find it, it will do a .insert() which will only persist these fields (Location, Type, Locator, LocatorExtension)
    // so if you want to set the Description or if it's primary or not, you will need to update the record after this call
    logisticsElectronicAddress = LogisticsElectronicAddress::findOrCreate(logisticsElectronicAddress);
    
    // We re-select it for update in case this isn't a new number and it found an existing
    // because the "find" doesn't "select for update"
    logisticsElectronicAddress = LogisticsElectronicAddress::findRecId(logisticsElectronicAddress.RecId, true);
    
    logisticsElectronicAddress.Description = "New Primary Phone";
    
    // If you set the number to primary, during the insert/update it will handle unassigning previously
    // marked primary numbers if they exist
    logisticsElectronicAddress.IsPrimary = NoYes::Yes;
    
    logisticsElectronicAddress.update();
    
    // At this point, we need to mark the "purpose" of the number.  I'm just using the default role, which should be "Business"
    LogisticsEntityLocationRoleMap::createEntityLocationRoles(tableNum(LogisticsElectronicAddressRole), logisticsElectronicAddress.RecId, conPeek(defaultRole, 1), true);
    
    info(strFmt("Created/updated phone number [%1] for customer %2.", logisticsElectronicAddress.Locator, custTable.AccountNum));
    ttsCommit;
}

Case- 1: Finding phone number for a customer/vendor

Method-1:
Display LogisticsElectronicAddressLocator Phone() { LogisticsElectronicAddress logisticsElectronicAddress; //return LogisticsElectronicAddress::findByLocation(DirPartyLocation::findOrCreate(this.Party, 0).Location).Locator; select Location, Type, Locator from logisticsElectronicAddress where logisticsElectronicAddress.Location == DirPartyLocation::findOrCreate(this.Party, 0).Location && logisticsElectronicAddress.Type == LogisticsElectronicAddressMethodType::Phone; Return logisticsElectronicAddress.Locator; }
Note: Here this.Party may refer to MSDirPartyPostalAddressView.Party or CustTable/VendTable.Party;
Method-2:
Display Description PhoneDescription() { LogisticsElectronicAddress logisticsElectronicAddress; //return LogisticsElectronicAddress::findByLocation(DirPartyLocation::findOrCreate(this.Party, 0).Location).Locator; select Location, Type, Locator from logisticsElectronicAddress where logisticsElectronicAddress.Location == DirPartyLocation::findOrCreate(this.Party, 0).Location && logisticsElectronicAddress.Type == LogisticsElectronicAddressMethodType::Phone; Return logisticsElectronicAddress.Description; }
Note: Here also this.Party may refer to MSDirPartyPostalAddressView.Party or CustTable/VendTable.Party;
Happy DAXing.....

No comments:

Post a Comment