Validate parent case mrvs before closing child task

IreneBest
Tera Contributor

I have a case (sn_customerservice_case) which has a child task that I don't want to close until a mrvs on the parent is not empty.

I have created a Before Update Business Rule and while this appears to validate correctly if I attempt to close the child task with an empty mrvs, it also does not recognise when the mrvs is not empty and issues an error.

The code is:

(function executeRule(current, previous /*null when async*/) {
   
    var parentCase = new GlideRecord('sn_customerservice_case'); // Adjust table name if needed
    if (parentCase.get(current.parent)) {
       
        var mrvsData = parentCase.variables.approved_quantities_backend;
       
        if (gs.nil(mrvsData) || mrvsData == '[]') {
            gs.addErrorMessage('You cannot close this task. The Approved Quantities on the Parent must be completed.');
            current.setAbortAction(true); // Aborts the save/close action
            return;
        }

    }

})(current, previous);
 
Any help would be appreciated.
1 ACCEPTED SOLUTION

pr8172510
Tera Guru

Hi @IreneBest 
Use this 
Before Update Business Rule:

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

    // Get parent case
    var parentCase = new GlideRecord('sn_customerservice_case');
    if (!parentCase.get(current.parent)) {
        return;
    }

    // Get MRVS value (it comes as JSON string)
    var mrvsValue = parentCase.variables.approved_quantities_backend + ''; // Force to string

    var isEmpty = true;

    // Check if MRVS has valid data
    if (mrvsValue && mrvsValue != '[]' && mrvsValue != '') {
        try {
            var parsed = JSON.parse(mrvsValue);
            if (parsed && parsed.length > 0) {
                isEmpty = false;
            }
        } catch(e) {
            // Invalid JSON - treat as empty
            isEmpty = true;
        }
    }

    // Block closure if MRVS is empty
    if (isEmpty) {
        gs.addErrorMessage('You cannot close this task. The Approved Quantities on the Parent must be completed.');
        current.setAbortAction(true);
    }

})(current, previous);

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron

@IreneBest 

this should work fine

(function executeRule(current, previous) {

    // Run only when closing the child task
    if (!current.state.changesTo('3'))  // replace 3 with your actual closed state value
        return;

    var parentCase = new GlideRecord('sn_customerservice_case');
    if (!parentCase.get(current.parent))
        return;

   var mrvs = parentCase.variables.approved_quantities_backend + '';
if (!mrvs || mrvs == '[]' || JSON.parse(mrvs).length === 0) {
    gs.addErrorMessage('You cannot close this task. The Approved Quantities on the Parent must be completed.');
    current.setAbortAction(true);
}

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

IreneBest
Tera Contributor

@Ankur Bawiskar 

As the When to run already checks for State changes to Closed, why is this required in the script?

Thanks

@IreneBest 

yes not required if your business rule has that condition already i.e. State Changes to Closed

Remove that and keep other script as it is

💡 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

pr8172510
Tera Guru

Hi @IreneBest 
Use this 
Before Update Business Rule:

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

    // Get parent case
    var parentCase = new GlideRecord('sn_customerservice_case');
    if (!parentCase.get(current.parent)) {
        return;
    }

    // Get MRVS value (it comes as JSON string)
    var mrvsValue = parentCase.variables.approved_quantities_backend + ''; // Force to string

    var isEmpty = true;

    // Check if MRVS has valid data
    if (mrvsValue && mrvsValue != '[]' && mrvsValue != '') {
        try {
            var parsed = JSON.parse(mrvsValue);
            if (parsed && parsed.length > 0) {
                isEmpty = false;
            }
        } catch(e) {
            // Invalid JSON - treat as empty
            isEmpty = true;
        }
    }

    // Block closure if MRVS is empty
    if (isEmpty) {
        gs.addErrorMessage('You cannot close this task. The Approved Quantities on the Parent must be completed.');
        current.setAbortAction(true);
    }

})(current, previous);

Thank you.  This worked.