- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 07:01 AM
Hello everyone.
I've created a catalog item and created an After - Business Rule, to save the RITM and variables in a custom table.
The code below creates a record in my custom table, but it's not the correct record. In the sc_req_item, this business rule creates two records, one is the correct one and the other is something like a copy. The second one is the record that is being saved in my custom table.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 12:38 AM
Hi @Alex-Lima84 ,
There's no such field called "u_business_rule_executed" in any OOB tables, As I mentioned earlier, this is new field/flag we need create (if you haven't already created) to ensure that the Business Rule runs only once for a given record.
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 07:07 AM - edited 12-06-2023 07:10 AM
Hi @Alex-Lima84 ,
You can make a few adjustments to your Business Rule:
(function executeRule(current, previous /*null when async*/ ) {
try {
// Check if the Business Rule has already run for this record
if (current.u_business_rule_executed) {
return;
}
var variables = current.variables;
var customTable = new GlideRecord('x_976040_app_pro_application_process');
customTable.addQuery('number', current.number);
customTable.query();
if (!customTable.next()) {
customTable.initialize();
customTable.number = current.number;
customTable.full_name = variables.app_full_name;
customTable.zip_code = variables.app_zip_code;
customTable.address = variables.app_address;
customTable.address_number = variables.app_address_number;
customTable.neighborhood = variables.app_address_neighborhood;
customTable.city = variables.app_address_city;
customTable.address_state = variables.app_address_state;
customTable.e_mail = variables.app_email;
customTable.contact_number = variables.app_number;
customTable.introduction_info = variables.app_introduction_text;
customTable.resume = variables.app_upload_resume;
customTable.insert();
}
// Mark the record to indicate that the Business Rule has been executed
current.u_business_rule_executed = true;
current.setWorkflow(false); // Prevent the workflow from running again
current.update();
} catch (ex) {
gs.error('An error occurred in the business rule: ' + ex);
throw ex;
}
})(current, previous);
This code introduces a check (u_business_rule_executed
) to ensure that the Business Rule runs only once for a given record. Additionally, it disables the workflow to prevent the rule from being triggered multiple times.
Please replace u_business_rule_executed
with an appropriate field name in your sc_req_item
table.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 07:16 AM
@Ratnakar7 Thank you very much for your reply.
When you say replace u_business_rule_executed with an appropriate field name in your sc_req_item table, could you help me with this? Can this be any field I have in the sc_req_item table? Like the number field?
Sorry, I'm relatively new to ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 12:38 AM
Hi @Alex-Lima84 ,
There's no such field called "u_business_rule_executed" in any OOB tables, As I mentioned earlier, this is new field/flag we need create (if you haven't already created) to ensure that the Business Rule runs only once for a given record.
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thanks,
Ratnakar