having problems to call script includes from a client-side UI actions

ShuRiseUp2023
Tera Expert

Hi Community,

I have tried many times to call script includes from client-side UI actions to fetch some data from server side. It never works and always give me error on the browser console: 

ShuRiseUp2023_0-1741716415705.png

In the Client-side UI actions, use g_modal to show fields on the pop up window, there's a choice field whose options are dynamically fetched using the GlideAjax to call a script include to fetch the data.  The g_modal shows but can't fetch any data from the server side. 

The UI action Workspace Client Script:

 

 

function onClick(g_form) {
    g_modal.showFields({
        title: "Recommendation",
        fields: [{
                type: 'choice',
                name: 'recommendation',
                label: 'Choose your recommendation here. If your recommendation is not listed, please add your own recommendation',
                choice: getRecommendations(),
                mandatory: true
            },
            {
                type: 'textarea',
                name: 'comments',
                label: getMessage('Please add close comment here'),
                mandatory: true
            }
        ],
        size: 'lg'
    }).then(function(fieldValues) {
        g_form.setValue('work_notes', "Recommendation: " + fieldValues.recommendation);
        g_form.setValue('comments', fieldValues.comments);
        g_form.setValue('sub_state', 6); // set sub state to recommendated by preliminary analysis
        g_form.setValue('state', 3); // Set state to closed
        g_form.save();
    });

}

function getRecommendations() {
    var pr_case = g_form.getValue('pr_case');

    var ga = new GlideAjax('<the full API Name of the script include');
    ga.addParam('sysparm_name', 'getRecommendations');
    ga.addParam('sysparm_pr_case', pr_case);

    ga.getXMLAnswer(function(response) {
        var recommendations = JSON.parse(response);

        if (recommendations.length === 0) {
            alert("no recommendations");
            return;
        }

        // Create dropdown options for recommendations
        var options = recommendations.map(function(rec) {
            return {
                displayValue: rec.recommendation,
                value: rec.sys_id
            };
        });

    });
}

 

The script include function: 

 

 

getRecommendations: function() {
        var prCaseId = this.getParameter('sysparm_pr_case'); // Get PR Case value from Case Task
        var recommendations = [];

        var recGR = new GlideRecord('<recommendation table name>'); // Recommendation table
        recGR.addQuery('case', prCaseId); // Match recommendation's 'case' field with case task's 'pr_case'
        recGR.query();

        while (recGR.next()) {
            recommendations.push({
                sys_id: recGR.getUniqueValue(),
                recommendation: recGR.getValue('recommendation')
            });
        }

        return JSON.stringify(recommendations); // Return as JSON
    },

 

Thank you very much
1 ACCEPTED SOLUTION

Medi C
Giga Sage

Hi @ShuRiseUp2023,

 

Please switch the order, first do GlideAjax to get recommendations, and in the callback function, display the modal.

function onClick(g_form) {
    var pr_case = g_form.getValue('pr_case');
    var ga = new GlideAjax('<the full API Name of the script include');
    ga.addParam('sysparm_name', 'getRecommendations');
    ga.addParam('sysparm_pr_case', pr_case);
    ga.getXML(displayModal);
}

function displayModal(response) {
	var options;
    var recommendations = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    if (recommendations.length === 0) {
        alert("no recommendations");
    }
    // Create dropdown options for recommendations
    options = recommendations.map(function(rec) {
        return {
            displayValue: rec.recommendation,
            value: rec.sys_id
        };
    });
    g_modal.showFields({
        title: "Recommendation",
        fields: [{
                type: 'choice',
                name: 'recommendation',
                label: 'Choose your recommendation here. If your recommendation is not listed, please add your own recommendation',
                choice: options,
                mandatory: true
            },
            {
                type: 'textarea',
                name: 'comments',
                label: getMessage('Please add close comment here'),
                mandatory: true
            }
        ],
        size: 'lg'
    }).then(function(fieldValues) {
        g_form.setValue('work_notes', "Recommendation: " + fieldValues.recommendation);
        g_form.setValue('comments', fieldValues.comments);
        g_form.setValue('sub_state', 6); // set sub state to recommendated by preliminary analysis
        g_form.setValue('state', 3); // Set state to closed
        g_form.save();
    });
}

 Please test it and let me know if it works or if it throw an error message with screenshots to assist further on this,


Thanks & Best regards,
Medi

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron

Is the Script Include Client callable?  Do the first two lines extend the AjaxProcessor object?

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

yes.  It's in a scoped application, not Global scope. It always gives me POST 404 Not found error as shown in the post.

 

 

 

var GetRecommendationsForCase= Class.create();
GetRecommendationsForCase.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    initialize: function() {},

 

 

 

 

 

 

 

I don't know if this matters, but Client callable scripts don't have the initialize function.  Are you using the full API Name in the GlideAjax call?  Is the Caller Access -- None -- and Accessible from All application scopes?

BradBowman_0-1741719622587.png

var ga = new GlideAjax('sn_bom.BankingQueryAjax');

Yes. I used the full API Name in the GlideAjax call. set it as you described. Removed the initialize function. but still not working.