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

Sounds like you've got it working? Internal should cover your ITIL users, though it's a pretty broad permission so you might want to consider use a different role.