Business rule

Prashant8kumar
Tera Contributor
This is a after insert BR on sc_task table and I am not able to get all the fields populated in u_task_sla_m2m table, only able to get the   start time and sla type. (slam2m.u_start_time = taskSLA.start_time; and  slam2m.u_sla_type = 'Resolution SLA';) other than these 2 fields I am not able to get any values from task_sla table to to u_task_sla_m2m table.
 
(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    gs.info("Prashant: the shipping address " + current.request_item.variables.shipping_address);
    if (!current.remote_flag || current.request_item.variables.shipping_address == 'Office Address') {
        var shippingAddress = current.request_item.variables.u_shipping_address;
        var locationGr = new GlideRecord('cmn_location');
        locationGr.addQuery('sys_id', shippingAddress);
        locationGr.query();
        gs.info("Prashant: the location " + shippingAddress);
        if (locationGr.next()) {
            // Getting the value of the coast field from the location table
            var coast = locationGr.u_coast.toString();
            gs.info("Prashant: the coast " + coast);

            var slaName;
            if (coast === 'East') {
                slaName = 'MYOB AU EAST Coast';
            } else if (coast === 'West') {
                slaName = 'MYOB AU West Coast';
            } else {
                slaName = 'MYOB NZ';
            }
            gs.info("Prashant: the sla " + slaName);

            var slaGr = new GlideRecord('contract_sla');
            slaGr.addQuery('name', slaName);
            slaGr.query();
            if (slaGr.next()) {
                var taskSLA = new GlideRecord('task_sla');
                taskSLA.initialize();
                taskSLA.task = current.sys_id; // sys_id of the current sc_task record
                taskSLA.sla = slaGr.sys_id; // sys_id of the matching contract_sla record
                taskSLA.insert();

                var slam2m = new GlideRecord('u_task_sla_m2m');
                slam2m.initialize();
                slam2m.u_rejection_task = current.sys_id;
                slam2m.u_sla_definition = slaGr.sys_id;
                //task.u_sla_definition = current.sla.sys_id;
                slam2m.u_start_time = taskSLA.start_time;
                slam2m.u_end_time = taskSLA.end_time;
                slam2m.u_breach_time = taskSLA.planned_end_time;
                slam2m.u_stage = taskSLA.stage.getDisplayValue();
                slam2m.u_business_time_left = taskSLA.business_time_left;
                slam2m.u_business_elapsed_time = taskSLA.business_duration;
                slam2m.u_business_elapsed_percentage = taskSLA.business_percentage;
                slam2m.u_has_breached = taskSLA.has_breached;
                slam2m.u_sla_type = 'Resolution SLA';
                slam2m.insert();
            }

        }
    }
3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

This is expected behavior.

What I understood from your script is you are inserting task_sla record after sc_task is created  and in same BR you are trying to copy the task_sla fields.

As the task_sla record is just inserted and the SLA has not processed so its End Time, Breach time, Duration etc fields should be empty and these values will get updated in task_sla record based on your SLA defination.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Prashant8kumar
Tera Contributor

what could be the alternative?

Mathieu Lepoutr
Mega Guru

Hi Prashant8kumar

 

 

Its my understanding that  the script successfully populates two fields in the "u_task_sla_m2m" table: "u_start_time" and "u_sla_type." However, the other fields don't seem to be getting populated correctly from the "task_sla" table.

To identify the issue, we need to examine the relationship between "sc_task," "contract_sla," and "task_sla" tables in the ServiceNow instance. It's possible that there is a problem with the query or the data retrieval process.

 

Exalate could be an option to synchronize data between ServiceNow instances or other systems. It has the capability to get all the records from different tables, including look-up tables. By integrating Exalate, you may be able to streamline data synchronization and ensure that relevant fields in "u_task_sla_m2m" are correctly populated based on data from other tables. I have been using it for quite a while and it works wonders.