Catalog client script returning sys_id instead field value

feibian
Kilo Contributor

I have an OnLoad catalog client script to populate fields based on information from a user's profile. other value is returned properly (email) except for the department field. it's giving me the sys_id for the department instead of displaying the department's name. department field is a reference field in sys_user table.

Your help on this will be appreciated

Catalog í�tem

find_real_file.png

Catalog Client Script

find_real_file.png

1 ACCEPTED SOLUTION

That is what I expected. I have a script include that you can use (and re-use) for populating user values:


Name: usrUtilsAjax


Client callable: true


Script:


var usrUtilsAjax = Class.create();


usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      getUsrDetails: function() {


              var result = this.newItem("result");


              result.setAttribute("message", "returning user details");


              var uID = this.getParameter('sysparm_usr');


              var usrObj = new GlideRecord('sys_user');


              usrObj.get(uID);


              this._addDetail('department', usrObj.department);


              this._addDetail('dept_name', usrObj.department.getDisplayValue());


              this._addDetail('email', usrObj.email);


      },



      _addDetail: function(name, value) {


              var det = this.newItem("detail");


              det.setAttribute("name", name);


              det.setAttribute("value", value);


      },



      type: 'usrUtilsAjax'


});


I included both department and dept_name in case you are populating reference fields or text fields.



You can use something similar to the following for your client script:


function onLoad() {


      //Populate client details


      var ga = new GlideAjax('usrUtilsAjax');


      ga.addParam('sysparm_name', 'getUsrDetails');


      ga.addParam('sysparm_usr', newValue);


      ga.getXML(setDetails);


}



function setDetails(serverResponse) {


      var details = serverResponse.responseXML.getElementsByTagName('detail');


      for (i = 0; i < details.length; i++) {


              var name = details[i].getAttribute('name');


              var value = details[i].getAttribute('value');


              if (name == 'dept_name'){g_form.setValue('YOUR_DEPARTMENT_VARIABLE',value);}


              if (name == 'email'){g_form.setValue('YOUR_EMAIL_VARIABLE', value);}


      }


}



Just be sure to change YOUR_XXX_VARIABLE to be the appropriate variable names you wish to set.


View solution in original post

15 REPLIES 15

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Fabian,



Two options here.


1.Either change the field from string to reference on catalog item


2. Get the display value by doing GlideAjax call.



Please let me know if you have any questions.


ccajohnson
Kilo Sage

The department field is a reference field. You may be able to get away with changing user.department to be user.department.name



If this does not work, you may have to use an AJAX call to get the values of the user to set. Let us know if you need help with this and we can provide examples.


Hi Chris,



I just changed the script with user.department.name instead user.department and I am getting an undefined value



find_real_file.png


find_real_file.png


That is what I expected. I have a script include that you can use (and re-use) for populating user values:


Name: usrUtilsAjax


Client callable: true


Script:


var usrUtilsAjax = Class.create();


usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      getUsrDetails: function() {


              var result = this.newItem("result");


              result.setAttribute("message", "returning user details");


              var uID = this.getParameter('sysparm_usr');


              var usrObj = new GlideRecord('sys_user');


              usrObj.get(uID);


              this._addDetail('department', usrObj.department);


              this._addDetail('dept_name', usrObj.department.getDisplayValue());


              this._addDetail('email', usrObj.email);


      },



      _addDetail: function(name, value) {


              var det = this.newItem("detail");


              det.setAttribute("name", name);


              det.setAttribute("value", value);


      },



      type: 'usrUtilsAjax'


});


I included both department and dept_name in case you are populating reference fields or text fields.



You can use something similar to the following for your client script:


function onLoad() {


      //Populate client details


      var ga = new GlideAjax('usrUtilsAjax');


      ga.addParam('sysparm_name', 'getUsrDetails');


      ga.addParam('sysparm_usr', newValue);


      ga.getXML(setDetails);


}



function setDetails(serverResponse) {


      var details = serverResponse.responseXML.getElementsByTagName('detail');


      for (i = 0; i < details.length; i++) {


              var name = details[i].getAttribute('name');


              var value = details[i].getAttribute('value');


              if (name == 'dept_name'){g_form.setValue('YOUR_DEPARTMENT_VARIABLE',value);}


              if (name == 'email'){g_form.setValue('YOUR_EMAIL_VARIABLE', value);}


      }


}



Just be sure to change YOUR_XXX_VARIABLE to be the appropriate variable names you wish to set.