Tuesday 14 August 2018

Get a list of Security Roles with Duties from Dynamics AX using code X++


Following code can be used to obtain a list of all security roles along with the associated duties from Dynamics AX using X++ code:

static void JZS_ExtractRoleswithDuties(Args _args)
{

    CommaIO                       commaIO;
    FileName                        fileName;
    InventTable                     inventTable;
    SecurityRole            SecurityRole;
    SecurityRoleTaskGrant grantTable;
    str DutyName;
    SecurityTask            securityTask, securityTaskDef;
    ;

    fileName       = WINAPI::getTempPath() + "Security Roles" + ".csv";
    commaIO      = new CommaIO(fileName,'W');


    commaIO.write("Security Role","Description","Duty name","Duty AOT name","Duty description");

    while select SecurityRole order by Name
        join grantTable where grantTable.SecurityRole == SecurityRole.RecId
        join securityTask where securityTask.RecId == grantTable.SecurityTask
        && SecurityTask.Type == SecurityTaskType::Duty

    {
       select securityTaskDef where securityTaskDef.RecId == securityTask.RecId;
     
       DutyName = securityTaskDef.Name ;
       // info(strFmt("%1", DutyName ));

        commaIO.write(SecurityRole.Name+"("+SecurityRole.AotName+")", SecurityRole.Description, securityTaskDef.Name ,securityTaskDef.AotName, securityTaskDef.Description);
    }

    WINAPI::shellExecute(fileName);
}

Tuesday 12 June 2018

Get customer contact Information by Type and Purpose

Following job can be used to reterieve customer contact information by type (email/telephone) and purpose (Business/Invoice)

static void JS_CustomerContactbyTypePurpose(Args _args)
{
    CustTable                   custTable = CustTable::find("003764");
    LogisticsElectronicAddress   electronicAddresslocal;
 
    LogisticsElectronicAddress getelectronicAddress(CustAccount                             CustAccount, // Customer Account
                                                    LogisticsElectronicAddressMethodType    contactType = LogisticsElectronicAddressMethodType::Phone,
                                                    LogisticsLocationRoleType               purpose = LogisticsLocationRoleType::Invoice)
    {
        LogisticsLocation               parentLocation;
        DirPartyLocation                dirPartyLocation;
        LogisticsElectronicAddress      electronicAddress;
        LogisticsElectronicAddressRole  electronicAddressRole;
        LogisticsLocationRole           locationRole;
        custTable                       _custtable;
         select _custtable
            where _custtable.accountNum == CustAccount// Specify customer account here
            join dirPartyLocation
            where dirPartyLocation.Party == custTable.Party
            join parentLocation
            where dirPartyLocation.Location == parentLocation.RecId
            join electronicAddress
            where electronicAddress.Location == parentLocation.RecId
            && electronicAddress.Type == contactType
            join electronicAddressRole
            where electronicAddressRole.ElectronicAddress == electronicAddress.RecId
            join locationRole
            where electronicAddressRole.LocationRole == locationRole.RecId
            && locationRole.Type == purpose;
        {
           return electronicAddress;
        }
    }
 
    electronicAddresslocal = getelectronicAddress(custTable.AccountNum, LogisticsElectronicAddressMethodType::Phone, LogisticsLocationRoleType::Invoice);
    info(strFmt("Customer: %1, Description: %2, Information %3",  custTable.AccountNum, electronicAddresslocal.Description, electronicAddresslocal.Locator));
 
    electronicAddresslocal = getelectronicAddress(custTable.AccountNum, LogisticsElectronicAddressMethodType::Email, LogisticsLocationRoleType::Invoice);
    info(strFmt("Customer: %1, Description: %2, Information %3",  custTable.AccountNum, electronicAddresslocal.Description, electronicAddresslocal.Locator));

}