Update case (parent) ticket status when task (child) is re-assigned

Gautam Gattu
Mega Expert

Requirement - When task (child) is re-assigned, case (parent) state should be updated to "In Progress" and comments should be added <task> has been re-assigned from <previous.assignment_group> to <current.assignment_group>

Created Business Rule

When - before and Update

===============================

(function onBefore(current, previous) {
    var gr = new GlideRecord('sn_customerservice_case');
    var parentstate = "In Progress";
    var casestate = state[current.state];
    var parentcase = current.parent.getRefRecord();
    var prevasngrp = previous.assignment_group;
    var currasngrp = current.assignment_group;
    if (parentcase.isValidRecord() && prevasngrp != currasngrp) {
    parentcase.state.changesTo(parentstate);
    current.work_notes = "[code]<a href='/" + current.sys_class_name + ".do?sys_id=" + current.sys_id + "'>" + current.number + "</a>[/code]" + "has been re-assigned from" + prevasngrp + "to" + currasngrp;
    gr.update();
    }
})(current, previous);

 

Could you please help in fixing this.

1 ACCEPTED SOLUTION

Gautam Gattu
Mega Expert

I found the solution for the requirement however there are few more features which I incorporated

1. When comments is added in child case, parent case get updated

2. If there is a change in 'assignment group' or 'assigned to', comments get added in parent case 

======================================================================

function onBefore(current, previous) {
    var states = {
        "1": "Open",
        "2": "In Progress",
        "3": "Pending",
        "4": "Closed"
    };
    var currentState = states[current.state];
    var previousState = states[previous.state];
    var parentcase = current.parent.getRefRecord();
    var task = "[code]<a href='/" + current.sys_class_name + ".do?sys_id=" + current.sys_id + "'>" + current.number + "</a>[/code]";
    var notes = current.work_notes.getJournalEntry();
    var parentState = current.parent.state.getDisplayValue();
    var subject = current.short_description;
    var closedat = current.closed_at;
    var asngrp = current.assignment_group.getDisplayValue();
    var asnto = current.assigned_to.getDisplayValue();

    
    if (parentcase.isValidRecord()) {
        if (current.state.changesTo(4)) {
            parentcase.work_notes = gs.getMessage("{0} assigned to {1} has been Closed.\n\n" + "Subject: {2}\n\n" + "Closed Notes: {3}\n\n" + "Closed at :{4}\n\n", [task, asngrp, subject, notes, closedat]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.state.changesTo(2) || current.state.changesTo(3) && current.work_notes.changes() && (current.assignment_group.changes() || current.assigned_to.changes())) {
            parentcase.work_notes = gs.getMessage("{0} Updated:\n\n" + "State: {1} to {2}\n\n" + "Assignment Group: {3}\n\n" + "Assigned To: {4}\n\n" + "Notes: {5}\n", [task, previousState, currentState, asngrp, asnto, notes]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.state.changesTo(2) || current.state.changesTo(3) && current.assignment_group.changes()) {
            parentcase.work_notes = gs.getMessage("{0} Updated:\n\n" + "State: {1} to {2}\n\n" + "Assignment Group: {3}\n\n" + "Assigned To: {4}\n", [task, previousState, currentState, asngrp, asnto]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.state.changesTo(2) || current.state.changesTo(3) && (current.assignment_group.changes() || current.assigned_to.changes())) {
            parentcase.work_notes = gs.getMessage("{0} Updated:\n\n" + "State: {1} to {2}\n\n" + "Assignment Group: {3}\n\n" + "Assigned To: {4}\n", [task, previousState, currentState, asngrp, asnto]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.state.changesTo(2) || current.state.changesTo(3) && current.work_notes.changes()) {
            parentcase.work_notes = gs.getMessage("{0} Updated:\n\n" + "State: {1} to {2}\n\n" + "Notes: {3}\n", [task, previousState, currentState, notes]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.state.changesTo(2) || current.state.changesTo(3)) {
            parentcase.work_notes = gs.getMessage("{0} Updated:\n\n" + "State: {1} to {2}\n", [task, previousState, currentState]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.comments.changes() || current.work_notes.changes()) {
            parentcase.work_notes = gs.getMessage("{0} Updated: \n\n" + "Notes: {1}\n", [task, notes]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        } else if (current.assignment_group.changes() || current.assigned_to.changes()) {
            parentcase.work_notes = gs.getMessage("{0} Updated: \n\n" + "Assignment Group: {1}\n\n" + "Assigned To: {2}\n", [task, asngrp, asnto]);
            //Set Parent case state to In Progress
            current.parent.state.setValue(10);
            parentcase.update();
        }

    }
}

View solution in original post

16 REPLIES 16

@Ankur Bawiskar - Could you please elaborate more on this.

Hi Gautam,

Can you explain few points:

1) on which table the BR is present

2) what is the type of BR

3) which record's state and work notes you want to update

please share the script

Regards
Ankur

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

Hi Ankur,

For point number #1 & #2, please refer the screen shot.

#3. When task (sn_customerservice_task) assignment group changes, case (sn_customerserivce_case) status should change to "In Progress" (value = 10) and comments should be added.

Hi Gautam,

when you are querying the table (sn_customerservice_case) you are querying with sys_id as current.sys_id; but current.sys_id would give sys_id of sn_customerservice_task table hence it is not working

So update it with the field in table sn_customerservice_task which refers the sn_customerservice_case table

Also in the condition of the BR give assignment group changes

gr.addQuery('sys_id', current.parent); // use proper field here

Also set the state like this

gr.state = 10;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

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

Hello Gautum, 

Based on your screenshot 2.jpg, your function name is still "onBefore". For an After BR, the function name should be " executeRule", could you try changing the function name and see if that works?