venerdì 29 novembre 2024

AX 2012 - Associare un contatto ad un indirizzo

Con questo job possiamo associare un contatto ad un qualsiasi indirizzo:

static void LIL_CreateAddressContact(Args _args)  
{  
    DirPartyContactInfoView          dirPartyContactInfoView;  
    LogisticsContactInfoView         logisticsContactInfoView;  
    LogisticsElectronicAddress       logisticsElectronicAddress;  
    dirPartypostalAddressView        dirPartypostalAddressView;  
    logisticsLocation                logisticsLocation;  
    CustTable              custTable;  
   
    //identificare l'indirizzo a cui associare il contatto  
    select firstOnly dirPartypostalAddressView  
        join CustTable  
        where CustTable.Party == dirPartypostalAddressView.Party  
        && CustTable.AccountNum == "US-002"  
        && dirPartypostalAddressView.LocationName == "TEST03"  
        && dirpartypostalAddressView.ValidFrom <= DateTimeUtil::utcNow()  
        && dirpartypostalAddressView.ValidTo >= DateTimeUtil::utcNow();  
    
    if(dirPartypostalAddressView)  
    {  
        select firstOnly logisticsLocation  
        where logisticsLocation.ParentLocation == dirPartypostalAddressView.Location;  
       
        if(!logisticsLocation)
        {
            logisticsLocation.clear();
            logisticsLocation.initValue();
            logisticsLocation.ParentLocation = LogisticsLocation::find (dirPartypostalAddressView.Location).RecId;
            logisticsLocation.IsPostalAddress = NoYes::No;

            logisticsLocation.insert();
        }
   
        dirPartyContactInfoView.LocationName  = "contatto mail";  
        dirPartyContactInfoView.Locator     = "somarisuax@mail.com";  
        dirPartyContactInfoView.Type      = LogisticsElectronicAddressMethodType::Email;  
        dirPartyContactInfoView.IsPrimary    = NoYes::No;  
        dirPartyContactInfoView.Location    = logisticsLocation.RecId;  
   
        logisticsElectronicAddress.initValue();  
        logisticsContactInfoView.initFromPartyContactInfoView(dirPartyContactInfoView);  
        logisticsElectronicAddress = LogisticsElectronicAddressEntity::initFromLogisticsContactInfoView(logisticsContactInfoView, logisticsElectronicAddress);  
   
        if (logisticsElectronicAddress.validateWrite())  
        {  
            logisticsElectronicAddress.insert();  
   
            info("Contatto creato!");  
        }  
    }  
    else  
    {  
        warning("Indirizzo non trovato!");  
    }  
}  
il risultato sarà il seguente: