logo

NJP

Survey/Assessment hell

Import · Nov 06, 2019 · article

There may be another way around this problem, but I had considerable grief trying to set up survey assessments for specific records in the contracts module. After trawling through the docs and trying to manually generate assessments and manually send, I still couldn't get the damn thing to work and kept getting the "noquestions" error, so I came up with a script that ensures surveys/assessments are sent.

Step 1: Set up your survey or assessment metric type as normal

Make sure you have your questions, etc in the related list for Metric Categories but don't schedule the survey. Leave it set to on demand and we'll set up our own scheduled job.

image

Step 2: Run a scheduled job to cycle through your accessible records

Strictly speaking, this shouldn't be necessary, but nothing was sending overnight so I wrote this to make sure all the component parts were in play.

var whichAssessment = '2349ed57dbbc4c103ea3cd0514961909';//<<<--- update with the sys_id of your assessment metric type
var thisAssessment  = new GlideRecord('asmt_metric_type');
if(thisAssessment.get(whichAssessment)){

    ////////////////////////////////////////////////////////////////////////
    //create the assessible records
    (new SNC.AssessmentCreation()).createAssessableRecords(thisAssessment);

    ////////////////////////////////////////////////////////////////////////
    //check to make sure they have questions attached
    var categories = [];
    var getCategories = new GlideRecord('asmt_metric_category');
    getCategories.addEncodedQuery('metric_type.sys_id=' + whichAssessment );
    getCategories.query();
    while(getCategories.next()){
        categories.push(getCategories.sys_id.toString());
    }

    //To avoid the noquestions error, look at all the assessible records and make sure questions are attached
    var checkAssessableRecords = new GlideRecord('asmt_assessable_record');
    checkAssessableRecords.addEncodedQuery('metric_type.sys_id=' + whichAssessment);
    checkAssessableRecords.query();
    while(checkAssessableRecords.next()){

        var assessibleCategories = new GlideRecord('asmt_m2m_category_assessment');
        if(!assessibleCategories.get('assessment_record.sys_id',checkAssessableRecords.sys_id)){
            categories.forEach(function(assessCategory){
                var newAssessCategory = new GlideRecord('asmt_m2m_category_assessment');
                newAssessCategory.initalize();
                newAssessCategory.assessment_record = checkAssessableRecords.sys_id;
                newAssessCategory.category = assessCategory;
                newAssessCategory.insert();
            });
        }
    }

    ////////////////////////////////////////////////////////////////////////
    //create and send the assessments
    var thisRecord = new GlideRecord(thisAssessment.table);
    thisRecord.addEncodedQuery(thisAssessment.condition);
    thisRecord.query();

    while(thisRecord.next()){
        (new SNC.AssessmentCreation()).createAssessments(whichAssessment, thisRecord.sys_id.toString(), thisRecord.contract_administrator.sys_id.toString());
    }
}

You can find the documentation for AssessmentCreation and functions such as createAssessments and createAssessibleRecords on the ServiceNow Developer API

Have fun

View original source

https://www.servicenow.com/community/now-platform-articles/survey-assessment-hell/ta-p/2325821