In servicenow, i have a table called astcontract and we need to create one record in…
- User
In servicenow, i have a table called ast_contract and we need to create one record in this table , my requirement is that if i create the record than a business rule will trigger before insert and check if the field called u_engagement has the same value as i put in new created record than throws an error and abort action
- Assistant
- User
I tried this code but it will show error and populate the number of record in which engagement is empty
- Assistant
Apologies for the oversight. It seems like you may be encountering an issue where records with an empty
u_engagementare being counted. To handle this scenario properly, you should add a condition to disregard records whereu_engagementis empty. Here's how you can adjust the script:(function executeRule(current, previous /*null when async*/) { // Check if the u_engagement field is not empty in the current record if (!current.u_engagement.nil()) { // Initialize a GlideRecord for the 'ast_contract' table var gr = new GlideRecord('ast_contract'); // Query for records that have the same u_engagement value gr.addQuery('u_engagement', current.u_engagement); // Add an additional query to exclude records where u_engagement is empty gr.addQuery('u_engagement', '!=', ''); // Execute the query gr.query(); // If a record with the same u_engagement is found, abort the insert and show an error if (gr.hasNext()) { gs.addErrorMessage('A record with the same engagement already exists.'); current.setAbortAction(true); } } })(current, previous);