Run some code as different user rights in D365 X++

 

we can define the user in parameter form where we define the user whose rights we want to use.

otherwise we can run it as an admin


public static void  main(Args _args)

    {

        

        FormRun fr   = _args.caller();

        FormDataSource fds;


        

       

        RunAsPermission perm;

        UserId runAsUser;

        SysUserInfo userInfo;

        if(CustomParrameter::Find().UserId)

        {

        userInfo = SysUserInfo::find(CustomParrameter::Find().UserId);

        }else

        {

            userInfo = SysUserInfo::find("Admin");

        }

        runAsUser = userInfo.Id;

        perm = new RunAsPermission(runAsUser);

        perm.assert();

        runAs(runAsUser, classnum(Yourclass), staticMethodStr(Yourclass,YourMethod),[_args.record()]);

        CodeAccessPermission::revertAssert();



        fds =   fr.dataSource();

        fds.refresh();

        fds.reread();

        fds.research(true);


    }


Regards 
Muhammad Farraz

email: farazjaved01@gmail.com

Comments

Popular posts from this blog

How to Bypass Next in COC in D365 X++

create movement journal through code in d365 FO x++