Total Field: Add or subtract integer dynamically

Desmo
Mega Guru

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

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

This works. Thank you!