Want to set another field value based requested for location and sector

slimgam
Tera Contributor

Hi Everyone, 

 

want to set another variable choice field value, based on requested for user location and sector, please help me with client script how to set that, below  on load client script i have using but it's not working

 

    var ga = new GlideAjax("UserGlideRecordAjax");
    ga.addParam("sysparm_name", "getFieldValues");
    ga.addParam("sysparm_parentTable", "sys_user");
    ga.addParam("sysparm_parentSysID", newValue);
    ga.addParam("sysparm_parentFieldNames", "location,u_sector");
    ga.getXMLAnswer(setLocation);
    
   
    function setLocation(answer) {
        var answerJSON = JSON.parse(answer);
      
      if(answerJSON.location == 'DCWA-Sumner' && answerJSON.u_sector == 'SCS'){
       
        g_form.setValue("functional_area", 'scs');
    }
}
}
 
#client script # catalog client script 
1 ACCEPTED SOLUTION

Shraddha Kadam
Mega Sage

Hello @slimgam,

 

You can try like below code -

Catalog Client script -

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }
var gr = new GlideAjax('ICClientCallableScriptInclude');
        gr.addParam('sysparm_name', 'returnEmployeeDetails');
        gr.addParam('sysparm_user', employee);
        gr.getXML(getResponse);

  function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
        g_form.setValue('email', values[0]);
        g_form.setValue('location', values[1]);
    }
}

 

 

Script Include-

 

returnEmployeeDetails: function() {
        var data = '';
        var employee = this.getParameter('sysparm_user');
        var grEmployee = GlideRecord('sys_user');
        grEmployee.addQuery('sys_id', employee);
        grEmployee.query();
        while (grEmployee.next()) {
            data = grEmployee.email + ',' + grEmployee.location;
            return data;

        }

    },

 

 If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !

 

Thank you

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

2 REPLIES 2

Brian Lancaster
Tera Sage

Use getXML instead of getXMLAnswer. I don't believe getXMLAnswer is the correct thing to use in this cases. 

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...

Shraddha Kadam
Mega Sage

Hello @slimgam,

 

You can try like below code -

Catalog Client script -

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }
var gr = new GlideAjax('ICClientCallableScriptInclude');
        gr.addParam('sysparm_name', 'returnEmployeeDetails');
        gr.addParam('sysparm_user', employee);
        gr.getXML(getResponse);

  function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
        g_form.setValue('email', values[0]);
        g_form.setValue('location', values[1]);
    }
}

 

 

Script Include-

 

returnEmployeeDetails: function() {
        var data = '';
        var employee = this.getParameter('sysparm_user');
        var grEmployee = GlideRecord('sys_user');
        grEmployee.addQuery('sys_id', employee);
        grEmployee.query();
        while (grEmployee.next()) {
            data = grEmployee.email + ',' + grEmployee.location;
            return data;

        }

    },

 

 If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !

 

Thank you

If my response was helpful, please mark it as correct and helpful.
Thank you.