Way to copy filed value to other table's record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 07:58 AM - edited 01-21-2024 07:59 AM
I want to copy filed value to other table's record, when record is updated.
I tried to it with the following business rule, but it did not working.
There is same user record with user_name in x_XXXXX_0_test_table.
When to run are: When is before and Update and Insert.
Can you help me to resolve this issue ?
(function executeRule(current, previous /*null when async*/ ) {
var value1 = current.value1.toString(); // this is string type field
var userid = current.user_name; //user_name is reference for sys_user
var gr = new GlideRecord('x_XXXXX_0_test_table');
gr.addQuery('test_table_user_name', userid ); // Find same user record with user_name in x_XXXXX_0_test_table
gr.query();
while (gr.next()) {
gr.test_table_value1 = value1.toString(); //Set value of value1 to test_table_value1.
gr.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 08:14 AM
Hi @matsui,
Your script is correct please check if this business rule is triggered by adding logs and try with after business rule to check if it works.
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 08:33 AM
Hello @matsui ,
Your business rule seems generally correct, To assist you better, let's add some logging statements to trace the script execution. Please try the modified script below:
(function executeRule(current, previous /*null when async*/ ) {
// Adding logging statements for better tracing
gs.log('Starting business rule execution...');
var value1 = current.value1.toString(); // this is a string type field
var userid = current.user_name.getDisplayValue(); // user_name is a reference for sys_user
// Adding logging statements
gs.log('Current value1: ' + value1);
gs.log('Current user_name: ' + userid);
var gr = new GlideRecord('x_XXXXX_0_test_table');
gr.addQuery('test_table_user_name', userid); // Find the same user record with user_name in x_XXXXX_0_test_table
gr.query();
while (gr.next()) {
gr.test_table_value1 = value1.toString(); // Set the value of value1 to test_table_value1.
gr.update();
}
// Update the current record after the loop
current.update();
// Adding logging statement
gs.log('Business rule execution completed.');
})(current, previous);
Please apply this script, run the business rule, and check the logs. The added logging statements will help us identify where the script is executing and if there are any unexpected issues. Let me know the results, and we can proceed to investigate further if needed.
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 09:02 PM
@Aniket Chavan
I could not find 'Starting business rule execution... in the first place, when this business rule is executed.
So, it means that did not trigger due to any reason...
This table is the extension source is Task table.
Should I act to trigger to this business rule ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 09:42 PM
Hello @matsui ,
If the business rule is not triggering as expected, it could be due to a few reasons. Here are some steps you can take to troubleshoot:
Check Business Rule Conditions: Ensure that the conditions set for the business rule are correct and match the scenarios where you want the business rule to execute. In your case, you mentioned that the trigger conditions are "Before" and "Update and Insert." Verify these conditions.
Check Field Changes: Business rules in ServiceNow often have conditions based on field changes. Ensure that the fields involved in the conditions are indeed changing when you expect the business rule to trigger.
Verify Extension Table: Since your source table is an extension of the Task table, make sure that the conditions in your business rule are applicable to the specific extension table. You might need to adjust the conditions to match the fields in your extension table.
Check Field Permissions: Ensure that the user executing the business rule has the necessary permissions to update records in the source table (Task table) and the target table (x_XXXXX_0_test_table).
Check System Logs: Review the system logs to see if there are any error messages or warnings related to the business rule execution. This can provide valuable information about why the business rule is not triggering.
Test with a Simple Condition: To simplify the troubleshooting process, you can temporarily modify the business rule to trigger on a very simple condition, such as always triggering on update, and see if it executes. If it does, you can gradually reintroduce your original conditions to identify where the issue might be.
Check Dependencies: If there are any dependencies in your business rule, such as scripts, scripts include, or other business rules, ensure that they are not causing any conflicts or preventing the execution of your business rule.
Check Record Audit: Enable record audit on the Task table or your extension table to track changes. This can help you verify if the record is being updated and if the conditions for the business rule are being met.