Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

thank you timi thats working fine and i have done similar changes only now its working fine .

now i have another qustion 

there is screen short captured in the answers , i wnt that to populate it on same RITM additional comments as a link so that whenever we click the link i want show a preview of that snippet to be viewed  how can we acheive this ,?

Hi @chaitanyakumarD 

You can leverage the GlideSysAttachment API to retrieve the associated attachments of the question.

Then you can build a reference link to open the attachment viewer.

Sample below.

Based on the script I provided before, let add an exception in the while loop.

    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();

		//If the question is in Attachment datatype
        if (assessmentQuestionGR.metric.datatype.toString() === 'attachment') {
            var attachments = [];
            var attachment = new GlideSysAttachment();
            var agr = attachment.getAttachments('asmt_metric_result', current.getUniqueValue()); //get all attachments
            while (agr.next()) {
				var attachmentID = agr.getUniqueValue();
				var attachmentName = agr.getValue('file_name');
				//build the url to open the document viewer
				var uri = '[code]<a href="sys_attachment.do?view=true&sys_id=' + attachmentID + '">' + attachmentName + '</a>[/code]';
                attachments.push(uri);
            }

			assessmentResponses.push({
                question: question,
                answer: attachments.join(',') //set answer
            });
			continue; //skip below lines and move to next question record in the loop
        }

        var answer = assessmentQuestionGR.string_value.toString(); //replace to string_value
        assessmentResponses.push({
            question: question,
            answer: answer
        });

    }

 

Enjoy your result

Timi_0-1705292747079.png

Timi_1-1705292755906.png

 

Make sure you have enabled Document Viewer in the table.

Ref: Enable Document Viewer

 

Cheers,

Tai Vu