AX / D365FO – Check if parameters value is set at Runtime with prmIsDefault() method

In X++ the prmIsDefault () let us know if a method’s input parameter variable has been set at runtime or not.

If it’s been set at runtime prmIsDefault () returns false, if it’s not set returns true.

This method is useful if you have to to skip a method’s logic if its input parameter variable’s value is not assigned at runtime (so its value is equal to its default value).

This is particularly useful if the assignment logic is complex or expensive in terms of system resources.

In below example we created the parmTest() method which accepts a number as input and has a default value set to 1.

class testClass
{
    private int number;
    public int parmTest(int _number = 1)
    {
        number = _number;
        // Expensive logic...
        return number;
    }
}

We want to skip the expensive logic inside the method if we don’t pass a value to the input parameter variable.

To do that we can use the prmIsDefault() method. So make a simple change to the code like show below

class testClass
{
    private int number;
    public int parmTest(int _number = 1)
    {
        if (!prmIsDefault(_number))
        {
            number = _number;
            // Some expensive logic ...
        }
        return number;
    }
}

We added the if condition –> if (!prmIsDefault(_number))

If the input variable is not set its value is the default value, so the expensive logic is skipped, instead the logic is executed if the parameter has been set (like shown below)

TestClass testClass = new TestClass();

o.parmTest();     //Skip the logic because input variable is not assigned so its value is equal to default value

o.parmTest(1);    //Do the logic beacuse input variable is manually assigned even if value is equal to its default value

o.parmTest(2);    //Do the logic beacuse input variable is manually assigned

Here is another example

class RunnableClass1
{
    public static void main(Args _args)
    {
        RunnableClass1::printInfo();    //Output is "Default Message"
        RunnableClass1::printInfo("");  //Output is Empty value ""
        RunnableClass1::printInfo("Not Default Message"); //Output is "Not Default Message"
    }

    public static void printInfo(str _message = "")
    {
        if (prmIsDefault(_message))
        {
            info("Default Message");
        }
        else
        {
            info(_message);
        }
    }

}

In the above example :

  • the first line RunnableClass1::printInfo() doesn’t pass a value at runtime so prmIsDefault(_message) returns true and Output value is “Default Message”
  • the second line RunnableClass1::printInfo(“”) passes a value at runtime so prmIsDefault(_message) returns false and Output value is an empty string “” (even if “” is equal to parameter default value)
  • the third line RunnableClass1::printInfo(“Not default message”) passes a value at runtime so prmIsDefault(_message) returns false and Output value is “Not default Message”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s