Monday 18 November 2019

Try/catch is not working in AX 2012 / D365

Sometimes we see catch statement does not catch the errors even though the code looks good.

The below code looks good for me. But, still not showing my custom exceptions.

while (resultSet.next())
{
    //some logic (...)

    ttsbegin;

    //This is a custom table that stores to where I want to integrate the given customer
    while select integrationCompany
            where integrationCompany.CRMCompany == customerInfo.parmCRMDataAreaId()
    {
        changeCompany(integrationCompany.ERPCompany)
        {
            try
            {
                customerInfo.createCustomer();

                //.. some more logic


            }
            catch
            {
                // My catch Block, that should update the source database to set
                // the processing status to "error"


                ttsAbort;
            }
        }
    }

    ttsCommit;

}



Reason: 
Because the try and catch statement is inside the transaction(ttsbegin and ttscommit) and this is what causing the issue.

Solution:

ttsbegin and ttscommit should be always inside the try and catch statements as shown below.



while (resultSet.next())
{
    //some logic (...)

    //This is a custom table that stores to where I want to integrate the given customer
    while select integrationCompany
            where integrationCompany.CRMCompany == customerInfo.parmCRMDataAreaId()
    {
        changeCompany(integrationCompany.ERPCompany)
        {
            try
            {
               ttsbegin;
              customerInfo.createCustomer();

                //.. some more logic
              ttscommit


            }
            catch
            {
                // My catch Block, that should update the source database to set
                // the processing status to "error";
               ttsbegin;
               //you update statement here
               ttscommit;
            }
        }
    }

}

For more details please refer : https://stackoverflow.com/questions/27529308/strange-behavior-with-try-catch-on-dynamics-ax-2012  

Wednesday 13 November 2019

How to decode/unescape/Special characters HTML in Dynamics AX 2012 / Dynamics 365 finance and operations

Below is the  simple job I have written in D365 for removing encoded values from a string. Usually we see this kind of encoded special characters in the API's.

class SW_HTMLDecode
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        str input   = ' Test &amp; notes';
        str output  = System.Web.HttpUtility::HtmlDecode(input);
        ;

        info(output);
    }

}


Result  = Test & notes

@Rahul

Monday 4 November 2019

Function DimensionValidationRequest::newForLedgerDimensionType has been incorrectly called inD365 F&O

Usually we see the below error when we create payment/general journals using X++ code.

Function DimensionValidationRequest::newForLedgerDimensionType has been incorrectly called.

The reason for this error is ledger dimension/offset ledger dimension(Main account) format is incorrect and assigned this format to ledgerjournalTrans.LedgerDimension or ledgerjournalTrans.OffsetLedgerDimension.

Below is the way to create and assign ledger dimensions.

//Variables
        DimensionDynamicAccount     offsetLedgerDim;
Array  acctDimAttrArray                     = new Array(Types::String);
            acctDimAttrArray.value(1,"MainAccount");

            Array acctDimArray                          = new Array(Types::String);
            acctDimArray.value(1,'123456');

            DefaultDimensionIntegrationValues DefaultDimensionIntegrationValues   = DimensionResolver::getEntityDisplayValue
                    (acctDimAttrArray, acctDimArray, extendedTypeStr(DimensionDynamicAccount), LedgerJournalACType::Ledger);
            DimensionDynamicAccountResolver DimensionDynamicAccountResolver     = DimensionDynamicAccountResolver::newResolver
                (DefaultDimensionIntegrationValues, LedgerJournalACType::Ledger, curExt());
            ledgerjournalTrans.AccountType = LedgerJournalACType::Ledger;
            ledgerjournalTrans.LedgerDimension = DimensionDynamicAccountResolver.resolve();
            ledgerjournalTrans.OffsetAccountType = LedgerJournalACType::Cust;

            offsetLedgerDim = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber
                ('1000005', LedgerJournalACType::Cust);
            ledgerjournalTrans.OffsetLedgerDimension = offsetLedgerDim;


In the same way if have main account with multiple dimensions you can follow the format.

Array  acctDimAttrArray                     = new Array(Types::String);
        acctDimAttrArray.value(1,"MainAccount");
        acctDimAttrArray.value(2,"Brand");
        acctDimAttrArray.value(3,"Channel");

        Array acctDimArray                          = new Array(Types::String);
        acctDimArray.value(1,_dataContract.parmMainAccount());//Main account
        acctDimArray.value(2,_dataContract.parmBrand());//Brand dimension
        acctDimArray.value(3,_dataContract.parmChannel());//Channel dimension

  DefaultDimensionIntegrationValues DefaultDimensionIntegrationValues   = DimensionResolver::getEntityDisplayValue
                    (acctDimAttrArray, acctDimArray, extendedTypeStr(DimensionDynamicAccount), LedgerJournalACType::Ledger);
            DimensionDynamicAccountResolver DimensionDynamicAccountResolver     = DimensionDynamicAccountResolver::newResolver
                (DefaultDimensionIntegrationValues, LedgerJournalACType::Ledger, curExt());
           ledgerjournalTrans.AccountType = LedgerJournalACType::Ledger;
            ledgerjournalTrans.LedgerDimension = DimensionDynamicAccountResolver.resolve();
            ledgerjournalTrans.OffsetAccountType = LedgerJournalACType::Cust;

            offsetLedgerDim = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber
                ('1000005', LedgerJournalACType::Cust);
            ledgerjournalTrans.OffsetLedgerDimension = offsetLedgerDim;

That's it.

@Rahul

System does not support setup 'continuous' of number sequence in Dynamics AX 2012 / D365 F&O

If you are facing the issue System does not support setup 'continuous' of number sequence, enclose your code of number sequence assignment within a transaction (TTSBegin & TTSCommit)


NumberSeq  numberSeq;
ttsbegin;
numberSeq = NumberSeq::newGetNum(HrmParameters::numRefApplicantId()).num()

ttscommit;

Error will be resolved!!!

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...