Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scripting Business Rule state changes

HP8
Giga Guru

Here's a BR I have on the sc_task table where it has a GlideRecord to check the current record's Request State on the sc_request table. I want 'request_state' CHANGES TO 'closed_complete' instead of equals, how can I achieve this?

 

var reqGR = new GlideRecord('sc_request');
reqGR.addQuery('sys_id', current.request);
reqGR.query();

if (reqGR.next()) {
    // Check if the sc_request conditions are met
    if (reqGR.request_state == 'closed_complete' && reqGR.short_description == ABC123') {
	gs.eventQueue('sn_customerservice.ape_rc_modify',current,current.variable_pool.user_email,gs.getUserName());
	gs.info('FPIM email recipient' + current.variable_pool.user_email);
    }
}

 

1 ACCEPTED SOLUTION

debendudas
Mega Sage
Mega Sage

Hi @HP8 ,

You can't check if the state changes to "Closed Complete" or not in a GlideRecord Query.

If you want to trigger some event when the Request is closed, then write the BR in the Request table with the condition: State Changes to Closed Complete.

View solution in original post

5 REPLIES 5

debendudas
Mega Sage
Mega Sage

Hi @HP8 ,

You can't check if the state changes to "Closed Complete" or not in a GlideRecord Query.

If you want to trigger some event when the Request is closed, then write the BR in the Request table with the condition: State Changes to Closed Complete.

Hi there. The reason why I did it this way is because I want the BR to trigger an event for an email notification when the request is closed complete. The BR is on sc_request and the email notification is on sc_task where I have a mail script set up to print fields from the sc_task table. Do you have any suggestions?

Please follow the below points:

  • Write the BR in the sc_request table.
  • Add condition State Changes to Closed Complete
  • In the Script section use GlideRecord to query the sc_task record.
  • Pass that GlideRecord object in the gs.eventQueue() method, instead of passing the current object.

Example code:

var scTaskGr = new GlideRecord('sc_task');
scTaskGr.addEncodedQuery(" < Add your encoded query to identify the sc_task record > ");
scTaskGr.query();
if (scTaskGr.next()) {
    gs.eventQueue('sn_customerservice.ape_rc_modify', scTaskGr, scTaskGr.variable_pool.user_email, gs.getUserName());
    gs.info('FPIM email recipient' + scTaskGr.variable_pool.user_email);
}

Try it out and let me know if this works.

 

Runjay Patel
Giga Sage

Hi @HP8 ,

 

You can use below code . Adjust code to trigger event

var reqGR = new GlideRecord('sc_request');
reqGR.addQuery('sys_id', '58097fe383211210a9589780deaad3ab');
reqGR.query();

if (reqGR.next()) {
   
   var gr1=new GlideRecord("sys_audit");
  gr1.addQuery("tablename","sc_request"); //filter by incident.
  gr1.addQuery("documentkey",reqGR.sys_id); 
  gr1.addQuery("fieldname","request_state");
  gr1.addQuery("newvalue","closed_complete"); 
  gr1.orderBy("created"); //sort by created date
  gr1.setLimit(1);
  gr1.query();
  if(gr1.next()) {
    gs.print('FPIM email recipient' + reqGR.request_state);
  }

   
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------