Before BR with Update

karthik65
Tera Guru
Hi ,
 
I want to remove update from this BR. Is there a way to remove update from the Script and Glide Api.
 
    updateChildren('comments', gs.getMessage('Comment copied from RITM'));
 
 
 
function updateChildren(fieldName, msg) {
    var rec = new GlideRecord("sc_task");
    rec.addQuery('request_item', current.sys_id);
    //rec.addActiveQuery();
    rec.orderBy();
    rec.query();
 
    var fieldRawValue = current.getValue(fieldName) + '';
    var fieldValue = fieldRawValue.trim();
    if (!fieldValue || fieldValue.length <= 0)
        return;
 
    while (rec.next()) {
 
        if (fieldRawValue.indexOf('Comment copied from') == 0)
            rec[fieldName] = fieldRawValue;
        else
            rec[fieldName] = msg + ': \n' + fieldRawValue;
if(rec.state == '-5' && (rec.request_item.request.requested_for == gs.getUserID() || rec.request_item.request.opened_by == gs.getUserID())){
rec.setValue('state', '2');
}
        rec.update();
 
    }
 
}
7 REPLIES 7

Hi Ankur,

 

Its an Before Br to check and Update comments in Sctask from RItm. Business is asking Update in a Before BR is not best practice. 

@karthik65 

BR is on RITM and you want to update another table SC Task.

Then ideally it should be after update BR.

We usually use before update BR if we want to set fields on the same table on which BR is defined

So please convert your BR to after update and you should be good to go

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

Amit Gujarathi
Giga Sage
Giga Sage

HI @karthik65 ,
I trust you are doing great.
Please find the below reference code

function updateChildren(fieldName, msg) {
    var rec = new GlideRecord("sc_task");
    rec.addQuery('request_item', current.sys_id);
    rec.orderBy();
    rec.query();

    var fieldRawValue = current.getValue(fieldName) + '';
    var fieldValue = fieldRawValue.trim();
    if (!fieldValue || fieldValue.length <= 0)
        return;

    while (rec.next()) {
        if (fieldRawValue.indexOf('Comment copied from') == 0)
            rec[fieldName] = fieldRawValue;
        else
            rec[fieldName] = msg + ': \n' + fieldRawValue;

        if (rec.state == '-5' && (rec.request_item.request.requested_for == gs.getUserID() || rec.request_item.request.opened_by == gs.getUserID())) {
            rec.setValue('state', '2');
        }
        // Comment out the next line to remove the update operation
        // rec.update();
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi