We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Business rule not working to prevent serial number in a request form

NayeemSk
Tera Contributor

I create a business rule, which needs to prevent a serial number in a request that already exists in a table. Business Rule is not working. Attached the screenshot.

10 REPLIES 10

Ankur Bawiskar
Tera Patron

@NayeemSk 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

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 )"

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!

@OlaN 

yes it does happen sometimes it converts and sometimes it doesn't

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

mohitbhadan
Tera Contributor

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