Monday, 20 March 2017

Security Role and privileges Label issue (Label not displayed ) AX 2012

Label text is not displayed for Security Role and privileges


In several environments I have seen some wrong security role names or other artifacts. Then instead of the role name it will display a label ID. This post will inform you about how to solve this issue in your environment if you encounter a similar issue.

Issue:

In the screenshot below, you can see the first role showing characters starting with an ape tail character ‘@’, then three capital characters and a number (e.g. @DMF501). This is a label, which should be replaced with the correct description in your language. Usually when you see this kind of label ID’s it means the label probably does not exists. In case of security artifacts it might be related to a not correctly updated role, duty or privilege name or description.
FixSecurityLabels01
The label in this example belongs to the Data Import/Export Framework and was not correctly displayed for some reason. This can also occur with other role names from e.g. an ISV, localization partner module or even your own customizations. I have not been able to reproduce it in a correct way, but I do think it might be related to a difference in system language of the source environment where a model was created and your environment of Microsoft Dynamics AX. Now we want to fix security labels in AX 2012 when you have this same issue.

Resolution:

There are some system tables in AX which do represent the roles and tasks (duties, privileges, process cycles). These tables do have the literal label ID as string instead of the language description. The name of these tables are SecurityRole and SecurityTask. These tables represent the security artifacts defined in the AOT. However they are not real tables in your Dynamics AX database. They are translated to act as tables.
Below you will find a job which can be copied into your environment. It will first do a scan which names and descriptions looks like a label ID. This works for all label files used in your environment. From the report you can decide to adjust the job to exclude some records when needed. Note that you first have to test the job in a separate environment. The coding is provided as is and you are responsible for the results in your environment. When you are not familiar in the development environment, I would suggest you ask help from a developer.
Perform the next steps to get this script in your environment.
  • Open the development workspace
  • Go to the jobs node and create a new job
  • Copy and paste the below code into the new created job.
static void MK_FixSecurityLabels(Args _args)
{
    SecurityRole    securityRole;
    SecurityTask    securityTask;
    Dialog          dialog = new Dialog(‘Fix security labels’);
    DialogField     dialogLanguage;
    DialogField     dialogCheckOnly;  
    Label           label;
 
    boolean         changed;
    boolean         checkOnly;
    int             i;

    int             j;
    LabelType       labelString;
    LanguageId      languageId;
    ;
 
    dialogLanguage  = dialog.addFieldValue(extendedTypeStr(LanguageId), SystemParameters::getSystemLanguageId());
    dialogCheckOnly = dialog.addFieldValue(extendedTypeStr(NoYesId), NoYes::Yes, ‘Check only’);
 
    if (dialog.run())
    {
        languageId  = dialogLanguage.value();
        checkOnly   = dialogCheckOnly.value();
 
        if (!languageId)
        {
            throw error(“@SYS75762”); //Language must be specified
        }
 
        label = new Label(languageId);
 
        ttsBegin;
        //Roles
        while select forUpdate securityRole
        {
            changed = false;
 
            if ((SysLabel::isLabelId(securityRole.Name) && Label.exists(securityRole.Name)))
            {
                labelString = label.extractString(securityRole.Name);
 
                if (labelString != securityRole.Name)
                {
                    info(strFmt(‘Role %1 – Name – from %2 to %3.’, securityRole.AotName, securityRole.Name, labelString));
 
                    securityRole.Name = labelString;
                    changed = true;
                    i++;
                }
            }
 
            if ((SysLabel::isLabelId(securityRole.Description) && Label.exists(securityRole.Description)))
            {
                labelString = label.extractString(securityRole.Description);
 
                if (labelString != securityRole.Description)
                {
                    info(strFmt(‘Role %1 – Description – from %2 to %3.’, securityRole.AotName, securityRole.Description, labelString));
 
                    securityRole.Description = labelString;
                    if (! changed)
                    {
                        i++;
                    }
                    changed = true;
                }
            }
 
            if (changed && !checkOnly)
            {
                securityRole.doUpdate();
            }
        }
 
        // Privileges, Duties, Process cycles
        while select forUpdate securityTask
        {
            changed = false;
 
            if ((SysLabel::isLabelId(securityTask.Name) && Label.exists(securityTask.Name)))
            {
                labelString = label.extractString(securityTask.Name);
 
                if (labelString != securityTask.Name)
                {
                    info(strFmt(‘%1 %2 – Name – from %3 to %4.’, securityTask.Type, securityTask.AotName, securityTask.Name, labelString));
 
                    securityTask.Name = labelString;
                    changed = true;
                    j++;
                }
            }
 
            if ((SysLabel::isLabelId(securityTask.Description) && Label.exists(securityTask.Description)))
            {
                labelString = label.extractString(securityTask.Description);
 
                if (labelString != securityTask.Description)
                {
                    info(strFmt(‘%1 %2 – Name – from %3 to %4.’, securityTask.Type, securityTask.AotName, securityTask.Description, labelString));
 
                    securityTask.Description = labelString;
                    if (! changed)
                    {
                        j++;
                    }
                    changed = true;
                }
            }
 
            if (changed && !checkOnly)
            {
                securityTask.doUpdate();
            }
        }
        ttsCommit;
 
        info(strFmt(‘Number of roles with wrong label: %1’, i));
        info(strFmt(‘Number of tasks with wrong label: %1’, j));
 
        if (checkOnly)
        {
            info(‘Parameter was set to check only. No updates executed.’);
        }
        else
        {
            info(‘Reported log is updated.’);
        }
    }
}
  • Save and compile the job. If there are errors in the compiler output, analyse and solve them. (Probably you missed a part during the copy process)
    Note that this job is not optimized to be free from all best practices. It contains text constants instead of labels to be able to distribute this job without having to create new labels in each environment.
  • Execute the job.
  • A dialog will appear where the Language is defaulted from your system language. The checkbox Check only will be used to first analyse which label ID’s needs to be fixed.
FixSecurityLabels02
  • Click OK. The next result will be presented. The outcome depends on the roles and tasks which are wrong in your environment.
FixSecurityLabels03
  • When there are roles, privileges, tasks or process cycles you wish to exclude, you need to adjust the select statements in the job.
  • To update the names and descriptions you need to run the job again.
  • Now disable the checkbox Check only.
FixSecurityLabels04
  • Click OK.
FixSecurityLabels05
  • Now the log will report the same, but also mentions that the reported log is updated.
  • Now check the results in the Security roles form. You will notice the labels have been fixed now.
FixSecurityLabels06
 
 

 

Thursday, 26 January 2017

Set Delivery Address on Sales order through code X++ AX 2012

In this post we will learn how to set delivery address / over rider the default customer delivery address on a Sales order.

This method will return the  LogisticsPostalAddress recId, which we will assign to parm method of AXsalesTable class..
axsalesTable.parmDeliveryPostalAddress(this.setDeliveryAddress(_tmpSalesheaders));
private RecId setDeliveryAddress(TmpSalesTable           _tmpSalesheaders)
{
    DirPartyPostalAddressView   addressView, ret;
    DirPartyRecId                        partyRecId;
    DirParty                                 dirParty;
    LogisticsPostalAddress          postalAddress;
    ;
    //Create new Name
    partyRecId = DirPartyTable::createNew(DirPartyType::None, _tmpSalesheaders.DeliveryName).RecId;
   
    addressView.CountryRegionId = _tmpSalesheaders.DeliveryCountryRegionId;//"AUS";
    addressView.State = _tmpSalesheaders.DeliveryState;//"VIC";
    addressView.ZipCode = _tmpSalesheaders.DeliveryZipCode;//"3000";
    addressView.Street  = _tmpSalesheaders.DeliveryStreet;//"238 Flinders St";
    addressView.Party = partyRecId;
   
    //Handel errors for addresses
    if (!LogisticsAddressCountryRegion::exist(addressView.CountryRegionId))
        throw error(strFmt("@SYS9347",addressView.CountryRegionId));
    if (addressView.County && !LogisticsAddressCounty::exist(addressView.CountryRegionId, addressView.State, addressView.County))
        throw error(strFmt("@SYS72719",addressView.County));
    if (addressView.State && !LogisticsAddressState::exist(addressView.CountryRegionId, addressView.State))
        throw error(strFmt("@SYS72786",addressView.State));
    if (!LogisticsAddressZipCode::exist(addressView.ZipCode))
        throw error(strFmt("@SYS24626",addressView.ZipCode));

    addressView.Party  = partyRecId;
    if( addressView.Street || addressView.ZipCode || addressView.City || addressView.State || addressView.CountryRegionId)
    {
        DirParty = DirParty::constructFromPartyRecId(addressView.Party );
        ret = DirParty.createOrUpdatePostalAddress(addressView);
        postalAddress = LogisticsPostalAddress::findByLocation(ret.Location);
    }
    return postalAddress.RecId;
}

Monday, 23 January 2017

X++ code to copy Ledger from company (A) to Company (B)


public void copyLedgerSetup()
{
    Ledger   fromLedger = Ledger::findByLegalEntity(CompanyInfo::findDataArea(fromCompany).RecId);
    Ledger   toLedger   = Ledger::findByLegalEntity(CompanyInfo::findDataArea(toCompany).RecId);
    LedgerStructure fromLedgerStructure, toLedgerStructure;
    LedgerGainLossAccount fromLedgerGainLossAccount, toLedgerGainLossAccount;

    if (fromLedger && !toLedger)
    {
        toLedger.data(fromLedger);
        toLedger.PrimaryForLegalEntity = CompanyInfo::findDataArea(toCompany).RecId;
        toLedger.Name = toCompany;
        toLedger.doInsert();

        while select fromLedgerStructure
            where fromLedgerStructure.Ledger == fromLedger.RecId
        {
            toLedgerStructure.data(fromLedgerStructure);
            toLedgerStructure.Ledger = toLedger.RecId;
            toLedgerStructure.insert();
        }

        while select fromLedgerGainLossAccount
            where fromLedgerGainLossAccount.Ledger == fromLedger.RecId
        {
            toLedgerGainLossAccount.data(fromLedgerGainLossAccount);
            toLedgerGainLossAccount.Ledger = toLedger.RecId;
            toLedgerGainLossAccount.insert();
        }
    }
}

X++ code to copy Number sequence from one company(A) to other (B)

public void copyNumberSequence()
{
    NumberSequenceScope         fromNumberSequenceScope, toNumberSequenceScope;
    NumberSequenceTable         fromNumberSequenceTable, toNumberSequenceTable;
    NumberSequenceReference     fromNumberSequenceReference, toNumberSequenceReference;
    //mk
    Map                         mapKeyValue;
    MapEnumerator               enumer;
    MapIterator                 mapIter;
    int64                       iCurrentKey;
    int64                       sCurrentValue;

    while select fromNumberSequenceScope
        where (fromNumberSequenceScope.LegalEntity == CompanyInfo::findDataArea(fromCompany).RecId ||
              fromNumberSequenceScope.DataArea == fromCompany)
    {
        try
        {
            ttsBegin;
            toNumberSequenceScope = NumberSequenceScope::findByNaturalKey(
                                        toCompany,
                                        CompanyInfo::findDataArea(toCompany).RecId,
                                        fromNumberSequenceScope.OperatingUnit,
                                        fromNumberSequenceScope.FiscalCalendarPeriod,
                                        fromNumberSequenceScope.OperatingUnitType);

            if ((fromNumberSequenceScope.LegalEntity || fromNumberSequenceScope.DataArea) && !toNumberSequenceScope)
            {
                toNumberSequenceScope.clear();
                toNumberSequenceScope.data(fromNumberSequenceScope);
                if (toNumberSequenceScope.LegalEntity)
                {
                    toNumberSequenceScope.LegalEntity = CompanyInfo::findDataArea(toCompany).RecId;
                }
                if (toNumberSequenceScope.DataArea)
                {
                    toNumberSequenceScope.DataArea = toCompany;
                }
                toNumberSequenceScope.insert();
                mapKeyValue = null;
                mapKeyValue = new Map(Types::Int64,Types::Int64); //mk

                while select fromNumberSequenceTable
                    where fromNumberSequenceTable.NumberSequenceScope == fromNumberSequenceScope.RecId
                {
                    if (!NumberSequenceTable::findByNaturalKey(fromNumberSequenceTable.NumberSequence, toNumberSequenceScope.RecId))
                    {
                        toNumberSequenceTable.clear();
                        toNumberSequenceTable.data(fromNumberSequenceTable);
                        toNumberSequenceTable.NumberSequenceScope = toNumberSequenceScope.RecId;
                        toNumberSequenceTable.NextRec = toNumberSequenceTable.Lowest;
                        toNumberSequenceTable.insert();
                        if (!mapKeyValue.exists(fromNumberSequenceTable.RecId))
                        {
                            mapKeyValue.insert(fromNumberSequenceTable.RecId,toNumberSequenceTable.RecId); //mk
                        }
                    }
                }
                enumer = null;
                while select fromNumberSequenceReference
                    where fromNumberSequenceReference.NumberSequenceScope == fromNumberSequenceScope.RecId
                {
                    if (!NumberSequenceReference::findNaturalKey(toNumberSequenceScope.RecId, fromNumberSequenceReference.NumberSequenceDatatype))
                    {
                        toNumberSequenceReference.clear();
                        toNumberSequenceReference.data(fromNumberSequenceReference);
                        toNumberSequenceReference.NumberSequenceScope = toNumberSequenceScope.RecId;
                        enumer = mapKeyValue.getEnumerator();
                        iCurrentKey = 0;
                        sCurrentValue = 0;
                        while (enumer.moveNext())
                        {
                            iCurrentKey = enumer.currentKey();
                            if (fromNumberSequenceReference.NumberSequenceId == iCurrentKey)
                            {
                                sCurrentValue = enumer.currentValue();
                                //info(strFmt("Key: %1 and Value %2",iCurrentKey,sCurrentValue));
                                break;
                            }

                        }
                        if (sCurrentValue != 0)
                        {
                            toNumberSequenceReference.NumberSequenceId  = sCurrentValue; //mk
                        }
                        toNumberSequenceReference.insert();
                    }
                }
            }
            ttsCommit;
        }
        catch
        {
            warning("Error copying number sequence");
        }

    }

}

Sunday, 22 January 2017

X++ code to set Default Order Settings for an Item

static void ASM_UpdateDefaultOrderSettings(Args _args)
{
    InventTable inventTable;
    InventItemInventSetup inventItemInventSetup;
    InventItemPurchSetup inventItemPurchSetup;
    InventItemSalesSetup inventItemSalesSetup;
    InventDim inventDim;
    DataArea                dataArea;
    InventDimid             inventDimid;
    InventSite              lInventSite;

   ;
 
   inventTable = InventTable::find("S403010", true);
     While select dataArea where !dataArea.isVirtual && ( dataArea.id == "xsd")
     {
        changeCompany(dataArea.Id)
        {
            inventDimid = "";
            inventTable = null;
            lInventSite = null;
            inventDim = null;
            select firstonly lInventSite where lInventSite.dataAreaId == dataArea.id;
            if (lInventSite.RecId)
            {
               while select * from inventTable
               {
                   ttsBegin;
                   //select firstonly lInventSite
                   inventDim.initValue();
                   inventDim.InventSiteId = lInventSite.SiteId;//"AUS";
                   inventDim.inventLocationId = "standard";
                   inventDim = InventDim::findOrCreate(inventDim);

                   //Site specific setup
                   /*
                   inventItemInventSetup.initValue();
                   inventItemInventSetup.InventDimId = inventDim.inventDimId;
                   inventItemInventSetup.ItemId = inventTable.ItemId;
                   inventItemInventSetup.insert();

                   inventItemPurchSetup.initValue();
                   inventItemPurchSetup.InventDimId = inventDim.inventDimId;
                   inventItemPurchSetup.ItemId = inventTable.ItemId;
                   inventItemPurchSetup.insert();

                   inventItemSalesSetup.initValue();
                   inventItemSalesSetup.InventDimId = inventDim.inventDimId;
                   inventItemSalesSetup.ItemId = inventTable.ItemId;
                   inventItemSalesSetup.insert(); */

                   //Default order settings
                   inventItemInventSetup= inventItemInventSetup::findDefault(inventTable.itemId, true);
                   if(inventItemInventSetup.RecId)
                   {
                        inventItemInventSetup.InventDimIdDefault = inventDim.inventDimId;
                        inventItemInventSetup.update();
                   }
                   else
                   {
                       inventItemInventSetup.initValue();
                       inventItemInventSetup.InventDimId = inventDim.inventDimId;
                       inventItemInventSetup.ItemId = inventTable.ItemId;
                       inventItemInventSetup.insert();
                     
                      /*  inventItemInventSetup= inventItemInventSetup::findDefault(inventTable.itemId, true);
                        inventItemInventSetup.InventDimIdDefault = inventDim.inventDimId;
                        inventItemInventSetup.update(); */
                     
                   }
                   inventItemPurchSetup = inventItemPurchSetup::findDefault(inventTable.itemId, true);
                   if (inventItemPurchSetup.RecId)
                   {
                       inventItemPurchSetup.InventDimIdDefault = inventDim.inventDimId;
                       inventItemPurchSetup.update();
                   }
                   else
                   {
                       inventItemPurchSetup.initValue();
                       inventItemPurchSetup.InventDimId = inventDim.inventDimId;
                       inventItemPurchSetup.ItemId = inventTable.ItemId;
                       inventItemPurchSetup.insert();
                     
                       /* inventItemPurchSetup = inventItemPurchSetup::findDefault(inventTable.itemId, true);
                        inventItemPurchSetup.InventDimIdDefault = inventDim.inventDimId;
                        inventItemPurchSetup.update(); */
                     
                 
                   }
                   inventItemSalesSetup= inventItemSalesSetup::findDefault(inventTable.itemId, true);
                   if(inventItemSalesSetup.RecId)
                   {
                        inventItemSalesSetup.InventDimIdDefault = inventDim.inventDimId;
                        inventItemSalesSetup.update();
                   }
                   else
                   {
                       inventItemSalesSetup.initValue();
                       inventItemSalesSetup.InventDimId = inventDim.inventDimId;
                       inventItemSalesSetup.ItemId = inventTable.ItemId;
                       inventItemSalesSetup.insert();
                     
                      /*  inventItemSalesSetup= inventItemSalesSetup::findDefault(inventTable.itemId, true);
                        inventItemSalesSetup.InventDimIdDefault = inventDim.inventDimId;
                        inventItemSalesSetup.update(); */
                 
                   }
                   ttsCommit;
               }
            }
        }
     }

}

Tuesday, 17 January 2017

Stock on Hand of a Item using X++ code

Here we will see how to find the Stock on hand of a Item using the X++ code.

Key things to know:

1. The stock on hand will vary depending on Inventory dimensions.
2. The standard Class "InventOnhand" will have all the methods to find out the different formats of stock.
3. Job:

static void MK_stockOnHand(Args _args)
{
    InventOnhand        inventOnHand;
    InventDim           inventDim;
    InventDimParm       inventDimParm;
    InventQty           stockOnHand;
    ItemId              itemid = "00001aTestInter";
   
    inventDim.InventSiteId = "VIC"; // site
    inventDim.InventLocationId = "SOM"; // warehouse
   // inventDim.InventLocationId = "RES";
   
    inventDimParm.initFromInventDim(inventDim);
   
    inventOnHand = InventOnhand::newParameters(itemid,inventDim,inventDimParm);
   
    stockOnHand = inventOnHand.availPhysical();
   
    info (strFmt(" Stock on Hand for Item %1 is %2",itemid,stockOnHand));
   
}

Happy learning...Mallik

Sunday, 15 January 2017

Unpick / unreserve a sales order line X++

Unreserve / Unpick a sales order line X++ 


In this article we will see how to unreserved or unpick the piked sales order line. In standard AX 2012 we can do it manually by using the 'Update line" and pick functionality.

For doing it automatically, I am going to add a button(Action Menu Item) on Sales order Header, using this button we will call the class to do the job. 

below is the code which dose the job.. 

One good thing about this job is, it also show the relation on InventTrans and SalesLine in AX 2012. As you all know that, there is no more InventTransID in InventTrans Table, we have to pick it from InventTransOrigin.

///
/// Contains the code that does the actual job of the class ---Mallik.
///
void  run()
{
    InventTrans                 inventTransLocal;
    InventTransOriginSalesLine  inventTransOriginSalesLine;
    InventTransOrigin           inventTransOrigin;
    SalesLine                   salesLine;
    InventTransWMS_Pick         inventTransWMS_Pick;
    TmpInventTransWMS           tmpInventTransWMS;
    Query                       baseQueryInventTrans;
    QueryBuildDataSource        qbdsInventTrans;

    baseQueryInventTrans =  new Query();
    qbdsInventTrans = baseQueryInventTrans.addDataSource(tableNum(InventTrans));
    baseQueryInventTrans.dataSourceTable(tableNum(InventTrans));
    qbdsInventTrans.clearDynalinks();
    qbdsInventTrans.clearRanges();
    qbdsInventTrans.addRange(fieldNum(InventTrans,StatusReceipt)).value(SysQuery::value(StatusReceipt::None));
    qbdsInventTrans.addRange(fieldNum(InventTrans,StatusIssue)).value(SysQuery::range(StatusIssue::Picked,StatusIssue::OnOrder));

    inventTransWMS_Pick = InventTransWMS_Pick::newStandard(tmpInventTransWMS,baseQueryInventTrans);
    while select forUpdate salesLine where salesLine.SalesId == salesTable.SalesId
        && salesLine.ILSLocked == true
    {
       select inventTransLocal
        where  inventTransLocal.ItemId                   == salesline.ItemId
            && inventTransLocal.StatusIssue              == StatusIssue::Picked
        exists join inventTransOrigin
        where   inventTransOrigin.RecId                  == inventTransLocal.InventTransOrigin
        exists join inventTransOriginSalesLine
            where inventTransOriginSalesLine.SalesLineInventTransId      == salesline.InventTransId;
        if (inventTransLocal.RecId)
        {
           
            inventTransWMS_Pick.createFromInventTrans(inventTransLocal);
        }
    }
    inventTransWMS_Pick.updateInvent();

}

Happy learning.. Mallik

How to Disable “Advanced Filter or Sort” and Enforce Custom Filters on Any D365FO Form

 In Dynamics 365 Finance and Operations, users can apply filters through the “Advanced filter or sort” feature found under the Options tab...