Sunday, June 9, 2024

Try Catch in X++

 Exception handling is important when we write code. Below is the sample code to catch exception when code is running.


class TryCatchTest

{

    SysInfologEnumerator            enumerator;

    SysInfologMessageStruct         msgStruct;

    Exception                       exception;

    Counter                         i = 0;

    str                             errorGlobal;


    public void createCustomer()

    {

        CustTable custTable;

        try

        {

            custTable.AccountNum              = "Account1";

            custTable.insert();

        }

        catch

        {

            ++i;

            enumerator = SysInfologEnumerator::newData(infolog.cut(i));

                

            while (enumerator.moveNext())

            {

                msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());

                exception = enumerator.currentException();

                errorGlobal +=  msgStruct.message();

            }


        }

    }

}

changecompany in x++

 If you want to select/insert/update/delete data from specific company or legal entity, then changecompany keyword we can use. Below is sample code.


 public str ChangeCompanyTest()

 {

       changecompany('ART') // ART is the legal entity name

            {

                //Your  X++ Code

    }


}



Get Enum Id and Enum Value in D365FO using SQL and X++

 Below is the sql get enum id and enum value of a enum in D365 F&O using sql. SELECT   enumidtable . NAME           'Enum Name' ...