UI Action on form to create and assign a survey

mmarinov007
Tera Contributor

Hello. I have the following UI Action button - Task Assessment, that when clicked on the [idea] form table should create and assign a new survey. Within the UI Action I added the following triggerAssessment() function: 

 

function triggerAssessment() {

    // Call a GlideAjax Script Include to start the assessment

    var ga = new GlideAjax('TriggerAssessment');

    ga.addParam('sysparm_name', 'startAssessment');

    ga.addParam('sysparm_idea_sys_id', g_form.getUniqueValue());

    ga.getXMLAnswer(function(response) {

        if (response) {

            // Show a confirmation or open modal

            g_form.showFieldMsg('short_description', 'Assessment has been assigned. Please check the portal.', 'info');

        } else {

            g_form.showFieldMsg('short_description', 'Error assigning assessment.', 'error');

        }

    });

 

and the following Script Include:

 

var TriggerAssessment = Class.create();

TriggerAssessment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    startAssessment: function() {

        var ideaId = this.getParameter('sysparm_idea_sys_id');

        if (!ideaId) return false;

 

        var ideaGR = new GlideRecord('idea');

        if (!ideaGR.get(ideaId)) return false;

 

        // Create assessment instance

        var asmtUtils = new AssessmentUtils();

        var instanceId = asmtUtils.createAssessmentInstance('23f2df3ffb46ee10a70ef7937befdcaa', ideaGR.sys_id);

 

        return !!instanceId;

    }

 

});

 

I tried using the AssessmentUtils to create a new instance of the survey and yet for some reason it still doesn't work. Any ideas as to why this is happening?

3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @mmarinov007,

 

To create and assign a new survey when the "Task Assessment" UI Action button is clicked on the [idea] form table, you'll need to implement a server-side script within the UI Action. This script will leverage the AssessmentCreation API to create the survey and then assign it to a user. 
 
Here's a breakdown of the process and a sample script:
1. UI Action Setup 
  • Table: idea
  • Action name: A unique name for the UI Action (e.g., create_assessment_on_idea)
  • Form button: Check this box to display the action as a button on the form.
  • Client: Uncheck this box, as the core logic will be server-side.
  • On click: You can optionally add a client-side function here to provide a confirmation message or handle other client-side logic before submitting the server-side script.
  • Script: This is where the server-side logic will reside. 
     
2. Server-Side Script:
(function executeUACode(current, g_scratchpad, baseElement) {    try {      // 1. Define the assessment type (replace with your actual assessment type sys_id)      var assessmentTypeSysId = 'your_assessment_type_sys_id'; // Replace with the actual sys_id of the assessment type      // 2. Get the user to assign the assessment to (e.g., current user, a specific user, etc.)      var assignee = gs.getUserID(); // Or gs.getUser().getID() or any other user's sys_id      // 3. Create the assessment using the AssessmentCreation API      var assessmentCreation = new sn_assessment_core.AssessmentCreation();      assessmentCreation.setUser(assignee);      assessmentCreation.conditionTrigger(current, assessmentTypeSysId);      // 4. Optionally, handle success or failure      gs.addInfoMessage('Assessment created and assigned to: ' + gs.getUserDisplayName());    } catch (err) {      gs.addErrorMessage('Error creating assessment: ' + err);    }})(current, g_scratchpad, baseElement);


If this is helpful, please mark it as helpful and accept the correct solution by referring to this solution in future, it will be helpful to them.

 

Thanks & Regards,

Abbas Shaik

If we had to make it client callable how would it work? Here is the UI Action implementation: 

function onClick() {
  var ga = new GlideAjax('CreateSurveyInstance');
  ga.addParam('sysparm_name', 'createSurvey');
  ga.addParam('sysparm_target_id', g_form.getUniqueValue());

  ga.getXML(function(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
      setTimeout(function() {
        location.href = answer;
      }, 200);
    } else {
      alert('Survey could not be created.');
    }
  });
}
 
I created a trigger condition and then applied it to the script include in the following way: 
var CreateSurveyInstance = Class.create();
CreateSurveyInstance.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  createSurvey: function() {
    var metricTypeSysId = '23f2df3ffb46ee10a70ef7937befdcaa';
    var targetId = this.getParameter('sysparm_target_id');

    var ideaGR = new GlideRecord('idea');
    if (!ideaGR.get(targetId)) {
      return '';
    }

    var submitter = ideaGR.getValue('submitter');
    if (!submitter) {
      return '';
    }

    var assessmentCreation = new sn_assessment_core.AssessmentCreation();
    assessmentCreation.setUser(submitter);

    var instanceId = assessmentCreation.conditionTrigger(ideaGR, metricTypeSysId);

    if (!instanceId) {
      return '';
    }

    return '/asmt_assessment_instance.do?sys_id=' + instanceId;
  }

});
 
When a user clicks on the Task Assessment button it doesn't load the survey, but it creates a new instance in the asmt_assessment_instance. Where do you think is the problem?

Ankur Bawiskar
Tera Patron
Tera Patron

@mmarinov007 

is glideajax working?

what debugging did you do?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader