Update work notes if variable changes

Joe Weisiger
Giga Expert

Hello,

I have an order guide/catalog item that uses a variable set and the variables are cascaded.  If someone changes a variable on an associated task I want to capture the change in the activity stream work note.  How can I accomplish this?

Thanks,

Joe

1 ACCEPTED SOLUTION

@Joe Weisiger 

 

Can you test this once 

var key;
	for(key in current.variables){
			if(current.variables[key] != previous.variables[key]){
				current.work_notes = "Variable changed "+key + "Value Changed to"+current.variables[key];
			}
	}

View solution in original post

16 REPLIES 16

@Joe Weisiger 

If you want this to run for all catalog items then just use this condition in BR

current.variables.changes()

For determining which variable changed use what mentioned by Pranav

Just to add on it you can print the previous value of variable as well

This will print all the variables which got changed with their previous value combined and then put in work notes

var str = '';

var key;
	for(key in current.variables){
			if(current.variables[key] != previous.variables[key]){
			    str = str + 'Variable Changed ' + key + ' from ' + previous.variables[key] + ' to ' + current.variables[key] + '\n';
			}
	}
current.work_notes = str;	

Regards
Ankur

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

Thank you, Ankur!  This is helpful.

This now prints the variable, previous value and new value.  It is also printing all of the variables which is not needed.  Is there a way to modify the script to avoid printing all of the variables?

 

Hi Joe The code that I provided is not working? Regards Pranav

Pranav,

Code is working!  It prints the variable, previous value and new value correctly.  

It is also printing all variables in the work note below this and I was wondering if there was a way to not print all the variables.  Just the changed variables.

 

find_real_file.png

@Joe Weisiger , The code that I provided was doing the same thing

 

var key;
	for(key in current.variables){
			if(current.variables[key] != previous.variables[key]){
				current.work_notes = "Variable changed "+key + "Value Changed to"+current.variables[key];
			}
	}