Business Rules Asynchronous Communication

SotaT
Tera Contributor

Before a record is inserted into the sc_req_item table, a business rule is made to run: if the cat_item field matches a certain condition, the approval in the record of the parent request (sc_request table) that is in the request field I want to update the field. However, this does not work, perhaps because of asynchronous communication. How can I achieve this?

 

(function executeRule(current, previous /*null when async*/ ) {
    var grProp = new GlideRecord('sys_properties');
    if (grProp.get('name', '〇〇')) {
        var propValue = grProp.getValue('value');
        var catItemId = current.cat_item.toString();
        if ((',' + propValue + ',').indexOf(',' + catItemId + ',') !== -1) {
            var reqGR = new GlideRecord('sc_request');
            var requestId = current.request.toString();
            if (reqGR.get('sys_id', requestId)) {
                if (reqGR.getValue('approval') != "approved") {
                    reqGR.setValue('approval', 'approved');
                    reqGR.update();
                }
            }
        }
    }
})(current, previous);
 
 The following if statement does not pass in the above script.
 
if (reqGR.get('sys_id', requestId)) {
                if (reqGR.getValue('approval') != "approved") {
                    reqGR.setValue('approval', 'approved');
                    reqGR.update();
                }
            }

 

1 ACCEPTED SOLUTION

Chaitanya ILCR
Kilo Patron

Hi @SotaT 

is the BR is on the sc_req_item table?

 

make it a after or Async insert BR 

 

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

    var propValue = gs.getProperty('〇〇'); //〇〇 is this a correct property name?
    if (propValue.includes(current.getValue('cat_item'))) {
        var reqGR = current.request.getRefRecord();
        if (reqGR.isValidRecord()) {
            reqGR.setValue('approval', 'approved');
            reqGR.update();
        }
    }

})(current, previous);

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@SotaT 

try this, I updated the script and added logs

(function executeRule(current, previous /*null when async*/ ) {
    var grProp = new GlideRecord('sys_properties');
    if (grProp.get('name', '〇〇')) {
        var propValue = grProp.getValue('value');
        var catItemId = current.cat_item.toString();
        if ((',' + propValue + ',').indexOf(',' + catItemId + ',') !== -1) {
            var reqGR = new GlideRecord('sc_request');
            var requestId = current.request.toString();
            gs.info('Request is' + requestId)
            if (reqGR.get(requestId)) {
                if (reqGR.getValue('approval') != "approved") {
                    reqGR.setValue('approval', 'approved');
                    reqGR.update();
                }
            }
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chaitanya ILCR
Kilo Patron

Hi @SotaT 

is the BR is on the sc_req_item table?

 

make it a after or Async insert BR 

 

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

    var propValue = gs.getProperty('〇〇'); //〇〇 is this a correct property name?
    if (propValue.includes(current.getValue('cat_item'))) {
        var reqGR = current.request.getRefRecord();
        if (reqGR.isValidRecord()) {
            reqGR.setValue('approval', 'approved');
            reqGR.update();
        }
    }

})(current, previous);

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

@Chaitanya ILCR 

I was able to resolve the issue. Thank you very much.