Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Thank you for sharing, still no success.

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();
        }

    }
}