how to display value of variable having different data types like number,string,images,date &time et

chaitanyakumarD
Tera Contributor
i triggered a survay once a RITM is closed and those answers i want to capture in additional comments but it is storing only backend values for answers . can any one  correctme in the code which i have written
 
(function executeRule(current, previous /*null when async*/ ) {
 
    var assessmentResponses = [];
    var assessmentQuestionGR = new GlideRecord('asmt_assessment_instance_question');
    assessmentQuestionGR.addQuery('instance', current.sys_id);
    assessmentQuestionGR.addQuery('valueISNOTEMPTY');
    assessmentQuestionGR.query();
    while (assessmentQuestionGR.next()) {
        var question = assessmentQuestionGR.metric.getDisplayValue(); // Use getDisplayValue() for reference fields
        var answer = assessmentQuestionGR.value.toString(); // Replace 'value' with the actual field name for the answer
 
        assessmentResponses.push({
            question: question,
            answer:  answer
        });
 
    }
    // Update RITM Additional Comments with assessment responses
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get('sys_id', current.task_id)) {
 
        var additionalComments = "\nAssessment Responses:\n";
        for (var i = 0; i < assessmentResponses.length; i++) {
            additionalComments += assessmentResponses[i].question + ': ' + assessmentResponses[i].answer + '\n';
        }
 
        ritm.comments += additionalComments;
        ritm.update();
    }
 
 
})(current, previous);
1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @chaitanyakumarD 

Typically, the Display value of the Metric (Question) is stored in the String Value field as per the Assessment Metric Definitions. But within the Assessment Instance Questions [asmt_assessment_instance_question] table currently used from your script, the String Value field is not fully populated.

 

You can try to pull data from the Metric Result [asmt_metric_result] table instead. This way, you can retrieve the answer along with the display value through the String Value field.

Timi_0-1704687818018.png

 

So your script could be like sample below.

(function executeRule(current, previous /*null when async*/ ) {
 
    var assessmentResponses = [];
    var assessmentQuestionGR = new GlideRecord('asmt_metric_result'); //replace the table
    assessmentQuestionGR.addQuery('instance', current.sys_id);
	assessmentQuestionGR.addNotNullQuery('actual_value'); //replace the field name
    //assessmentQuestionGR.addQuery('valueISNOTEMPTY');
    assessmentQuestionGR.query();
    while (assessmentQuestionGR.next()) {
        var question = assessmentQuestionGR.metric.getDisplayValue();
        var answer = assessmentQuestionGR.string_value.toString(); //replace to string_value
 
        assessmentResponses.push({
            question: question,
            answer:  answer
        });
 
    }
    // Update RITM Additional Comments with assessment responses
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get('sys_id', current.task_id)) {
 
        var additionalComments = "\nAssessment Responses:\n";
        for (var i = 0; i < assessmentResponses.length; i++) {
            additionalComments += assessmentResponses[i].question + ': ' + assessmentResponses[i].answer + '\n';
        }
 
        ritm.comments += additionalComments;
        ritm.update();
    }
 
 
})(current, previous);

 

Cheers,

Tai Vu

 

 

View solution in original post

6 REPLIES 6

Ivan Betev
Mega Sage
Mega Sage

Hello there,

 

some metrics like "Date", "DateTime", and "String" store their values in the String value (string_value) field, you can try grabbing that one.

As for the ones that have a "Metric Definition" table behind them, you can try something like this:

 

 

(function executeRule(current, previous /*null when async*/) {
       
    var assessmentResponses = [];

    var assessmentQuestionGR = new GlideRecord('asmt_assessment_instance_question');
    assessmentQuestionGR.addQuery('instance', current.sys_id);
    assessmentQuestionGR.addQuery('valueISNOTEMPTY');
    assessmentQuestionGR.query();

    while (assessmentQuestionGR.next()) {
        var question = assessmentQuestionGR.metric.getDisplayValue(); // Use getDisplayValue() for reference fields

        var metricDefGr = new GlideRecord('asmt_metric_definition');
        metricDefGr.addQuery('metric', assessmentQuestionGR.metric);
        metricDefGr.addQuery('value', assessmentQuestionGR.value);
        metricDefGr.query();

        var answer = metricDefGr.next() ? metricDefGr.display : "";

        assessmentResponses.push({
            question: question,
            answer: answer
        });

    }
    // Update RITM Additional Comments with assessment responses
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get('sys_id', current.task_id)) {

        var additionalComments = "\nAssessment Responses:\n";
        for (var i = 0; i < assessmentResponses.length; i++) {
            additionalComments += assessmentResponses[i].question + ': ' + assessmentResponses[i].answer + '\n';
        }

        ritm.comments += additionalComments;
        ritm.update();
    }

})(current, previous);

 

 

BUT, you need to get rid of the nested call first (e.g. by using a prefetch), as it's a bad practice for performance.

 

Regards, Ivan

Tai Vu
Kilo Patron
Kilo Patron

Hi @chaitanyakumarD 

Typically, the Display value of the Metric (Question) is stored in the String Value field as per the Assessment Metric Definitions. But within the Assessment Instance Questions [asmt_assessment_instance_question] table currently used from your script, the String Value field is not fully populated.

 

You can try to pull data from the Metric Result [asmt_metric_result] table instead. This way, you can retrieve the answer along with the display value through the String Value field.

Timi_0-1704687818018.png

 

So your script could be like sample below.

(function executeRule(current, previous /*null when async*/ ) {
 
    var assessmentResponses = [];
    var assessmentQuestionGR = new GlideRecord('asmt_metric_result'); //replace the table
    assessmentQuestionGR.addQuery('instance', current.sys_id);
	assessmentQuestionGR.addNotNullQuery('actual_value'); //replace the field name
    //assessmentQuestionGR.addQuery('valueISNOTEMPTY');
    assessmentQuestionGR.query();
    while (assessmentQuestionGR.next()) {
        var question = assessmentQuestionGR.metric.getDisplayValue();
        var answer = assessmentQuestionGR.string_value.toString(); //replace to string_value
 
        assessmentResponses.push({
            question: question,
            answer:  answer
        });
 
    }
    // Update RITM Additional Comments with assessment responses
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get('sys_id', current.task_id)) {
 
        var additionalComments = "\nAssessment Responses:\n";
        for (var i = 0; i < assessmentResponses.length; i++) {
            additionalComments += assessmentResponses[i].question + ': ' + assessmentResponses[i].answer + '\n';
        }
 
        ritm.comments += additionalComments;
        ritm.update();
    }
 
 
})(current, previous);

 

Cheers,

Tai Vu

 

 

hi I have tried that also but it is not populating the results as desired even they are not reference fields also 

thankyou for your response

Hi @chaitanyakumarD 

For better understanding of the specific challenges you're encountering, can you provide more information in your business rule? Table, when to run, your advanced script, etc.

 

Cheers,

Tai Vu