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 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.
I tried your code and got the same error, although I would expect it to work. I'll try to investigate it tomorrow.
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; }
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.
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();
}
Use runAs() method.
André Arnaud de Cal...
291,979
Super User 2025 Season 1
Martin Dráb
230,848
Most Valuable Professional
nmaenpaa
101,156