write a servicenow business rule in javascript that aborts an update to the sys_user…
- User
write a servicenow business rule in javascript that aborts an update to the sys_user table if the email field is changed and is a duplicate of another record
- Assistant
Certainly! Here's an example of a ServiceNow business rule written in JavaScript that will abort an update to the sys_user table if the email field is changed and is a duplicate of another record:
(function executeRule(current, previous /*, g*/) { // Check if email field is changed if (current.email.changes()) { // Query for other records with the same email var gr = new GlideRecord('sys_user'); gr.addQuery('email', current.email); gr.query(); // Abort update if duplicate email found if (gr.hasNext()) { gs.addErrorMessage('Email already exists in another record. Update aborted.'); current.setAbortAction(true); } } })(current, previous);In this example, the business rule is triggered on the "update" operation for the sys_user table. It first checks if the email field is changed using the
changes()method. If it is, a GlideRecord query is performed to find any other records with the same email. If a duplicate email is found, an error message is added usinggs.addErrorMessage()and the update action is aborted by settingcurrent.setAbortAction(true).Make sure to replace
'sys_user'with the actual table name if you are using a different table.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.