How to bring the age of oldest ticket created in the list of child task (x_sts_claims) to one of the fields in Parent Ticket (x_sts_mains) . Please suggest

Mishu
Tera Expert

How to bring the age of oldest ticket created in the list of child task (x_sts_claims) to one of the fields[Age of Oldest open child ticket ]  in Parent Table(x_sts_mains) . Any help would be appreciated .

1 ACCEPTED SOLUTION

Hi,

keep BR on parent table

Display

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

	// Add your code here
	var gr = new GlideRecord("x_sts_claims");
	gr.addQuery('status','open'); // give correct field name and value to check
	gr.orderBy('sys_created_on');
	gr.addQuery("parent",current.getUniqueValue()); // give correct parent field
	gr.setLimit(1);
	gr.query();
	if (gr.next()) {
		var nowTime = new GlideDateTime();
		var childTime = new GlideDateTime(gr.sys_created_on);

		var dur = GlideDateTime.subtract(childTime, nowTime);
		var days = parseInt(dur.getNumericValue()/86400000);

		current.fieldName = days; // give correct field name here
	}

})(current, previous);

Regards
Ankur

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

View solution in original post

12 REPLIES 12

I tried with display BR on child table with condition as State is Open :

 

var now = new GlideDateTime();
var created = new GlideTimeDate(current.sys_created_on);
var duration = GlideTimeDate.subtract(created, now);
var value = (duration.getNumericValue() / 1000);
var days = value / 86400;

var aud = new GlideAggregate('x_sts_claims');

aud.addQuery('status' , 'open');
aud.addQuery('parent, current.parent);
aud.addAggregate('MIN', days);
aud.setGroup(false);
aud.query();
while (aud.next()) {

 

var gr = new GlideRecord ('x_sts_mains');
gr.addQuery('sys_id' , current.parent);
gr.query();

if(gr.next())

{

gr.age_of_child_ticket =aud.getAggregate('MIN', days);

}

Hi,

you can use display business rule on parent table

1) query child table with this parent and use orderBy('sys_created_on') to get oldest child

I assume your field "Age of Oldest open child ticket" is string type

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

	// Add your code here
	var gr = new GlideRecord("x_sts_claims");
	gr.addQuery("state", "2"); // give correct query to indicate open state
	gr.orderBy('sys_created_on');
	gr.addQuery("parent", current.getUniqueValue());
	gr.setLimit(1);
	gr.query();
	if (gr.next()) {
		var nowTime = new GlideDateTime();
		var childTime = new GlideDateTime(gr.sys_created_on);

		var dur = GlideDateTime.subtract(childTime, nowTime);
		current.fieldName = dur.getDisplayValue();
	}

})(current, previous);

Regards
Ankur

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

The output or the aging your script is returning is in days ?

Hi,

you want in exact days ?

BR would be on parent table and not child

Regards
Ankur

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

yes I want to show the aging in days . The current script is not returning the correct age in days.