Default answer from script in Assessments

dng
Kilo Contributor

We have a requirement to default some of the answers on an assessment based on values from the Assessable record.  I have been playing with an option in the Assessment Metric method called “Default answer from script” which would insinuate that I could default the value in a custom script.  However I put the code below in the script and some variations of it with no success.  The logs seem to be printing properly with the right values but i’m not seeing the Assessment answer default to what I’m trying to set it to.  I am using the "Yes/No" data type right now, but I have also tried it with String and Number with no success.  Has anyone had experience with this or has any ideas about another way to set the Assessment answer default value on load?

 
 
 
// The following variables are available: 
 // - primary: contains the sys_id of the assessable object to be evaluated 
 // - string_result: the script sets the display string value for this metric to this variable 
 // - actual_result: the script sets the actual value for this metric to this variable 
 // - scaled_result: the script sets the scaled value (used in calculations) for this metric to this variable 
 // 
 // For example:
 // var gr = new GlideRecord('cmdb_ci');
 // gr.addQuery('manufacturer', primary);
 // gr.query();
 // actual_result = gr.getRowCount();
 // string_result = actual_result + '';
 // if (actual_result > 100) scaled_result = 5;
 // else scaled_result = 1;
 
 
var grRvcRec = new GlideRecord('x_case_review');
grRvcRec.addQuery('sys_id', primary);
grRvcRec.query();
if(grRvcRec.next()) {
    var reviewType = grRvcRec.getValue('review_type');
    var cr = 'Complex Review';
    gs.info('review found! review_type=' + reviewType);
    if(cr.indexOf(reviewType != -1)) {
        actual_result = 1;
        string_result = 'Yes';
        scaled_result = 1;
        gs.info('review Found! actual_result=' + actual_result);
    }
}
 
7 REPLIES 7

Michael Fry1
Kilo Patron

You can use the Script method with these Data type values:

  • Duration
  • Number
  • Percentage

I don't believe there is a way to set a default value on a Yes/No questions

https://docs.servicenow.com/bundle/madrid-servicenow-platform/page/administer/assessments/concept/c_...

dng
Kilo Contributor

Thanks for the reply.  I did read this in the doc but i couldn't even get Number or Duration to work.  We just want to default the answer on the assessment using the script but I can never get a value populated in the answer text box upon opening the assessment.  Have you ever gotten this to work with the data types you mentioned above?

 

Pruthvi Talupul
Giga Contributor

Hi All,

The actual_result variable, that you are setting from the Script populates the field 'Actual Value' on 'Metric Result' form but not on the Assessment form where user fills the data.

 

Please refer the below docs for more details - 

https://docs.servicenow.com/bundle/paris-servicenow-platform/page/administer/assessments/concept/c_A...

 

for getting checkbox checked, set - 

string_result = 'true';

 

 

 

Let me know if it helped

Thanks!

Hi Pruthvi,

 

We have a requirement to prefill the assessment on 'Take Assessment' form with the last answers submitted on the business application (we are doing apm assessmnet). any idea how can we acheive this?

 

Thanks in advance