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,


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

9 REPLIES 9

The next step is to add some gs.info logging lines at the beginning of the getRecommendations function to confirm it is running and the value passed in, and to see where it may be going wrong, so something like this:

getRecommendations: function() {
    var prCaseId = this.getParameter('sysparm_pr_case'); // Get PR Case value from Case Task
    gs.info('SI running prCaseId = ' + prCaseId)
    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()) {
        gs.info ('SI in while ' + recGR.name) //identifying field name on rec table
        recommendations.push({
            sys_id: recGR.getUniqueValue(),
            recommendation: recGR.getValue('recommendation')
        });
    }
    return JSON.stringify(recommendations); // Return as JSON
},

 

ShuRiseUp2023
Tera Expert

Do I have to put g_modal inside the getRecommendations function after calling the GlideAjax?

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,


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Thank you for your response. I figured out by myself. first move the g_modal after calling glideAjax. then change the script include to callable from All application scope. 3rd, in the g_modal choice field, fixed the typo 'choice' to 'choices' before options.  

Hi @ShuRiseUp2023 

Glad you got it working! I believe my solution goes with the approach you followed. Could you please close this thread by accepting the solution as this would benefit me and future readers.


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.