Call Schedule Job from Business Rule passing current object as args
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 01:32 PM
Hi All,
My requirement is to run the Business rule to update record as logged in user , I tried to use flow designer flow instead of BR but that is also updating record as System user instead of logged in User.
I found some articles suggesting to create a schedule job which will update the case record. This scheduled job is to be called from Business Rule.
Is there any code example available to Call Scheduled Job from Business Rule and Passing Case record as argument.
Or is there any other way to run Business Rule as logged in User instead of system . Any code example for this.
Thanks
Deepak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 02:24 PM
I'm not sure this is a good/best practice, but a quick and dirty way would to be to set impersonation in the business rule.
For example:
session.onlineImpersonate(user_sys_id);
// do some stuff
session.onlineUnimpersonate();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 10:53 AM
Hi,
I tried impersonating using both the apis gs.getSession().impersonate and GlideImpersonate but getting error.
impersonation works only in global scope.
I need this in scoped application.
Thanks
Deepak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 11:40 AM
Who is the flow to set to run as? You can check this under (...) | Properties in the upper right.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2023 12:14 PM
Hi @deepak50 ,
Hope you are doing great.
To achieve your requirement of running a Business Rule (BR) as the logged-in user instead of the system user, and considering your attempt to use Flow Designer resulted in updates by the system user, you can implement a solution using a scheduled job that's triggered from the Business Rule.
Scheduled Job: Create a Scheduled Job in ServiceNow that performs the necessary updates on the case record. This job should be designed to accept the case record as an argument.
Business Rule: Modify the existing Business Rule to trigger the Scheduled Job and pass the case record as an argument.
Reference example:
Schedules Job :
// Create a new Scheduled Job in ServiceNow
// Name: UpdateCaseJob
// Type: Scripted
(function() {
// Get the current case record passed as the argument
var currentCase = new GlideRecord('case'); // Assuming the table name is 'case'
currentCase.get(current.sys_id); // Assuming the sys_id of the case is stored in 'current.sys_id'
// Perform the necessary updates on the case record
if (currentCase.isValid()) {
currentCase.setValue('status', 'resolved'); // Example update
currentCase.update();
}
})();
BR :
// Update the script to trigger the scheduled job and pass the case record as an argument
(function() {
// Check the condition under which the update is needed
if (current.u_some_field === 'some_value') { // Example condition
// Trigger the scheduled job and pass the current case record as an argument
var updateJob = new GlideScheduledJob('UpdateCaseJob');
updateJob.setScriptParams(current.sys_id); // Pass the case sys_id as an argument
updateJob.schedule();
}
})();
Regards,
Riya Verma