Business duration left on Incident form

Abishek1998
Tera Contributor

Hi all,

 

I have created a new field named as business time left and Wrote a business rule on incident table to query the related task sla record and copy the same value and gets pasted there. now the field is not populated. I am pasting my script below.

My requirement is when the business time left  in task sla changes, The field on the incident form should also reflect same.

It is an ASYNC business rule.

(function executeRule(current, previous) {

    var slaGR = new GlideRecord('task_sla');
    slaGR.addQuery('task', current.sys_id);
    //slaGR.addQuery('active', true);
    slaGR.addQuery('sla.target', 'resolution');
    slaGR.setLimit(1);
    slaGR.query();

    if (slaGR.next()) {
        current.u_business_time_left = slaGR.getValue('business_time_left');
        current.update();
    }

})(current, previous);
3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Abishek1998 

you should have after update BR on task_sla with condition as Business time left Changes

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

    // Ensure we are only targeting incidents
    if (current.task.sys_class_name == 'incident') {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(current.task)) {
            // Copy the business time left value from task_sla to the incident
            incidentGR.u_business_time_left.setDateNumericValue(current.business_time_left.dateNumericValue());
            incidentGR.update();
        }
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @Abishek1998 

 

What type of Field  business time left is? 

 

In this line  parse the value properly , it will be set .

current.u_business_time_left = slaGR.getValue('business_time_left');

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

vaishali231
Kilo Sage

Hey @Abishek1998 

 

The main issue is that your Business Rule is running on the Incident table, while the business_time_left field is updated on the related task_sla record. Changes to a Task SLA record do not trigger a Business Rule on the Incident table, so the Incident field will not automatically stay in sync.

For this requirement, I would recommend creating an After Update Business Rule on the Task SLA [task_sla] table and updating the related Incident whenever business_time_left changes.

 

Business Rule Configuration:

Table: Task SLA [task_sla]

When: After Update

Condition: current.business_time_left.changes()

Script :

(function executeRule(current, previous) {
    // Ensure the Task SLA is associated with an Incident
    var incGR = new GlideRecord('incident');
    if (incGR.get(current.task)) {
        incGR.setValue(
            'u_business_time_left',
            current.getValue('business_time_left')
        );
        // Prevent unnecessary workflow processing if desired
        incGR.setWorkflow(false);
        incGR.update();
    }
})(current, previous);

 

A few additional points:

  1. Avoid using current.update() inside a Business Rule on the same record unless absolutely necessary, as it can lead to recursion and performance issues.
  2. Verify that u_business_time_left is the correct field type to store the SLA value.
  3. If multiple SLAs exist on the Incident, you may need additional filtering to identify the correct SLA record (for example, Resolution SLA only).

This approach ensures that whenever the SLA engine updates business_time_left, the Incident field is updated as well.

 

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb