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

Hi Sironi,

the issue is the table which stores the variable information is not audited.

in the BR you can do this

1) get the previous variables value and store in json object

2) get the current variables value and store in json object

Yash has already shared the BR script

Regards
Ankur

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

Hi Ankur,

i want this functionality for email notification,

if i use BR script how can i send current, previous data from notification. 

@Sironi 

you can use this in BR script

(function executeRule(current, previous /*null when async*/ ) {
var question,i,prv1,cur1;

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();
				}	
         }
       gs.eventQueue('updateevent', current, gs.getUserID(), JSON.stringify(arr));
    }

})(current, previous);

then use this in email script

(function runMailScript(current, template, email, email_action, event) {

	// Add your code here
	var jsonString = event.parm2;
	var parser = JSON.parse(jsonString);
	for(var i=0;i<parser.length;i++){
		template.print('Variable ' + parser[i].variableName + ' old value ' + parser[i].oldValue + ' new value ' + parser[i].currentValue);
	}

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

Note: this is not tested

Regards
Ankur

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

Hi Ankur,

i tried you script, it didn't worked  and i made small changes but still it is not working.

here is test result:

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


})(current, previous);

i have updated 3-variables , but it is always taking single variable details that too Date variable details only.

Hi Ankur,

i did small changes in you script and tried, then it worked fine, 

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

 Result: Array : 

Array:[{"variableName":"value","currentValue":"test 123456","oldValue":"tyerter"},{"variableName":"Date","currentValue":"2020-10-01 12:17:30","oldValue":"2020-10-15 12:17:30"}]

 

 

Email Script: i tried below script, 

(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 parser = new JSONParser();
    var parsedData = parser.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);

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

email script :

find_real_file.png

 

 

gs.eventQueue("updateventemail", current, JSON.stringify(arr), '');

when i tried this format email script:

var myList = JSON.parse(event.parm1);
gs.info("EventParm1:"+myList);
template.print("EmailJSONname  " + myList);
template.print("EmailJSONname 2"+event.parm1);

Result:

org.mozilla.javascript.EcmaError: Expected end of stream at char 4
Caused by error in Email Script: 'sc_req_item_script_3' at line 1

==> 1: var myList = JSON.parse(event.parm1);
2: gs.info("EventParm1:"+myList);
3: template.print("EmailJSONname " + myList);
4: template.print("EmailJSONname 2"+event.parm1);