Pass Assessment Questions Dynamically

Utkarsha
Tera Contributor

Hello All,

I was working on configuring an assessment for our client...and I got a ask if it's possible to push assessment instance questions dynamically ...if we submit a assessment record by using assessment template, a record gets inserted into the assessment instance table,and user has to take the assessment in ready to take stage...as per the current scenario, all the questions are being sent to the user whichever are associated with the assessment...I want to figure out a way if I can pass only those questions which he selects on the assessment form..FYI assessment instance questions are nothing but the (metric_categories)

All kinds of suggestions are welcome here,

Many thanks,

3 REPLIES 3

Aravind2799
Giga Expert

Hey @Utkarsha  

Please find the recommended solution 

  1. Create a Workflow:

    • Trigger the workflow on "assessment.after" insert event (when a new assessment instance is created).
    • Add a "Script" activity to the workflow.
  2. Develop the Script:

 

// Get the assessment instance record (current)
var assessmentRecord = current;

// Access the Assessment Template record (parent)
var templateRecord = assessmentRecord.assessment_template.getRecord();

// Retrieve all metric categories associated with the template
var grMetricCategories = new GlideRecord('assessment_metric_category');
grMetricCategories.addQuery('assessment_template', templateRecord.sys_id);
grMetricCategories.query();

// Initialize an empty array to store selected metric category sys_ids
var selectedMetricCategories = [];

// Logic to determine selected metric categories based on user form selections
// (Replace with your specific logic using current.variables or other data sources)
// Example: Check for a checkbox variable named 'selected_categories'
if (current.variables.selected_categories) {
  var selectedCategoriesString = current.variables.selected_categories.toString();
  selectedMetricCategories = selectedCategoriesString.split(','); // Split CSV string
}

// Iterate through retrieved metric categories
while (grMetricCategories.next()) {
  var metricCategoryRecord = grMetricCategories.getRecord();

  // Check if the current metric category is selected
  if (selectedMetricCategories.indexOf(metricCategoryRecord.sys_id) !== -1) {
    // Add the selected metric category to the assessment instance
    var assessmentInstanceMetricCategory = new GlideRecord('assessment_instance_metric_category');
    assessmentInstanceMetricCategory.initialize();
    assessmentInstanceMetricCategory.assessment_instance = assessmentRecord.sys_id;
    assessmentInstanceMetricCategory.metric_category = metricCategoryRecord.sys_id;
    assessmentInstanceMetricCategory.insert();
  }
}

// (Optional) Update the assessment instance stage to "Ready to Take"
// assessmentRecord.stage = 'ready_to_take';
// assessmentRecord.update();

 

Explanation of the Script:

  1. It retrieves the assessment instance record, template record, and all metric categories associated with the template.
  2. It initializes an array to store selected metric category sys_ids.
  3. Replace the placeholder logic with your actual code to determine selected metric categories based on user form selections (e.g., checkbox variable, radio buttons, etc.).
  4. It iterates through all metric categories and checks if they are included in the selectedMetricCategories array.
  5. For each selected metric category, it creates a new assessment_instance_metric_category record linking the assessment instance and the selected category.
  6. The commented-out section (optional) can be used to update the assessment instance stage to "Ready to Take" after filtering questions.

Key Points:

  • Replace the placeholder logic for determining selected metric categories with your specific assessment form implementation.
  • Consider using a GlideAggregate or a custom UI component (if feasible) for a more streamlined selection process.
  • Thoroughly test the workflow and script to ensure questions are filtered correctly.

By implementing this workflow and script combination, you can dynamically control the assessment instance questions displayed to users based on their form selections, creating a more tailored and efficient assessment experience.

 

Thanks

Aravind Panchanathan

Hi @Aravind2799 ,

Thank you so much for your inputs on this 🙂

One thing...I couldn't find this event in my instance

Do I need to figure out first which event is responsible for creating assessment instance or I can create new one?

Also in the script, you have mentioned the table name 'assessment_instance_metric_category'...Is this asmt_metric_category table?

If you can confirm on this, 

Thank you 

Hey @Utkarsha  

  • You're right, the "assessment.after" insert event might not be readily available in your ServiceNow instance. This event could be a custom one created by your organization or a specific plugin.

Here's how you can determine the relevant event:

  1. Navigate to System Definition > Workflows.
  2. Search for existing workflows related to assessments. Look for workflows with names like "Assessment Create" or "Assessment Instance Create."
  3. Review the workflow triggers. These triggers define the events that activate the workflow. Check if any of these triggers align with creating assessment instances (e.g., "assessment.insert," "assessment_instance.insert").
  4. If you can't find an existing workflow: You might need to create a new one with the appropriate event trigger (e.g., "assessment_instance.insert").

Table Name Confirmation:

  • Yes, in the script, "assessment_instance_metric_category" is likely the correct table name for linking assessment instances to metric categories (questions). However, it depends on your specific configuration.

Here's how to verify:

  1. Navigate to System Definition > Tables.
  2. Search for tables related to assessments. Look for tables with names like "assessment_instance_metric_category" or "asmt_metric_category."
  3. Review the table fields. If the table has fields for "assessment_instance" and "metric_category" (or similar names), then it's the correct table. Otherwise, you might need to adjust the script to use the actual table name from your instance.

Thanks
Aravind Panchanathan