Business rule not working to prevent serial number in a request form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
2 issues in your script
-> missing query()
-> you should use dot but you used comma in the next() method
-> use setLimit(1) to query only 1 record
-> use hasNext() and not next()
(function executeRule(current, previous /*null when async*/ ) {
var invGr = new GlideRecord("x_1747006_device_0_device_inventory");
invGr.addQuery("serial_number", current.serial_number);
invGr.addQuery("sys_id", ' != ', current.sys_id);
invGr.setLimit(1);
invGr.query();
if(invGr.hasNext()) {
gs.addErrorMessage("This serial number already exists in the database");
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Sorry for more or less duplicating your answer, I didn't see it until after I posted.
One thing that made me curious when I saw your answer.
You did get to make the codeexample with the setLimit work.
Whenever I typed it into the post it got transformed into a special character like so -- >> (1)
How did you do to remain the original text? My workaround was to insert a space.. not pretty. "(1 )"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hahaha! I saw now that it only showed the conversion when editing.
When the text was posted, it changed back into the actual text.
How annoying!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
yes it does happen sometimes it converts and sometimes it doesn't
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @NayeemSk,
At line 7, use the dot operator:
if (invGr.next()) {
// your code
}
Please mark my answer as correct and helpful if this works for you.
Thanks & Regards,
Mohit