Assessment instance creation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago - last edited an hour ago
Hi All,
I am working on triggering a manual survey via a “Send Survey” button. I have created a UI Action and UI Page, and I am calling a Script Include from the UI Page.
In the Script Include, I am using the below API to create assessment instances:
var result = new SNC.AssessmentCreation().createAssessments(
'3208756633448750371cbf173e5c7b95',
'',
userSysId
);When I pass an empty string ('') as the second parameter, the assessment instance gets created successfully.
• However, when I pass the incident sys_id as the second parameter:
var result = new SNC.AssessmentCreation().createAssessments(
'3208756633448750371cbf173e5c7b95',
incidentSysId,
userSysId
);I receive “noquestions” as the result.
Requirement:
• I need to generate surveys manually (not via trigger condition).
• The survey should be linked to the Incident record for reporting purposes.
• I also need task and trigger_id to be populated in the assessment instance.
⸻
Issue:
• Using createAssessments():
• Assessment instance is created
• But task and trigger_id are not populated
• Passing incident sys_id causes “noquestions” error.
Giving whole script below.
var PostIncidentSurvey = Class.create();
PostIncidentSurvey.prototype = Object.extendsObject(AbstractAjaxProcessor, {
sendSurveyEmail: function() {
var emails = this.getParameter('sysparm_emails');
var incidentSysId = this.getParameter('sysparm_sys_id');
if (!emails || !incidentSysId)
return "Missing inputs";
var inc = new GlideRecord('incident');
if (!inc.get(incidentSysId))
return "Invalid Incident";
gs.info('assessment incidentSysIdd ' + incidentSysId);
var emailArray = emails.split(',');
for (var i = 0; i < emailArray.length; i++) {
var email = emailArray[i].trim();
if (!email)
continue;
try {
// Get user
var userSysId = '';
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', email);
userGr.query();
if (userGr.next())
userSysId = userGr.sys_id;
if (!userSysId) {
gs.error("User not found for email: " + email);
continue;
}
// CREATE ASSESSMENT (OOTB way)
var result = new SNC.AssessmentCreation()
.createAssessments(
'3208756633448750371cbf173e5c7b95',
"",
userSysId
);
gs.info('assessment result ' + result);
if (result && result != 'noquestions') {
var instanceSysId = result.split(',')[0];
// (Optional) attach task manually if needed
var inst = new GlideRecord('asmt_assessment_instance');
if (inst.get(instanceSysId)) {
inst.task = incidentSysId;
inst.trigger_id = incidentSysId;
inst.update();
}
}
if (!result) {
gs.error("Assessment creation failed for: " + email);
continue;
}
// Extract instance sys_id
var instanceSysId = result.split(',')[0];
// Generate URL
var url = new global.AssessmentUtils()
.getAssessmentInstanceURL(instanceSysId);
// Fire event
gs.eventQueue(
'manual.send_survey',
inc,
email,
url
);
} catch (ex) {
gs.error("Survey send failed for email: " + email + " Error: " + ex.message);
}
}
return "success";
},
type: 'PostIncidentSurvey'
});
Any guidance and best practice would be really helpful.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a minute ago
The SNC.AssessmentCreation().createAssessments(metricTypeSysId, sourceId, userSysId) API behaves differently based on that second parameter. When you pass the incident sys_id as the source, the system evaluates all question conditions defined on the metric type's questions against that incident record. If any of those conditions don't match the specific incident (or if the metric type's Table field doesn't align with the record you're passing), the API determines there are no applicable questions and returns "noquestions".
To diagnose this, check two things on your metric type (asmt_metric_type) record with sys_id 3208756633448750371cbf173e5c7b95:
- Confirm the Table field is set to
incident. - Open the related questions under that metric type (
asmt_metricrecords) and check if any have a Condition field set. Those conditions are evaluated against the source record you pass in. If the incident doesn't satisfy those conditions, all questions get filtered out and you get"noquestions".
If you remove or broaden those question-level conditions, passing the incident sys_id as the second parameter should work and the task field will get populated automatically by the platform.
Issue with trigger_id Assignment
There's an important correction needed in your code. The trigger_id field on asmt_assessment_instance is a reference to the asmt_metric_type_condition table — it points to the trigger condition record that caused the survey to be generated, not to the incident. Setting it to the incident sys_id won't give you meaningful results and could cause issues in reporting. If you're generating surveys manually (no trigger condition involved), it's acceptable to leave trigger_id empty.
Bug in Your Script — Duplicate Variable Declaration
You're declaring instanceSysId twice: once inside the if (result && result != 'noquestions') block and again outside it. The second declaration will execute even when result is "noquestions", which would cause an error on result.split(','). Here's the corrected flow:
var result = new SNC.AssessmentCreation().createAssessments(
'3208756633448750371cbf173e5c7b95',
incidentSysId, // pass incident if conditions are fixed
userSysId
);
gs.info('assessment result: ' + result);
if (!result || result == 'noquestions') {
gs.error("Assessment creation failed for: " + email + " Result: " + result);
continue;
}
var instanceSysId = result.split(',')[0];
// Generate URL and fire event
var url = new global.AssessmentUtils().getAssessmentInstanceURL(instanceSysId);
gs.eventQueue('manual.send_survey', inc, email, url);
If you've confirmed the metric type table is incident and removed restrictive question conditions, passing the incident sys_id as the second parameter should automatically populate the task field on asmt_assessment_instance. No manual GlideRecord update needed.
If You Must Keep Question Conditions (Fallback Approach)
If you have a legitimate need for question conditions that conflict with passing the source record, then your original workaround of passing an empty string and updating task manually is reasonable — just fix the duplicate variable and drop the trigger_id assignment:
var result = new SNC.AssessmentCreation().createAssessments(
'3208756633448750371cbf173e5c7b95',
'',
userSysId
);
if (!result || result == 'noquestions') {
gs.error("Assessment creation failed for: " + email);
continue;
}
var instanceSysId = result.split(',')[0];
// Link to incident manually
var inst = new GlideRecord('asmt_assessment_instance');
if (inst.get(instanceSysId)) {
inst.setValue('task', incidentSysId);
inst.update();
}
var url = new global.AssessmentUtils().getAssessmentInstanceURL(instanceSysId);
gs.eventQueue('manual.send_survey', inc, email, url);
