- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 05:15 AM
Hi All,
The query is, I need to restrict to add affected CIs to the change request if the change state is not in implement/review/cancelled/closed.
I have written the below script but the script is not working, the script is not entering into second if loop.
(function executeRule(current, previous /*null when async*/ ) {
var taskRef = current.task.getRefRecord();
var tableName = taskRef.getTableName();
var state = taskRef.state;
gs.info('Table Name: ' + '-->' + tableName);
if (tableName === 'change_request' || tableName === 'change_task') {
if (state === 500 || state === 600 || state === 900 || state === 1000) {
gs.info("Enter into second IF loop");
gs.addErrorMessage('The Change request is in ' + taskRef.state + " " + ', So cannot add affected CI');
current.setAbortAction(true);
}
return;
}
})(current, previous);
Thanks & Regards,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 06:37 AM
Glad to know.
Please close the thread by marking appropriate response as correct so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 05:42 AM
Hi @madhusagar ,
Try below code.
(function executeRule(current, previous /*null when async*/ ) {
var taskRef = current.task.getRefRecord();
var tableName = taskRef.getTableName();
var state = taskRef.getValue('state');
if (tableName === 'change_request') {
if (state == -1|| state == 0 || state == 3 || state == 4) {
gs.info("Entered into second IF loop");
gs.addErrorMessage('The Change request is in state ' + taskRef.state + ', so you cannot add an affected CI.');
current.setAbortAction(true); // Prevent further processing
}
}
})(current, previous);
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 05:47 AM
Hi @madhusagar
The state variable may is not an numeric value as you check it in the second if statement. And as you use "===" it also checks for the type. You may stringify the value?
Something like:
(function executeRule(current, previous /*null when async*/ ) {
var taskRef = current.task.getRefRecord();
var tableName = taskRef.getTableName();
var state = taskRef.getValue("state");
gs.info('Table Name: ' + '-->' + tableName);
if (tableName === 'change_request' || tableName === 'change_task') {
if (state === "500" || state === "600" || state === "900" || state === "1000") {
gs.info("Enter into second IF loop");
gs.addErrorMessage('The Change request is in ' + state + " " + ', So cannot add affected CI');
current.setAbortAction(true);
}
return;
}
})(current, previous);
Best regards
Dominik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 06:00 AM
Thanks Everyone i fix it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 06:37 AM
Glad to know.
Please close the thread by marking appropriate response as correct so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader