Updated work notes from the old value to the new value.

Abbas_Ahamed
Tera Contributor

Hi @Ankur Bawiskar,

 

I saw your earlier response in the community about updating work notes. I tried that method, and it successfully updates a single variable. However, I need to update multiple variables at once. For example, if there are 10 to 15 variables, what should I do? Could you explain how to achieve this?

 

Below is the code I’m using to update a single variable with the OnChange (Catalog Client Script):

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var str = 'Test value changed to ' + oldValue + ' to ' + newValue;
    g_form.setValue('work_notes', str);
    g_form.update();
}
2 ACCEPTED SOLUTIONS

@Abbas_Ahamed 

so what debugging did you do?

share your BR screenshot and latest script.

also small thing you will have to update is check the previous and current value, if different then only add that in array

I updated the script with gs.info(), use that and share the analysis

(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);

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

View solution in original post

@Abbas_Ahamed 

Glad to know that my script is working fine.

To get Display value update as this.

-> use getDisplayValue()

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

    // Add your code here
    var arr = [];
    var variableArr = ['test', 'software_title', 'requested_for'];
    for (var i = 0; i < variableArr.length; i++) {
        if (previous.variables[variableArr[i]] != current.variables[variableArr[i]]) {
            arr.push('Variable changed from ' + previous.variables[variableArr[i]].getDisplayValue() + ' to ' + current.variables[variableArr[i]].getDisplayValue());
        }
    }
    current.work_notes = arr.join('\n');

})(current, previous);

I believe I have answered your question and you can enhance it further.

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

View solution in original post

12 REPLIES 12

Rafael Batistot
Tera Sage

Hi @Abbas_Ahamed 

 

There are 2 ways that might help you 

 

1 ) Update multiple variables in one onChange client script

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var str = 'Test value changed from ' + oldValue + ' to ' + newValue;

// Update multiple variables
g_form.setValue('work_notes', str);
g_form.setValue('u_variable1', 'Updated Value 1');
g_form.setValue('u_variable2', 'Updated Value 2');
g_form.setValue('u_variable3', 'Updated Value 3');
g_form.setValue('u_variable4', newValue); // Example: re-use changed value
g_form.setValue('u_variable5', oldValue); // Example: store old value

// No need to call g_form.update() — setValue is enough
}

 

2) Dynamically update 10–15 variables

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var str = 'Changed from ' + oldValue + ' to ' + newValue;
g_form.setValue('work_notes', str);

// Suppose you have 10–15 variables to update
var varsToUpdate = ['u_var1', 'u_var2', 'u_var3', 'u_var4', 'u_var5'];

varsToUpdate.forEach(function(v) {
g_form.setValue(v, str); // set the same value for all
});
}

Hi @Rafael Batistot ,

 

Thank you for your response. I tried this method, but it isn't working for my situation because I have 15 variables. Is there another way to resolve this issue?

Ankur Bawiskar
Tera Patron
Tera Patron

@Abbas_Ahamed 

you will have to write those many onChange catalog client scripts on each of those variable

OR

Another way is to use before update business rule and grab the old value and new value of the variable and update the work notes

Something like this in before update BR on sc_req_item table

(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++) {
        arr.push('Variable changed from ' + previous.variables[variableArr[i]] + ' to ' + current.variables[variableArr[i]]);
    }
    current.work_notes = arr.join('\n');

})(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

@Abbas_Ahamed 

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