Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Use different user for execute task

(0) ShareShare
ReportReport
Posted on by 76

Hi,

I'm looking for a way to execute code as a different ax user. Like in batch i can change the user that is running the job I want to do this from code when a user executes a action.

For example I have a code that stores documents and reads them for a certain location.

I need that the code uses only the created user not the logged in one for this task especially that only one user has all the needed access created to the storage.

  • RadekM Profile Picture
    RadekM 76 on at
    RE: Use different user for execute task

    I did find your old response it this topic. Looks like RunAs() just dont like the container itself... When u pass it like this [''] it works. ConNull() breaks it, just like a declared empty container.

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: Use different user for execute task

    I tried your code and got the same error, although I would expect it to work. I'll try to investigate it tomorrow.

  • RadekM Profile Picture
    RadekM 76 on at
    RE: Use different user for execute task

    I didn't know I need to return anything using this method. Anyway changing it to return a container didn't change anything. The parameter container is also confusing since it states to be optional but I must pass at least a empty container just to compile it.

    So it is not possible to pass a user as a parameter in batch it will always create it with the logged user?

        public  static container runOperation( container _param)
        {
            setPrefix(DocumentHistorySaveSelected::description());
    
            return _param;
        }

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: Use different user for execute task

    Let me post your code again, this time in the right way with line indentation. Please don't forget to use Insert > Code.

    public static void main(Args _args)
    {
    	FormRun formRun = _args.caller();
    	
    	if (formRun.name() == formStr(DocumentHistory))
    	{
    		UserId runAsUser = 'testUser';
    		
    		RunAsPermission perm = new RunAsPermission(runAsUser);
    		perm.assert();
    	
    		//Commented out because it's not used
    		//FormDataSource custPackingSlipJour_toSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, CustPackingSlipJour));
    		//FormDataSource custInvoiceJour_ToSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, CustInvoiceJour_ToSend));
    		//FormDataSource docuRef_toSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, docuRef));
        	//DocumentHistorySaveSelected sendSelected = new DocumentHistorySaveSelected(custInvoiceJour_ToSend_ds, custPackingSlipJour_toSend_ds, docuRef_toSend_ds);
    
    		runAs(runAsUser, classNum(DocumentHistorySaveSelected), staticMethodStr(DocumentHistorySaveSelected, runOperation), conNull());
    	}
    }

    I also simpliied your code a bit.

    A likely explaination why your method (DocumentHistorySaveSelected.runOperation()) can't be used is that it doesn't have the right parameters and/or return value. Unfortunately you didn't share this information, so we can't review it. The methjod should accept a single container and return a container.

    Client and server keywords are ignored, because F&O has no client to run X on. All code runs at the same place - the web server.

  • RadekM Profile Picture
    RadekM 76 on at
    RE: Use different user for execute task

    Well I didn no succeed in running runAs(). It keep telling my method does not exist. It looks like the method im calling must me static not sure if I must make it also server static but it still don't work. When trying to work with prompt() it just skip the RunAS() part..

    Can I force batch to run on custom user instead of client / server side by run runsImpersonated()?

    // I was unable to insert this as code... 

    server static public void main(Args _args)
    {
    RunAsPermission _perm;
    UserId _runAsUser;
    SysUserInfo _userInfo;
    _userInfo = SysUserInfo::find('testUser');
    _runAsUser = _userInfo.Id;
    _perm = new RunAsPermission(_runAsUser);
    _perm.assert();
    container _param;

    FormRun formRun = _args.caller();
    FormDataSource CustInvoiceJour_ToSend_ds, CustPackingSlipJour_toSend_ds, docuRef_toSend_ds;

    if (formRun.name() == formStr(DocumentHistory))
    {
    CustPackingSlipJour_toSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, CustPackingSlipJour));
    custInvoiceJour_ToSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, CustInvoiceJour_ToSend));
    docuRef_toSend_ds = formRun.dataSource(formDataSourceStr(DocumentHistory, docuRef));

    DocumentHistorySaveSelected sendSelected = new DocumentHistorySaveSelected(custInvoiceJour_ToSend_ds, CustPackingSlipJour_toSend_ds, docuRef_toSend_ds);

    // if (sendSelected.prompt())
    {
    RunAs(_runAsUser, classnum(DocumentHistorySaveSelected), staticmethodStr(DocumentHistorySaveSelected, "runOperation"),_param);
    // sendSelected.runOperation();
    }
    // RunAs(_runAsUser, classnum(DocumentHistorySaveSelected), methodStr(DocumentHistorySaveSelected, "run"),_param);
    // CodeAccessPermission::revertAssert();
    }

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: Use different user for execute task

    Use runAs() method.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans