Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Calculate Records from Related List to Add to Field on Incident Table

Zo2
Tera Contributor

Hi All,

I am trying to "Calculate records from a related List to add to field on the incident table".

I imagine it would be a client script as i would need for it to update onLoad and onChange.

Im not too sure how i would write the script but think I should use the GlideAggregate function.

Your help/guidance is much appreciated

1 ACCEPTED SOLUTION

Hi,

So u_related_incident field stores the incident details.

Is it a string field or reference field ?

1) if it is string field then use this query along with other

gr.addQuery('u_related_incident',current.number);

2) if it is reference field then use this query along with other

gr.addQuery('u_related_incident',current.sys_id);

then it would give the count of new call records for this current incident and store that count in the count field

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Ankur Bawiskar
Tera Patron

Hi,

use display business rule to query related list table with the current incident record; get the row count

store in g_scratchpad variable

in onload client script set that scratchpad variable value into that field

sample script here; I have queried the task_sla table as an example here

var gr = new GlideRecord('task_sla');
gr.addQuery('task', current.sys_id);
gr.query();
var rowCount = gr.getRowCount();

g_scratchpad.rowCount = rowCount;

function onLoad(){

g_form.setValue('field', g_scratchpad.rowCount);

}

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Zo2
Tera Contributor

Hi Ankur,

Thanks for the code but it did not seem to work. I tried to also add an encoded query but also got the same results. I think the business rule is causing the issue because i am getting undefined in the field. please see the changes i made below

 

Business rule

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

var gr = new GlideRecord('new_call');
gr.addEncodedQuery('call_type=incident^u_related_incident!=NULL');
gr.query();
var rowCount = gr.getRowCount();

g_scratchpad.rowCount = rowCount;

})(current, previous);

 

Client Script

 

function onLoad() {
g_form.addErrorMessage('test');
g_form.setValue('u_total_number_of_calls_received', g_scratchpad.rowCount);

}

 

Thanks for your quick reposnse

Hi,

Can you try to run this script in background and check what rowCount you are getting

var gr = new GlideRecord('new_call');
gr.addEncodedQuery('call_type=incident^u_related_incident!=NULL');
gr.query();
var rowCount = gr.getRowCount();

gs.info('Row Count is: ' + rowCount);

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

It provides a row count of 16.