Call Glide record of table (RITM) in the Business rule which is written on same table (RITM)

Renu9
Tera Contributor

Hi ,

I am writing an After Business rule on RITM table, where in as per requirement I also need to glide record to the same RITM table in that Business rule, Is it possible to call glide record of same table.

In the After Business rule, as current.update() should not be used, we were asked to use gr.update(); to satisfy the requirement.

Please guide me how to call the same table using glide record.

1 ACCEPTED SOLUTION

Hi,

If RITM has no approval then why to calculate the difference?

You should be concerned about only RITMs which have approval

After update BR on sysapproval_approver

Condition: current.source_table == 'sc_req_item'

Script:

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

	// Add your code here

	var ritm = new GlideRecord('sc_req_item');
	ritm.get(current.sysapproval);

	var opened = new GlideDateTime(ritm.sys_created_on);
	var nowTime = new GlideDateTime();

	var duration = GlideDateTime.subtract(opened, nowTime); 
	ritm.u_duration = duration;
	ritm.update();

})(current, previous);

regards
Ankur

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

View solution in original post

19 REPLIES 19

Mahendra RC
Mega Sage

Hello Renu,

If you are trying to update the same record by Gliding the sc_req_item and hence trying to use gr.update() then that should not be done. because you are updateing current record and after updating current record you are querying the same record and updating it. So in this case all the BRs will again run for this record update as well.

What is restricting you from using Before BR?? Can you please let us know??

So to implement your requirement you can have 1 BR on Approval table and 1 BR on RITM table:

After BR on sysapproval_approver table 

Condition: current.state.changesTo("approved") && current.source_table == 'sc_req_item'

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

	var ritm = new GlideRecord('sc_req_item');
	ritm.get(current.sysapproval);

	var opened = new GlideDateTime(ritm.sys_created_on);
	var nowTime = new GlideDateTime();

	var duration = GlideDateTime.subtract(opened, nowTime); 
	ritm.u_duration = duration;
	ritm.update();

})(current, previous);

Before BR on sc_req_item:

Condition: current.state.changesTo(3)

(function executeRule(current, previous /*null when async*/) {
	var opened = new GlideDateTime(current.sys_created_on);
	var nowTime = new GlideDateTime();

	var duration = GlideDateTime.subtract(opened, nowTime); 
	current.<field_name> = duration;
})(current, previous);

Please mark this as helpful/correct, if it answer your question.

Thanks

 

Hi Mahendra,

If the RITM is auto approved (no approvers), then that has to be populated with 0 seconds . It is needed as per reporting purpose.

 

if you want to set 0 seconds for the RITM that does not have any approver, then please try to set the default value for your field in dictionary and change the BR on sysapproval_approver table as mentioned below:

Initally the field value will be 0 and if the approval record is generated later it will be blanked out. So once the approval is approved the field will be updated with correct duration.

make the BR as After Insert and Update BR

Condition: (current.operation() == "insert") || (current.state.changesTo("approved") && current.operation == "update") && current.source_table == 'sc_req_item'

(function executeRule(current, previous /*null when async*/) {
	var duration = "";
	var ritm = new GlideRecord('sc_req_item');
	ritm.get(current.sysapproval);
	if (current.operation() == "update") {
		var opened = new GlideDateTime(ritm.sys_created_on);
		var nowTime = new GlideDateTime();

		duration = GlideDateTime.subtract(opened, nowTime);
		
		ritm.u_duration = duration;
		ritm.update();
	}else if (current.operation() == "insert"){
		ritm.u_duration = duration;
		ritm.update();
	}
})(current, previous);

Please mark this as helpful/correct, if it answer your question.

Thanks

 

Hello Renu,

Just wanted to check with you, if the my above response answered your question. If yes, then please do close this thread/question my marking the appropriate response as correct.

If you still need any further help or guidance on this then please update those on this question.

Thanks

Nagoor Basha 1
Tera Contributor

HI Renu,

 

As a Policy Exception Requester, I need the Risk Rating field to be calculated based on inputs.