The CreatorCon Call for Content is officially open! Get started here.

Populate original value of Assessment Instance Question?

SandyL83
Tera Guru

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..

SandyL83_0-1680789984677.png

 

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,

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

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".

View solution in original post

10 REPLIES 10

Bert_c1
Kilo Patron

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?

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:

(function executeRule(current, previous /*null when async*/) {

 

    current.original_value = current.value; 
    current.update();
})(current, previous);
 
 
This did in fact populate the Original Value, but it also caused the Original Value to change when Value was changed. 
So, I went back and unchecked "Update" and just insert, using the same code above. And now Original Value isn't being populated at all. 
I have tried it Before and After, with and without current.update, and I am not sure why Original Value is only being set when I have not insert and update selected. Hopefully that makes some sense. 

 

 

Bert_c1
Kilo Patron

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.

SandyL83
Tera Guru

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: 

(function executeRule(current, previous /*null when async*/) {

gs.addInfoMessage("myBusinessRule: is running, current.original_value = " + current.original_value + ", current.value = " + current.value);
    current.original_value = current.value; 
    //current.insert();
})(current, previous);
 
this is what happens after I create the demand...and i think it explains why it's not populating the original value.. at the time the business rule works, apparently the assessment instance question record is already created (and the user has not taken the assessment yet).  I am going to mess around with the conditions and see if I do something like "instance state = complete".... and see if that will populate.
If you have any other ideas, let me know. Thanks so much again. 

 

SandyL83_0-1681132321816.png

 

Bert_c1
Kilo Patron

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.