- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 07:06 AM
Hello,
I have updated our Assessments to that the asmt_assessment_instance_question value can be updated.
I am looking to store the original value that was present when the assessment was submitted.
I thought I had it working yesterday, then we cloned down our instance and my business rule was lost. Now I redid it and it's not working. here is the business rule I am trying to use..
Here is the code:
(function executeRule(current, previous /*null when async*/) {
current.original_value = previous.value;
})(current, previous);
What am I missing?
there is a column titled "original_value", and the field the user is able to update is "value"...
Any ideas?
Thanks,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 07:55 AM
Hi,
It appears that your business rule runs "After" and there is no "current.update();" (which is to be avoided in an After BR). Set your BR to run "Before".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 11:20 AM
Hi @SandyL83 ,
first, see:
https://docs.servicenow.com/search?q=How%20business%20rules%20work
There is no need for 'current.update()' in a Before BR. If you must run what you last posted in an After BR, please add "current.setWorkflow(false);" This prevents BRs from running on your update(). The Best Practice for this is a Before BR, change record values after the user saves and before the record is written to the database.
If you are still having problems, there must be something else in your way.
You stated earlier you found what works, why change from that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 11:51 AM
Hello , thanks again. I am not sure if I explained it correctly. I accidentally had both Insert and Update checked, as well as After.
So it was:
After, Before, Update with this code:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 12:11 PM
Hello @SandyL83
Please spend some time reviewing the documentation links I posted.
And no, what you posted doesn't make sense to me. I can't determine the table your BR is defined on or the condition present in your screen shot. Seems the table is 'asmt_instance_question' and the condition involves the 'dmn_demand' table. Remove that condition and add the same logic in the script. Add:
gs.addInfoMessage("myBusinessRule: is running, current.original_value = " + current.original_value + ", current.value = " + current.value);
at the top of the function and see what values are there.
you can also learn the Script Debugger for this.
If you will provide that info textual, I'll add a 'u_original_value' to the table in my instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 06:18 AM
Hello @Bert_c1 ! Thanks for the feedback. I definitely spent time reviewing the link you sent.
So I believe the issue is - based on the debugging script you sent, the record is being created at the time the demand is being created...
So, the business rule is running on the asmt_assessment_instance_question table.
I had a filter condition on the br that said only run when source table is dmn_demand, because this only applies to our Demand records.
I removed that condition to test, and nothing changed. So currently the setup is: When to run: Before, Insert.
The script is:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 07:50 AM
Hello @SandyL83 ,
I don't have a field named 'original_value' defined on the 'asmt_assessment_instance_question' table in my instance. I see that as 'admin' I can not add records to that table from the UI. I can add records in that table using a script, to test. And my BR like you describe runs. My BR script follows:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
gs.addInfoMessage("New assessment question for source_table: " + current.source_table + ", value = " + current.value);
gs.info("New assessment question for source_table: " + current.source_table + ", value = " + current.value);
})(current, previous);
The script I use to create a new record:
var aaiq = new GlideRecord('asmt_assessment_instance_question');
aaiq.initialize();
aaiq.source_table='dmn_demand';
aaiq.value = 4;
var newRecord = aaiq.insert();
gs.info("Add new record result = " + newRecord);
/*
Results in:
Background message, type:info, message: New assessment question for source_table: dmn_demand, value = 4
*** Script: New assessment question for source_table: dmn_demand, value = 4
*** Script: Add new record result = 459f28f6970221101dd3fa67f053af5d
*/
And I have the BR condition defined as "Source table", "is", "dmn_demand".
I have no idea why your are not successful. I am not familiar with the "Assessments" feature, there is documentation on that. You may want to check for ACLs on the 'asmt_assessment_instance_question' table and the field you have 'original_value'. I hope you do get success with your goals.