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

quayjian
Giga Contributor

Hi,

I have a few suggestions that might get you started.

 

First of all, whenever you deal with email scripts and record queries, it is safe to assume that you will require server side scripting. As such, the new/old value parameters seen on onChange() client scripts just wont work.

 

What you should consider instead is using the "previous" specifier seen on business rules. See the post below to learn more about current vs previous:

https://community.servicenow.com/community?id=community_question&sys_id=635d9dc9dbf94c106064eeb5ca96197b

 

That being said, previous is not accessible from email scripts. You will need to set up an event trigger and pass the previous values to param1 / param2. Since emails are triggered by events, you may call the parameter values from there. Please refer to the below article:

https://community.servicenow.com/community?id=community_question&sys_id=e6b383a5dbd8dbc01dcaf3231f96196f

Yash Agrawal1
Tera Guru

Hello Sironi,

Some one here,

Please try the below URL,I am also working on this solution to implement it on my instance.

https://community.servicenow.com/community?id=community_question&sys_id=e6b383a5dbd8dbc01dcaf3231f96...

Also check the below link.

https://docs.servicenow.com/bundle/madrid-application-development/page/script/server-scripting/conce...

 

Regards

Yash Agrawal

Yash Agrawal1
Tera Guru

Hello @Sironi ,

I tried very hard to get a solution,

Please follow the below steps:

1.Create an event.

2.Create a business rule with the same name, which you gave to  Event.

Write the below code there.

(function executeRule(current, previous /*null when async*/ ) {
var question,i,prv1,cur1;
    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])
				{
                        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);
				}	
			 //gs.eventQueue('updateevent',current,question,'Cuurent Value-'+cur1+',Previous Value-'+prv1);
         }
       
    }

})(current, previous);

4.To check,

Open any RITM, change the value of any 1 variable (for testing purpose)

Go to logs.

You will see the record which contains

Question: Name of the variable 

Current Value: Current Value

Previous value: Previous value that you modified.

If and Only if the value if modified/updated.

Please check the below scrrenshot

find_real_file.png

I am working on how to send this value in Notification,I will do that also,but need some time.till that time you can try to implement the provided solution.

Regards

Yash agrawal

Hello Sironi,

Did you check the logs from above screenshot?

You can see we are getting current value and previous value if and only if it is changed.

Please implement this solution till here,and check you are getting the same result or not.

If is giving you correct value ,then we can create notification to send the variable detials to the user.

I already implemented the solution on my instance

Regards

Yash Agrawal