The CreatorCon Call for Content is officially open! Get started here.

Overriding OOB script include

swapnil15
Tera Contributor

Hi All,

I have created an after-insert Business Rule on the CASE table that associates a change to a CASE:

 

var changeGr = new GlideRecord("change_request");
changeGr.initialize();
changeGr.chg_model = '007c4001c343101035ae3f52c1d3aeb2';
changeGr.insert();
current.change = changeGr.sys_id;
current.assignment_group = 'b7f666c0972861d0f084fa11f053af70';
current.update();

 

This part works as expected.

Next, I am trying to push additional comments from the CASE to the Change Request:

 

var regex = new RegExp('\n'); // Correctly defined regex for newline
var relchg = current.change;
var chg = new GlideRecord('change_request');
chg.addQuery('sys_id', relchg.sys_id);
chg.query();

if (chg.next()) {
    var chgWorkNotes = chg.work_notes.getJournalEntry(1).trim();
    var caseWorkNotes = current.work_notes.getJournalEntry(1).trim();

    var i = chgWorkNotes.search(regex);
    if (i > 0) {
        chgWorkNotes = chgWorkNotes.substring(i + 1);
    }

    var c = caseWorkNotes.search(regex);
    if (c > 0) {
        caseWorkNotes = caseWorkNotes.substring(c + 1);
    }
    if (chgWorkNotes.indexOf(caseWorkNotes) == -1) {
        // Append new work notes instead of replacing
        chg.work_notes = (chg.work_notes ? chg.work_notes + '\n' : '') + caseWorkNotes;
    }
    var chgComments = chg.comments.getJournalEntry(1).trim();
    var caseComments = current.comments.getJournalEntry(1).trim();
    var j = chgComments.search(regex);
    if (j > 0) {
        chgComments = chgComments.substring(j + 1);
    }
    var k = caseComments.search(regex);
    if (k > 0) {
        caseComments = caseComments.substring(k + 1);
    }
    if (chgComments.indexOf(caseComments) == -1) {
        chg.comments = (chg.comments ? chg.comments + '\n' : '') + caseComments;
    }
    chg.update();
}

 

This also works as expected.

However, I am facing an issue where comments from the CASE are being pushed back to the CASE from the Change Request (PFA). This behavior is triggered by an out-of-the-box (OOB) script include called CSMChangeIntegration.

 

Ask - Since it is not recommended to edit existing OOB script includes, I plan to clone this script include and add a new function. I would like to: 

1. Preventing Comment Updates: How can I modify or create a script to prevent the OOB function updateCaseWorkNotesWithChangeComments from pushing comment updates from Change Requests back to CASE?

 

2. Cloning and Overriding: If I call the cloned script include in my Business Rule, how would that override the OOB script include?

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@swapnil15 

2 ways to handle

1) use setWorkflow(false) when you update the change so that this OOB BR doesn't trigger

AnkurBawiskar_0-1737634086849.png

 

OR

2) Extend that script include and override that function and comment that line. This way you are not updating any OOB script include.

Extend a Script Include 

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

@swapnil15 

Hope you are doing good.

Did my reply answer your question?

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