No results returned from Script Include when called from Client Script in Declarative UI Action

kristinakra
Tera Contributor

In my scoped app I have created: Declarative UI Action implemented as client script (for SOW) and client callable Script Include. However, I see that the function which should be called from the GlideAjax (reopenForProblemAjax) is not even called and the result is always null. From the script condition of the UI Action I call isSurveyCompleted and it is working as expected so the issue is with the GlideAjax call.

Client script: 

function onClick() {

    var ga = new GlideAjax('scopedApp.MyScriptInclude);

    ga.addParam('sysparm_name', 'reopenForProblemAjax');
    ga.addParam('sysparm_problem_sys_id', g_form.getUniqueValue());
    ga.addParam('sysparm_user_sys_id', g_user.userID);

    ga.getXMLAnswer(function(instanceID) {

        if (!instanceID) {
            return;
        }

        var url = 'assessment_take2.do?sysparm_assessable_sysid=' + instanceID +
                  '&sysparm_assessable_type=asmt_assessment_instance';

        var win = top.window.open(url, '_blank');
        if (win) {
            win.focus();
        }
    });
}

My Script Include (I have used the exact API from the script include in the client script): 

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    initialize: function() {
    },

    reopenForProblemAjax: function() {
        var problemSysId = this.getParameter('sysparm_problem_sys_id');
        var userSysId = this.getParameter('sysparm_user_sys_id');
        var result = this.reopenForProblem(problemSysId, userSysId);
        return result;
    },

    isSurveyCompletedAjax: function() {
        var problemSysId = this.getParameter('sysparm_problem_sys_id');
        return this.isSurveyCompleted(problemSysId);
    },

    // Reopens the last completed survey for the problem and assigns it to the user
    reopenForProblem: function(problemSysId, userSysId) {
        var surveyInstanceGr = this.getSurveyInstance(problemSysId);

        if (surveyInstanceGr.next()) {
            surveyInstanceGr.state = 'ready';
            surveyInstanceGr.user = userSysId;
            surveyInstanceGr.update();
            return surveyInstanceGr.sys_id.toString();
        }

        return null; // no completed survey found
    },

    // Checks if there is any completed survey for the problem 
    isSurveyCompleted: function(problemSysId) {
        var hasCompleted = this.getSurveyInstance(problemSysId).hasNext();
        return hasCompleted; // true if a completed survey exists
    },

    getPostMortemSurveyId: function() {
        // Get the Survey record dynamically by name 
        var metricTypeGR = new GlideRecord('asmt_metric_type');
        metricTypeGR.addQuery('name', 'Problem Survey');
        metricTypeGR.setLimit(1);
        metricTypeGR.query();

        if (!metricTypeGR.next()) {
            return false;
        }

        return metricTypeGR.sys_id;
    },

    getSurveyInstance: function(problemSysId) {
        var surveyId = this.getPostMortemSurveyId();

        if (!surveyId) {
            return new GlideRecord('asmt_assessment_instance'); // empty GR
        }

        var surveyInstanceGr = new GlideRecord('asmt_assessment_instance');
        surveyInstanceGr.addQuery('metric_type', surveyId);
        surveyInstanceGr.addQuery('trigger_id', problemSysId);
        surveyInstanceGr.addQuery('state', 'complete');
        surveyInstanceGr.setLimit(1);
        surveyInstanceGr.query();

        return surveyInstanceGr;
    },

    type: 'MyScriptInclude'
});

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@kristinakra 

Few issues in your script and my thoughts

-> client callable script include don't have initialize method, remove that and then try to call, share screenshot of your script include

-> if you are calling it from script condition then how are you passing the value to that method, I couldn't see that function accepting any parameters?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

4 REPLIES 4

Mark Manders
Mega Patron
var ga = new GlideAjax('scopedApp.MyScriptInclude);

You are missing a closing ' on your script include name. Nothing will happen with the onClick. Since I assume you didn't want to share your scope name and name of the script include, it could just be a typo in your question, but please validate that first. And check if there are any typos in the scope and/or script include name.
Is it callable from the scope you are in?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

It's just a typo in the question, it has closing ' on my client script and the names are matching. No typos in both scripts.

Ankur Bawiskar
Tera Patron

@kristinakra 

Few issues in your script and my thoughts

-> client callable script include don't have initialize method, remove that and then try to call, share screenshot of your script include

-> if you are calling it from script condition then how are you passing the value to that method, I couldn't see that function accepting any parameters?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

kristinakra
Tera Contributor

I've found the solution: First I created an ACL for the Script Include and then removed the initialize method as @Ankur Bawiskar suggested. Thank you all for your help!