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

Samaksh Wani
Giga Sage
Giga Sage

You need to use Script Include for that.

Community Alums
Not applicable

Hi @Samaksh Wani , Could you help with that please.

Write a Client Script :-

 

var gr = new GlideRecord('table_name');
var comp = current.company;
 var ga = new GlideAjax('Script_include_name');
 ga.addParam('sysparm_name', 'script_include_function_name');
ga.addParam('sysparam_assignedTo', comp);

 

Script Include :- 

 

var Script_include_name = Class.create();
Script_include_name.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    script_include_function_name: function() {
        
        var comp = this.getParameter("sysparam_assignedTo")
        return comp;
    },
    type: 'Script_include_name'
});

 

You can Call this Script include and get the variable there.

 

Plz mark my SOlution as Accept, If you find it helpful.

 

Regards,

Samaksh

Community Alums
Not applicable

For which one