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.

Wait for CHG to be in Scheduled State and then update RITM variable

erin-marie
Tera Contributor

I have a workflow for a catalog item that we would like to hold until a CHG has reached the Scheduled State.  The form asks for the CHG number in a reference field (currently no conditions).  I have a checkbox on the Catalog Item (which will be hidden) that is called CHG Approved.  I have a Wait for Condition that waits for the box to be checked before continuing the workflow.  There is a Business Rule that is supposed to check the box but the BR isn't working.

 

BR

Table: change_request

When to Run: Before Update

Conditions: State changes to Scheduled

                     OR State is one of Scheduled, Implement, Review, Closed

Advanced Actions:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var aws = new GlideRecord('sc_req_item');

    aws.addQuery('sys_id', aws.sc_aws_assetdecom_change_request);

    aws.query();
    while (aws.next()) {
        aws.sc_aws_assetdecom_chg_approved = true;
        aws.update();

    }
})(current, previous);
 
Any assistance is appreciated.
Erin
7 REPLIES 7

James Chun
Kilo Patron

Hi @erin-marie,

 

I am assuming that the CHG reference field is a variable of the catalog item.

 

If so, can you try the following:

 

 

(function executeRule(current, previous /*null when async*/ ) {
    var aws = new GlideRecord('sc_req_item');
	aws.addEncodedQuery('variables.{sys_id of variable}=' + current.getUniqueValue()); //use your reference variable's sys_id. Also you may want to add additional condition such as item={sys_id of catalog item}
	
    aws.query();
    while (aws.next()) {
        aws.variables.sc_aws_assetdecom_chg_approved  = true;
        aws.update();
    }
})(current, previous);

 

 

Cheers

I think that did the trick!!  Going to do a little more testing and then I will accept solution.  Thank you so much!!!

No worries, I forgot to mention in the last reply, but I would recommend changing it to run after instead of before. You would only want this to be executed after the condition is satisfied.

 

Cheers