We are facing some issue on the client scripts for onload and on change scripts

rajeshKongamudi
Tera Contributor

Hi Community,

 

we have written a code when the user has cost center and manager and location should auto populate this is working fine, but for the cost center it is referring to the person department f the user has department then it should get populate on onload and on change if the person doesn't have any department then a (please_mention_cost_center) this variable should be visible and mandatory here everything works fine for admin roles but when I impersonate as end user the error saying( Error There is a JavaScript error in your browser console) showing this and console message is showing as below. can someone please help me to overcome this issue.

rajeshKongamudi_0-1716382967946.png 

 

Here below the validation was not working eve though i have changed the user for onchange and as well for onloading of the form.

rajeshKongamudi_1-1716383168523.png

 

 

This is my on change client script.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('LRQA_fetchlocation_details');
    ga.addParam('sysparm_name', 'getLocationDetails');
    ga.addParam('sysparm_userSysId', g_form.getValue('requested_for'));
    ga.getXML(getEmailcallBack);
    function getEmailcallBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var result = JSON.parse(answer);
        g_form.setValue('requestor_s_manager', result.manager);
        g_form.setValue('cost_centre', result.department);
        g_form.setValue('location', result.location);
        // Check if the cost center field is empty
        if (!result.department || isLoading) {
            // Making the new variable mandatory and visible
            g_form.setMandatory('please_mention_cost_center', true);
            g_form.setDisplay('please_mention_cost_center', true);
            g_form.setReadOnly('cost_centre', true);
            // Adding an alert or a message to notify the user
            g_form.addInfoMessage('Please fill in the required fields.');
        } else{
            // Making the new variable not mandatory and not visible
            g_form.setMandatory('please_mention_cost_center', false);
            g_form.setDisplay('please_mention_cost_center', false);
            g_form.setReadOnly('cost_centre', false);       
        }
    }
 }
this is my onload client script that it should get works on onloading the form as well. i'm using the onload script not only for the onloading of the form but also it should get applicable for the RITM and sc task level
function onLoad() {
   fetchLocationDetails();
   function fetchLocationDetails() {
        var ga = new GlideAjax('LRQA_fetchlocation_details');
        ga.addParam('sysparm_name', 'getLocationDetails');
        ga.addParam('sysparm_userSysId', g_form.getValue('requested_for'));
      ga.getXML(getEmailcallBack);
   }
    function getEmailcallBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var result = JSON.parse(answer);
        g_form.setValue('requestor_s_manager', result.manager);
        g_form.setValue('cost_centre', result.department);
        g_form.setValue('location', result.location);
        if (!result.department) {
            // Making the new variable mandatory and visible
            g_form.setMandatory('please_mention_cost_center', true);
            g_form.setDisplay('please_mention_cost_center', true);
            g_form.setReadOnly('cost_centre', true);
            // Adding an alert or a message to notify the user
            //g_form.addInfoMessage('Please fill in the required fields.');
        } else {

            // Making the new variable not mandatory and not visible
            g_form.setMandatory('please_mention_cost_center', false);
            g_form.setDisplay('please_mention_cost_center', false);
            //g_form.setReadOnly('cost_centre', false);
        }
    }
}



 

1 ACCEPTED SOLUTION

KevinBellardine
Kilo Sage

If this is working for your admin accounts then I would check the ACL's on the GlideAjax and make sure your users have access to the script include. If that's not it could you provide the GlideAjax code?

View solution in original post

5 REPLIES 5

dgarad
Giga Sage

Hi @rajeshKongamudi 

in script includes the preferred way to return multiple values is to place them into an object then stringify (convert the object to a string) and return to the client script

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Hi Mega, here is my script include can you please suggest how I can proceed in the script include here. thanks in advance

 

var LRQA_fetchlocation_details = Class.create();
LRQA_fetchlocation_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocationDetails: function() {
        var sys_id = this.getParameter('sysparm_userSysId');
        var user = new GlideRecord('sys_user');
        user.get(sys_id);
        var userdata = {};
        userdata.location = user.location.toString();
        userdata.cost_center = user.cost_center.toString();
        userdata.department = user.department.toString();
        userdata.manager = user.manager.toString();
        return JSON.stringify(userdata);

 },
    type: 'LRQA_fetchlocation_details'
});

KevinBellardine
Kilo Sage

If this is working for your admin accounts then I would check the ACL's on the GlideAjax and make sure your users have access to the script include. If that's not it could you provide the GlideAjax code?

Hi @KevinBellardine 

 

Thanks for the reply. I have given the access to the end users(snc_internal) as well now this code working fine. my other query was it fine if we gave the access to the ITIL users also?

 

Below was the ACL access I have given.

rajeshKongamudi_0-1716395577315.png