← All articles
◈ Tech & ServiceNowServiceNowScripting

How to Generate an Assessment Using AssessmentCreation API

Posted on 23 Oct 2021 · 1 min read

In some scenarios, we want to generate on-demand assessments — triggered from a Service Portal, a UI Action, or a Business Rule.

ServiceNow's AssessmentCreation API provides a way to generate assessments via script. In this article, I am using the createAssessments method to generate assessments via a Service Portal Widget.

Method Signature

AssessmentCreation.createAssessments(
  String typeID,        // Assessment Metric Type Sys ID
  String sourceRecordID, // Assessable record (source of the assessment)
  String userID         // User ID to send the assessment instance to
)

Setup

Before writing the script, I prepared two things:

  1. I created a ServiceNow Portal service in the cmdb_ci_service table to act as the source record. Its Sys ID is 4baec2fe07d3f0101263f19d7c1ed0a6.
  2. I created an Assessment Type named "ServiceNow Portal Assessment" configured to trigger on demand. Its Metric Type Sys ID is d902e09a075730101263f19d7c1ed03a.

Triggering the Assessment from a Widget

I decided to trigger the assessment randomly for whoever visits the Service Portal home page on day 23 of any month. I created a widget and added it to my portal home page with the following server script:

(function() {

    var gdtSurvey = new GlideDateTime();
    if (gdtSurvey.getDayOfMonthUTC() == 23) {
        (new SNC.AssessmentCreation()).createAssessments(
            'd902e09a075730101263f19d7c1ed03a',
            '4baec2fe07d3f0101263f19d7c1ed0a6',
            gs.getUserID()
        );
        data.uSelected = 'You have been selected to take the Service Portal Assessment';
    }

})();

When the condition is met, an assessment instance is created and assigned to the visiting user. This pattern is flexible — you can swap out the trigger condition to suit any business logic, such as after a case is resolved or following an interaction closure.