Create and launch assessment from ui action

rohitservicenow
Mega Guru

Hi, We have a requirement from L2 teams where they want to have the possibility to share the feedback with L1 teams when the tickets are passed to L2 without the proper checks. 

To achieve above, I am thinking to make use of assessments, but have difficulty creating an launching the assessment instance from ui action. Want to launch assessment instance in an ui page/new window so the users don't need to navigate to another page. Following is the script, I am using. It is creating the assessment instance, but not launching. 

Please advise if any errors/corrections. 

IF you think of another solution, please share it. 

_______________________________________________________________-

var location;
var typeSysID = 'd898867a2f0750103b9f57892799b668'; //The sys_id of an "Assessment Metric Type" record (asmt_metric_type)
var createAssessmentOutput = (new SNC.AssessmentCreation()).createAssessments(typeSysID, "", gs.getUserID());
var assessmentDetailsArray = createAssessmentOutput.split(',');
var assessmentInstanceSysId = assessmentDetailsArray[0];
if (assessmentDetailsArray.length >= 3) {
var grAsmtInstance = new GlideRecord('asmt_assessment_instance');
if (grAsmtInstance.get(assessmentInstanceSysId)) {
grAsmtInstance.trigger_table = current.getTableName();
grAsmtInstance.trigger_id = current.getValue('sys_id');
grAsmtInstance.update();

location = 'assessment_take2.do?sysparm_assessable_sysid=' + grAsmtInstance.sys_id.toString() + '&sysparm_assessable_type=' + grAsmtInstance.metric_type.toString();

}
} else {
throw 'Could not create new Survey: not found';
}
} else {
throw 'Could not create new Survey:' + createAssessmentOutput;
}
window.open(location, "Feedback", "width=600,height=500,toolbar=yes,scrollbars=yes");

_____________________________________________________________

Thank you.

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

You mixed client and server side code. Use a Script include to handle the server side code and set the UI Action to Client like so:

find_real_file.png

And fill Onclick

find_real_file.png

 

 

UI Action script:

function onClick() {
    var ga = new GlideAjax('global.customSurveyUtil');
    ga.addParam('sysparm_name', 'createSurvey');
    ga.addParam('sysparm_tablename', g_form.getTableName());
    ga.addParam('sysparm_sysid', g_form.getUniqueValue());
    ga.getXML(ajaxResponse);
    function ajaxResponse(serverResponse) {
        var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
        //window.open(answer, "Feedback", "width=600,height=500,toolbar=yes,scrollbars=yes");
        g_navigation.openPopup(answer);
    }

}

If g_navigation is not to your liking try the window.open.

 

Then create a script include. Set to Client collable:

var customSurveyUtil = Class.create();
customSurveyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    createSurvey: function () {
        var typeSysID = 'd898867a2f0750103b9f57892799b668'; //The sys_id of an "Assessment Metric Type" record (asmt_metric_type)
        var createAssessmentOutput = (new SNC.AssessmentCreation()).createAssessments(typeSysID, "", gs.getUserID());
        var assessmentDetailsArray = createAssessmentOutput.split(',');
        var assessmentInstanceSysId = assessmentDetailsArray[0];
        var grAsmtInstance = new GlideRecord('asmt_assessment_instance');
        if (grAsmtInstance.get(assessmentInstanceSysId)) {
            grAsmtInstance.trigger_table = this.getParameter('sysparm_tablename');
            grAsmtInstance.trigger_id = this.getParameter('sysparm_sysid');
            grAsmtInstance.update();
            return 'assessment_take2.do?sysparm_assessable_sysid=' + grAsmtInstance.sys_id.toString() + '&sysparm_assessable_type=' + grAsmtInstance.metric_type.toString();
        }
    },

    type: 'customSurveyUtil'
});

 

Let me know if this works

View solution in original post

16 REPLIES 16

Willem
Giga Sage
Giga Sage

You mixed client and server side code. Use a Script include to handle the server side code and set the UI Action to Client like so:

find_real_file.png

And fill Onclick

find_real_file.png

 

 

UI Action script:

function onClick() {
    var ga = new GlideAjax('global.customSurveyUtil');
    ga.addParam('sysparm_name', 'createSurvey');
    ga.addParam('sysparm_tablename', g_form.getTableName());
    ga.addParam('sysparm_sysid', g_form.getUniqueValue());
    ga.getXML(ajaxResponse);
    function ajaxResponse(serverResponse) {
        var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
        //window.open(answer, "Feedback", "width=600,height=500,toolbar=yes,scrollbars=yes");
        g_navigation.openPopup(answer);
    }

}

If g_navigation is not to your liking try the window.open.

 

Then create a script include. Set to Client collable:

var customSurveyUtil = Class.create();
customSurveyUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    createSurvey: function () {
        var typeSysID = 'd898867a2f0750103b9f57892799b668'; //The sys_id of an "Assessment Metric Type" record (asmt_metric_type)
        var createAssessmentOutput = (new SNC.AssessmentCreation()).createAssessments(typeSysID, "", gs.getUserID());
        var assessmentDetailsArray = createAssessmentOutput.split(',');
        var assessmentInstanceSysId = assessmentDetailsArray[0];
        var grAsmtInstance = new GlideRecord('asmt_assessment_instance');
        if (grAsmtInstance.get(assessmentInstanceSysId)) {
            grAsmtInstance.trigger_table = this.getParameter('sysparm_tablename');
            grAsmtInstance.trigger_id = this.getParameter('sysparm_sysid');
            grAsmtInstance.update();
            return 'assessment_take2.do?sysparm_assessable_sysid=' + grAsmtInstance.sys_id.toString() + '&sysparm_assessable_type=' + grAsmtInstance.metric_type.toString();
        }
    },

    type: 'customSurveyUtil'
});

 

Let me know if this works

Thank you so much Willem. Yes, it is opening the survey in the new window. Wondering if it's possible to open it in a pop-up window and copy the survey answers to the worknotes of the ticket?

Thank you again for the great help. 

Just realised, window.open is opening the survey in a pop-up window. Now just need to have the possibility to copy the answer of one of the questions to worknotes, if it's possible?

 

Thank you again.

Hi Rohit,

Great that this is working for you! 🙂

The best approach will be to mark this answer as Correct and post the question about copying the answers as a new question. By doing so others will be able to assist us in this as well. It will also make the answer I gave available for others to find, helping them as well.

 

Can you kinly mark the answer as Correct and create the new question? I will continue helping of course! 🙂