Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to send notification with Old value , new value of variable when we change variable value in RITM

Sironi
Kilo Sage

Hi,

some one suggest me about this use case how to solve

I created a notification, just update it a bit as below

 

but that notification has to carry only modified variables details only with Old value , new value of variable when we change variable value in RITM

 

 

1 ACCEPTED SOLUTION

@Sironi 

please update as below and try once

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    template.print("<b>Summary of  items:\n</b>");

    var notiBodyString = event.parm2;
    var parsedData = JSON.parse(notiBodyString);

    template.print('<br>');

    gs.info("parsedData" + parsedData);
    template.print('parsedData----------' + parsedData + '\n' + '\n');

    var notiLength = parsedData.length;
    template.print('<br>');
    gs.info('notiLength' + notiLength);
    template.print('notiLength----------' + notiLength);

    for(var i=0;i<parsedData.length;i++){
    template.print('Variable ' + parsedData[i].variableName + ' old value ' + parsedData[i].oldValue + ' new value ' + parsedData[i].currentValue);    
    }

})(current, template, email, email_action, event);

Regards
Ankur

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

View solution in original post

41 REPLIES 41

@Sironi 

please update as below and try once

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    template.print("<b>Summary of  items:\n</b>");

    var notiBodyString = event.parm2;
    var parsedData = JSON.parse(notiBodyString);

    template.print('<br>');

    gs.info("parsedData" + parsedData);
    template.print('parsedData----------' + parsedData + '\n' + '\n');

    var notiLength = parsedData.length;
    template.print('<br>');
    gs.info('notiLength' + notiLength);
    template.print('notiLength----------' + notiLength);

    for(var i=0;i<parsedData.length;i++){
    template.print('Variable ' + parsedData[i].variableName + ' old value ' + parsedData[i].oldValue + ' new value ' + parsedData[i].currentValue);    
    }

})(current, template, email, email_action, event);

Regards
Ankur

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

@Sironi 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Hi Ankur,

why we are not receiving any response into Email-Script from BR, i didn't understand 

kindly take a look below result one

 

find_real_file.png

BR script:

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

    var question, i, prv1, cur1;

    //    var obj = {};
    var arr = [];
    if (current.variables.changes()) {
        var cur = current.variables.getElements();
        var pvr = previous.variables.getElements();
        gs.info('Current Value-' + current.variables.getElements());
        gs.info('Previous Value-' + previous.variables.getElements());
        for (i = 0; i < cur.length; i++) {
            if (cur[i] != pvr[i]) {
                var obj = {};
                question = cur[i].getQuestion().getLabel();
                gs.info('question' + question);

                cur1 = cur[i];
                gs.info('current value' + cur1);

                prv1 = pvr[i];
                gs.info('prvious value' + prv1);
                obj["variableName"] = cur[i].getQuestion().getLabel();
                obj["currentValue"] = cur[i].getQuestion().getDisplayValue();
                obj["oldValue"] = pvr[i].getQuestion().getDisplayValue();

                arr.push(obj);

            }

        }
        gs.info('Entered');
        gs.info("JSON:" + JSON.stringify(obj));
        gs.info("Array:" + JSON.stringify(arr));
		
        gs.eventQueue("updateventemail", current, gs.getUserID(),JSON.stringify(arr));
    }


})(current, previous);

 

Email Script:

template.print("<b>Summary of  items:\n</b>");

var notiBodyString = event.parm2;
var parsedData = JSON.parse(notiBodyString);

template.print('<br>');

gs.info("parsedData" + parsedData);
template.print('parsedData----------' + parsedData + '\n' + '\n');

var notiLength = parsedData.length;
template.print('<br>');
gs.info('notiLength' + notiLength);
template.print('notiLength----------' + notiLength);

for (var i = 0; i < parsedData.length; i++) {
    template.print('Variable ' + parsedData[i].variableName + ' old value ' + parsedData[i].oldValue + ' new value ' + parsedData[i].currentValue);
}

 

 

here is result:

org.mozilla.javascript.EcmaError: Empty JSON string
Caused by error in Email Script: 'sc_req_item_script_3' at line 4

1: template.print("<b>Summary of items:\n</b>");
2:
3: var notiBodyString = event.parm2;
==> 4: var parsedData = JSON.parse(notiBodyString);
5:
6: template.print('<br>');
7:

@Sironi 

It seems the json string is empty hence the error

did you check what came for this in logs?

 gs.info("Array:" + JSON.stringify(arr));

if it came then it should come in email script as well

Also remove event parameter 2 contains recipient checkbox for your notification

Regards
Ankur

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

yes Ankur,

i m getting JSON format value.

find_real_file.png