web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Use different user for execute task

(0) ShareShare
ReportReport
Posted on by 174

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.

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    238,734 Most Valuable Professional on at

    Use runAs() method.

  • RadekM Profile Picture
    174 on at

    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();
    }

  • Martin Dráb Profile Picture
    238,734 Most Valuable Professional on at

    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
    174 on at

    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
    238,734 Most Valuable Professional on at

    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
    174 on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 577 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 309

#3
Diego Mancassola Profile Picture

Diego Mancassola 259

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans