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.

Update the description field with catalog variable description on sc_task form

Sri29
Tera Contributor

I have below after business rule to run on Insert, which supposed to insert description on the sc_task by reading all the catalog variables descriptions record but it doesn't. If I change the case to before business rule, it gives error when inserting the record as 'Invalid insert' on page and also a flow associated to this fails as I see it doesn't like  grTask.update() call at the end. Any advise on what would be the best way  to resolve the issue.?

 

 

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

    var grTask = new GlideRecord('sc_task');
    grTask.get(current.getUniqueValue());
    var summaryStr = '';
	
    for (var key in grTask.variables) {
        //if key is empty discard the entry
        if (grTask.variables[key].getLabel()) {
            summaryStr += grTask.variables[key].getLabel() + " ==> " + grTask.variables[key].getDisplayValue() + "\n\n";
        }
    }
    grTask.setValue('description', summaryStr);
    grTask.update();
}

 

 

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@Sri29 Change the business rule to before insert and add the following script in the script field.

 

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

    var summaryStr = '';	
    for (var key in current.variables) {
        //if key is empty discard the entry
        if (current.variables[key].getLabel()) {
            summaryStr += current.variables[key].getLabel() + " ==> " + current.variables[key].getDisplayValue() + "\n\n";
        }
    }
    current.setValue('description', summaryStr);
}

 

Since this business rule runs before insert no need to use current.insert() or current.update() in this case.

 

Hope this helps.

 

View solution in original post

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

Sandeep Rajput
Tera Patron
Tera Patron

@Sri29 Change the business rule to before insert and add the following script in the script field.

 

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

    var summaryStr = '';	
    for (var key in current.variables) {
        //if key is empty discard the entry
        if (current.variables[key].getLabel()) {
            summaryStr += current.variables[key].getLabel() + " ==> " + current.variables[key].getDisplayValue() + "\n\n";
        }
    }
    current.setValue('description', summaryStr);
}

 

Since this business rule runs before insert no need to use current.insert() or current.update() in this case.

 

Hope this helps.