Call Schedule Job from Business Rule passing current object as args

deepak50
Tera Contributor

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

4 REPLIES 4

sleepycrown
Giga Guru

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

deepak50
Tera Contributor

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

thomaskennedy
Tera Guru

Who is the flow to set to run as? You can check this under  (...) | Properties in the upper right.

thomaskennedy_0-1691692785090.png

 

Riya Verma
Kilo Sage
Kilo Sage

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.

  1. 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.

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

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma