- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @IreneBest
Your are using setAbortAction(). Then why you have added "return;"
Remove Return; line
(function executeRule(current, previous /*null when async*/) { var parentCase = new GlideRecord('sn_customerservice_case'); if (parentCase.get(current.parent)) { var mrvsData = parentCase.variables.approved_quantities_backend; if (gs.nil(mrvsData) || mrvsData == '[]' || mrvsData == '') { gs.addErrorMessage('You cannot close this task. The Approved Quantities on the Parent must be completed.'); current.setAbortAction(true); } } })(current, previous);
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks for the quick reply.
Unfortunately this did not fix the issue, only made it appear that the task was closed when it wasn't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @IreneBest
Try this script with Before Update BR
condition: State changes to Closed
Sample script:
(function executeRule(current, previous /*null when async*/) {
var parentCase = new GlideRecord('sn_customerservice_case');
if (parentCase.get(current.parent)) {
var mrvsJson = parentCase.variables.approved_quantities_backend;
var isEmpty = true;
if (mrvsJson) {
var parsedData = JSON.parse(mrvsJson);
if (parsedData.length > 0) {
isEmpty = false;
}
}
if (isEmpty) {
gs.addErrorMessage(‘You cannot close this task. The Approved Quantities on the Parent must be completed.');
current.setAbortAction(true);
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago