Unable to fetch Sla displayvalue/sysid in business rule

Siddharth Parna
Tera Contributor
Hello ServiceNow Community
I am trying to write business rule to create new Task sla when Assigned to changes.
As per the below script new Task sla is being created,but the new sla Defnition is showing blank.
I tried hardcoding newSLA.sla value to 'Sys_id' and it worked.I see that sla of initial task sla is not being retreived.I am trying to retreive through .getValue('sla') 
Could the community please suggest where would the correction be needed.
Thanks in Advance
 
 
(function executeRule(current, previous /*null when async*/) {

    // check if assignee has changed
    if(current.assigned_to!=previous.assigned_to){
        //check if task already has a Task SLA
        var slaGR = new GlideRecord('task_sla');
       
        var slaDisplay = slaGR.getDisplayValue('sla');
        slaGR.addQuery('task', current.sys_id);
        //slaGR.addQuery('task', slaGR.getValue('sla'));
        slaGR.query();

    if(slaGR.hasNext()){
        //create a new Task SLA
        var newSLA = new GlideRecord('task_sla');
        var slasysid = slaGR.getValue('sla');
        newSLA.initialize();
        newSLA.task = current.sys_id;
        newSLA.sla = slasysid;
        newSLA.start_time = new GlideDateTime();
        newSLA.stage = 'In Progress';
        //newSLA.
        newSLA.insert();
    }
    }

})(current, previous);
 
SLA Get Value.PNG
6 REPLIES 6

Chaitanya ILCR
Kilo Patron

Hi @Siddharth Parna ,

try this

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

    // Check if assignee has changed
    if (current.assigned_to != previous.assigned_to) {
        var slaGR = new GlideRecord('task_sla');
        slaGR.addQuery('task', current.sys_id);
        slaGR.query();

        if (slaGR.next()) {
            // Retrieve the SLA sys_id from the existing task_sla record
            var slasysid = slaGR.getValue('sla');

            // Create a new Task SLA
            var newSLA = new GlideRecord('task_sla');
            newSLA.initialize();
            newSLA.task = current.sys_id;
            newSLA.sla = slasysid;
            newSLA.start_time = new GlideDateTime();
            newSLA.stage = 'In Progress';
            newSLA.insert();
        }
    }

})(current, previous);

 

 

but you can the condition in the SLA definition it self that if assigned to changes attach the sla 
this is the right approach 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

I tried this script but I still I see the new sla defnition showing empty

Abbas_5
Tera Sage
Tera Sage

Hello @Siddharth Parna,

 

In a ServiceNow business rule, you may be encountering issues when trying to retrieve the display value of an SLA instead of its sys_id. This can happen because the current.sla field directly returns the sys_id of the SLA record, not the human-readable display value. To get the display value, you need to use the getDisplayValue() method on the current.sla object. 
 
Here's how to correctly retrieve the SLA's display value in a business rule:
1. Use getDisplayValue()
  • Instead of current.sla, use current.sla.getDisplayValue() to access the display value of the SLA. 
     
2. Example:
   (function executeRule(current, previous /*null when async*/) {       // Get the display value of the SLA       var slaDisplayValue = current.sla.getDisplayValue();       // Use the display value in your logic (e.g., logging, updating fields)       gs.log("SLA Display Value: " + slaDisplayValue);   })(current, previous);
3. Retrieving other SLA attributes:
  • If you need other details from the SLA record besides the display value, you can query the sla table using GlideRecord 
   (function executeRule(current, previous /*null when async*/) {       // Get the display value of the SLA       var slaDisplayValue = current.sla.getDisplayValue();       // Get the SLA record itself       var slaGR = new GlideRecord('sla');       slaGR.addQuery('sys_id', current.sla); // Assuming 'current.sla' contains the sys_id       slaGR.query();       if (slaGR.next()) {           // Access other SLA attributes           var slaName = slaGR.name; // Example: Get the SLA name           var slaDescription = slaGR.description; // Example: Get the SLA description           gs.log("SLA Display Value: " + slaDisplayValue + ", SLA Name: " + slaName);       }   })(current, previous);
By using getDisplayValue() and querying the sla table when needed, you can ensure you have the correct information for your business rule logic.
 
If this is helpful, please hit the thumbs button and accept the correct solution by referring to this solution in future it will be helpful to them.
 
Thanks & Regards,
Abbas Shaik

I tried this approach but still I dont see sla defnition being mapped for the new sla created

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

    // check if assignee has changed
    if(current.assigned_to!=previous.assigned_to){
        //check if task already has a Task SLA
        var slaGR = new GlideRecord('task_sla');
       
        //var slaDisplay = slaGR.getDisplayValue('sla');
        var slaDisplay = current.sla.getDisplayValue();
        slaGR.addQuery('task', current.sys_id);
        //slaGR.addQuery('task', slaGR.getValue('sla'));
        slaGR.query();

    if(slaGR.hasNext()){
        //create a new Task SLA
        var newSLA = new GlideRecord('task_sla');
        var slasysid = slaGR.getValue('sla');
        //var slaName = slaGR.name
        newSLA.initialize();
        newSLA.task = current.sys_id;
        newSLA.sla = slaDisplay;
        //newSLA.sla = slaDisplay;
        newSLA.start_time = new GlideDateTime();
        newSLA.stage = 'In Progress';
        //newSLA.
        newSLA.insert();
    }
    }

})(current, previous);