Pages

Tuesday, October 23, 2018

Fun and better method to get Args.Caller() name

In any AX/D365 environment, there's often a need to get the Args.Caller().Name(), except not all possible caller's will have a .name() method, so here's a little function that can be added to a form to get the caller's name in a more proper method.


private IdentifierName getCallerName()
{
    Object              caller;
    SysDictClass        sysDictClass;
    IdentifierName      callerName;

    // There must be a caller to retrieve a name
    if (!(element.args() && element.args().caller()))
        return callerName;

    caller          = element.args().caller();
    sysDictClass    = new SysDictClass(classIdGet(caller));

    // Use reflection & recursion to get the name if possible
    while (sysDictClass)
    {
        if (sysDictClass.hasObjectMethod(identifierStr(Name)))
        {
            callerName      = caller.name();
            sysDictClass    = null;
        }
        else if (sysDictClass.extend())
        {
            sysDictClass    = new sysDictClass(sysDictClass.extend());
        }
        else
        {
            sysDictClass    = null;
        }
    }

    return callerName;
}

No comments:

Post a Comment