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
Here's an example of a ServiceNow Business Rule written in JavaScript that aborts an update to the
sys_usertable if the email field is changed and is a duplicate of another record:(function() { // Only run this business rule on update if (current.operation !== 'update') { return; } // Get the old and new values of the email field var oldEmail = current.getOldValue('email'); var newEmail = current.email; // Check if the email field has changed if (oldEmail !== newEmail) { // Query the sys_user table for any records with the new email address var gr = new GlideRecord('sys_user'); gr.addQuery('email', newEmail); gr.addQuery('sys_id', '!=', current.sys_id); // Exclude the current record gr.query(); // If a duplicate record is found, abort the update and display an error message if (gr.next()) { current.setAbortAction(true); current.setWorkflow(false); gs.addInfoMessage('Error: The email address ' + newEmail + ' is already in use by another user. Please choose a different email address.'); } } })();Note: This is just an example and may need to be modified to fit your specific use case. Additionally, this business rule should be added to the
sys_usertable and configured to run when a record is updated.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.