Get row count of related records

Priya75
Tera Contributor

Hi All, 

I have a field on the form as test. i want to populate this field on the basis of a related list, so i have a related list of business service which shows the records from cbdb_ci_service tables.i want to count of services related to that particular task and update that field. 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Priya,

When you want to populate that? when any new record gets added in related list?

if yes then you would require BR on that related list table to increment or decrement the count based on insert or delete

Regards
ankur

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

No, when a task is created or updated and a business service is added to that task.i can't have run a business rule on cmdb_ci_service table.i want that count from task table and need to update the field .could you provide some scripting

 

Hi Priya,

So you would require before update BR on the current table.

Sample script below

var gr = new GlideRecord('cmdb_ci_service');

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

gr.query();

current.<fieldName> = gr.getRowCount();

Regards
Ankur

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

i want it on cert_task table. when a related service is added or updated .

what i have to mention in current.field name.. when it is a table record. 

I have a certificate task form, in this the business services(cmdb_ci_services)is the related list record.i have a field on the certificate task form as test. i want to populate this field, for that i want to count all the service record in business service related list.