
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2020 07:13 PM
Hello Community,
Help with existing script or accepting possible alternate solution(s).
I have a business rule that sends an update from the Interaction Log table to the Interaction table. Currently, the function sends the value of Time Worked to Total Work. Total Worked is accumulated (meaning interaction logs add to the existing value; does not replace the existing value.)
Note: There can be more than one Interaction Log records related to one Interaction record. Looking for scripts to meet other requirements.
Requirements:
- Add or subtract from Total Work based on value changes to an Interaction Log record
- Subtract Time Worked when an interaction log record is deleted
(function executeRule(current, previous /*null when async*/) {
var gr= new GlideRecord('interaction');
gr.addQuery('sys_id',current.interaction); //query record number
gr.query();
if(gr.next())
{
gr.u_total_work = current.u_time_worked; //send value to Total Work field on interaction table
gr.update();
}
})(current, previous);
Desmo
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2020 02:28 AM
Hi,
you can have after insert/update business rule on interaction log table with sample script below for adding/subtracting the value
I assume this is after insert/update business rule;
Note: Ensure you use proper field on interaction log table which stores the interaction information
Also proper table name for interaction
(function executeRule(current, previous /*null when async*/) {
var value = 0;
// query all interaction logs for this current interaction
// add up the time worked from these to variable
// set the parent field with this value
var gr= new GlideRecord('interaction_log');
gr.addQuery('<interactionField>', current.<interactionField>); //query record number
gr.query();
while(gr.next())
{
value = value + gr.u_time_worked;
}
var interactionRecord = new GlideRecord('interaction');
interactionRecord.get(current.<interactionField>);
interactionRecord.u_total_work = value;
interactionRecord.update();
})(current, previous);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2020 02:02 AM
Hi,
You are replacing the value of 'u_total_work' with 'u_time_worked' with the below line-
gr.u_total_work = current.u_time_worked;
If you want to add/subtract the values every time an interaction log is added use this-
gr.u_total_work += current.u_time_worked; //This will add new values to the existing value
gr.u_total_work -= current.u_time_worked; //This will subtract values from the existing value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2020 02:28 AM
Hi,
you can have after insert/update business rule on interaction log table with sample script below for adding/subtracting the value
I assume this is after insert/update business rule;
Note: Ensure you use proper field on interaction log table which stores the interaction information
Also proper table name for interaction
(function executeRule(current, previous /*null when async*/) {
var value = 0;
// query all interaction logs for this current interaction
// add up the time worked from these to variable
// set the parent field with this value
var gr= new GlideRecord('interaction_log');
gr.addQuery('<interactionField>', current.<interactionField>); //query record number
gr.query();
while(gr.next())
{
value = value + gr.u_time_worked;
}
var interactionRecord = new GlideRecord('interaction');
interactionRecord.get(current.<interactionField>);
interactionRecord.u_total_work = value;
interactionRecord.update();
})(current, previous);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 01:18 PM
Hi Ankur,
I am not seeing the updates to Total Time Worked field on the Interaction table.
Curious if the following line needs a sys_id instead of the field name?
gr.addQuery('<interactionField>', current.<interactionField>); //query record number

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 02:29 PM
This works. Thank you!