Two different Error messages need to be displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello all,
In service-now, on the incident form, there is a reference field, "Caused by Change" and it is referencing to Change request table. And similarly, on the change request form, in the related sections we have Incidents caused by change, where it has New and Edit buttons, and we can add Incidents by clicking the Edit button.
So, I got a requirement, like, when an incident is added to the Change request either by adding from the incident form or from the change request form by clicking the edit button and adding, and if the added incident is cancelled, then it should abort the action like it should not add that incident and the other scenario is if the incident that already added to the change request, later if gets cancelled, then the incident should be untagged from the change request.
So, I have written a before update business rule on Incident table and the conditions I have given is, State is cancelled, and below is the script:
current.setValue('caused_by','');
current.setValue('rfc','');
gs.addErrorMessage('You try to link an Incident which is in Cancelled state, this is not allowed. Incident must be an any other state than Cancelled when linking to Change record.');
So, from the change request form, when we are trying to add the already cancelled incidents, it is showing the error what I have written in addErrorMessage, But when the incident has already a change request on the caused by change, and later if the incident is cancelled, then the same error message is showing, which I don't want. On incident form it should show some other message like, "As the incident is cancelled, the corresponding CRs are cleared". So, tell me how can I do that?
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
try this before update BR
(function executeRule(current, previous /*null when async*/) {
var CANCELLED = 8; // change if your Incident cancelled state value is different
var isCancelled = current.state == CANCELLED;
var wasCancelled = previous.state == CANCELLED;
var rfcChanged = current.rfc != previous.rfc;
var causedByChanged = current.caused_by != previous.caused_by;
var linkChangedNow = rfcChanged || causedByChanged;
var linkAddedNow =
(!gs.nil(current.rfc) && gs.nil(previous.rfc)) ||
(!gs.nil(current.caused_by) && gs.nil(previous.caused_by));
var alreadyLinkedBeforeCancel =
(!gs.nil(previous.rfc) || !gs.nil(previous.caused_by));
if (!isCancelled)
return;
// Scenario 1: user is trying to link a cancelled incident to a change
if (linkChangedNow && linkAddedNow) {
gs.addErrorMessage('You cannot link an Incident in Cancelled state to a Change record.');
current.rfc = '';
current.caused_by = '';
current.setAbortAction(true);
return;
}
// Scenario 2: incident was linked earlier, and now it is being cancelled
if (!wasCancelled && alreadyLinkedBeforeCancel) {
current.rfc = '';
current.caused_by = '';
gs.addInfoMessage('As the Incident is cancelled, the corresponding Change link has been cleared.');
}
})(current, previous);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
40m ago
Hello Ankur,
Thanks for your response.
It's working.
But on the Incident form, when I cancel the state, it is redirecting to my business rule.
Can we stop redirecting and stay on the same page?
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
16m ago
update as this and see if it works
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader