Help to track variable value changes on RITM

OllyI-B
Giga Contributor

Hi all

I'm currently working on a requirement to allow Service Desk users to edit the variables on RITM records if needed (and, yes, we have tried to persuade the requestor that they should be cancelled and re-raised but losing that battle).

I have the config done for allowing the changes to happen but am struggling to find a way to write the new variable values to a comment on the RITM so the Requested For can be notified. I have tried both a business rule and flow, but can't seem to consistently bring back the variable question text plus old and new values.

I can see this information in the history of the RITM but just can't translate that into anything useable. 

Has anyone tried this, or are there any suggestions of what to try?

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@OllyI-B 

you can use before update business rule on RITM table

See which variable got changed and add the old and new value to work notes

check this link and sample script

How to add the work notes when catalogue variables are changed on RITM Form 

OR

Sample script like this which worked for other where I shared solution

Updated work notes from the old value to the new value.  -> you need to add which variables to track here

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

    // Add your code here
    var arr = [];
    var variableArr = ['variable1', 'variable2', 'variable3'];
    for (var i = 0; i < variableArr.length; i++) {
        if (previous.variables[variableArr[i]] != current.variables[variableArr[i]]) {
            gs.info('value changed for variable' + variableArr[i]);
            arr.push('Variable changed from ' + previous.variables[variableArr[i]] + ' to ' + current.variables[variableArr[i]]);
        }
    }
    current.work_notes = arr.join('\n');

})(current, previous);

OR

Before update BR on sc_req_item table and it will track any variable which got changed

Condition: current.cat_item.name == 'Your Item Name' && current.variables.changes()

Script:

(function executeRule(current, previous /*null when async*/ ) {
    var question, prv1, cur1;
    var cur = current.variables.getElements();
    var pvr = previous.variables.getElements();
    var arr = [];
    for (var i = 0; i < cur.length; i++) {
        if (cur[i] != pvr[i]) {
            question = cur[i].getQuestion().getLabel();
            cur1 = cur[i]; // current value
            prv1 = pvr[i]; // previous value
            var str = 'The ' + question + 'has changed from ' + prv1 + ' to ' + cur1;
            arr.push(str + '\n');
        }
    }
    current.work_notes = arr.toString();

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

5 REPLIES 5

OllyI-B
Giga Contributor

No worries, have worked it out:

(function executeRule(current, previous /*null when async*/) {
    var question, prv1, cur1;
    var cur = current.variables.getElements();
    var pvr = previous.variables.getElements();
    var arr = [];

    for (var i = 0; i < cur.length; i++) {
        if (cur[i] != pvr[i]) {
            question = cur[i].getQuestion().getLabel();
            cur1 = cur[i]; // current value
            prv1 = pvr[i]; // previous value

            var str = question + ' has changed from ' + prv1 + ' to ' + cur1;
            arr.push(str);
        }
    }

    // Join with newline instead of commas
    current.work_notes = arr.join('\n');

})(current, previous);