Send a notification with old value and new value from history table on change of company name

Community Alums
Not applicable

I want to send a notification with the old value and new value (from sys_history_line table) when the company name changes on core_company table.

 

Notification Subject should be like below.

Company name changed from 'Old Company Name' to 'New Company Name'. 

 

How to get the old value on the notification subject/body?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why to use sys_history_line table?

you can use eventQueue based approach and send the old and new value

1) create event on that table

2) use after update BR on core_company table Name Changes to trigger the event

3) use script to trigger event and include the old and new value in event parameter

4) then use email script to print the old and  new value

5) include the email script in email body. I hope you are aware on how to do this

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

	// Add your code here
	gs.eventQueue('event_name', current, gs.getUserID(), current.name + '||' + previous.name);

})(current, previous);

Email script:

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

	// Add your code here
	var val = event.parm2.toString();
	var newValue = val.split('||')[0];
	var oldValue = val.split('||')[1];
	template.print('Company name changed from ' + oldValue + 'to ' + newValue);

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

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

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why to use sys_history_line table?

you can use eventQueue based approach and send the old and new value

1) create event on that table

2) use after update BR on core_company table Name Changes to trigger the event

3) use script to trigger event and include the old and new value in event parameter

4) then use email script to print the old and  new value

5) include the email script in email body. I hope you are aware on how to do this

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

	// Add your code here
	gs.eventQueue('event_name', current, gs.getUserID(), current.name + '||' + previous.name);

})(current, previous);

Email script:

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

	// Add your code here
	var val = event.parm2.toString();
	var newValue = val.split('||')[0];
	var oldValue = val.split('||')[1];
	template.print('Company name changed from ' + oldValue + 'to ' + newValue);

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

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

@Community Alums 

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

Hi Ankur,

 

Your approach is correct but getting values not labels. Can you please check below text which I am getting.

Previous Priority: Priority changed from 2to 1 ( Here I want ' 2 - High to 1 - Critical').

Hi Ankur,

 

Great thanks for your approach, It's working for my query.

 

Mail script :-  incident_priority_email_script

 

var val = event.parm2.toString();
    var newValue = val.split('||')[0];
    var oldValue = val.split('||')[1];
    template.print(newValue);
 
Business Rule :- After, Update
 
gs.eventQueue("incident.priority.changed", current, gs.getUserID(), previous.priority.getDisplayValue());