OnLoad script retrieve values and display them with the incorrect format

Luis Roman1
Tera Contributor

I have the onChange script (see below) to get the values from the alm_asset table, but when I am retrieving the fields to displayed it in the catalog form, the fields are not display correct?  

Could you please share your knowledge and/or expertise and educate me on how to correct this problem?  I am learning scripting and cannot figure the solution. The script below I need assistance to address the following issues:  

  • The manage_by display the sysid, how can I display the actual string value?
  • The state display the integer of the choice value, how can I display the actual value (string value)?
  • The asset_use is display as “loaner” instead of displaying “Loaner” that is the value on the alm_asset table?

Your feedback and scripting education will be greatly appreciated.

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    try {

       g_form.getReference('asset', callback);

function callback(ret) {

             g_form.setValue('location',ret.location);

              g_form.setValue('asset_use',ret.asset_function);

              g_form.setValue('state',ret.install_status);

              g_form.setValue('managed_by',ret.managed_by);

              }

} catch (ex) {

    g_form.addErrorMessage(" TYPE [on assigned_asset Change] encountered the error: " + ex.message);

    g_form.addInfoMessage("TYPE [on assigned_asset Change] encountered the error:" + ex.message);

}

}

 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

HELLO @Luis Roman ,

The method you are using to get the values from your database is getReference() where there is only single level of dot-walking is allowed which you did perfectly in the above script .

But in order to get the display value of the value for example 

g_form.setValue('asset_use',ret.asset_function.getDisplayValue()); -->you need to write like this which is a double dot walking which is not possible in get reference so you need to change your approach by writing an on change client script on asset field and call a script include from it to get your values .

Sample code:

CLIENT SCRIPT:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue == '') {
  return;
 }
 //Type appropriate comment here, and begin script below
 var ga = new GlideAjax('getStandardFields');//this is the script include
  ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
  ga.addParam("sysparm_asset",newValue);
  ga.getXML(getResponse);
}

function getResponse(response) {
  var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
  g_form.setValue('asset_use', values[0]);
  g_form.setValue('state', values[1]);
g_form.setValue('managed_by', values[2]);
}
}

SCRIPT INCLUDE:

var getStandardFields = Class.create();


getStandardFields.prototype = {
getFields : function() {
var assetID = this.getParameter('sysparm_asset');
var standardFields = new GlideRecord('alm_asset');
standardFields.addQuery('sys_id',assetID);
 standardFields.query();
if(standardFields.next()) {
return standardFields.asset_function.getDisplayValue()+ '|' + standardFields.install_status.getDisplayValue()+'|'+standardFields.managed_by.getDisplayValue();
}
return '';
},
type: 'getStandardFields'
};

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

 

View solution in original post

3 REPLIES 3

Mohith Devatte
Tera Sage
Tera Sage

HELLO @Luis Roman ,

The method you are using to get the values from your database is getReference() where there is only single level of dot-walking is allowed which you did perfectly in the above script .

But in order to get the display value of the value for example 

g_form.setValue('asset_use',ret.asset_function.getDisplayValue()); -->you need to write like this which is a double dot walking which is not possible in get reference so you need to change your approach by writing an on change client script on asset field and call a script include from it to get your values .

Sample code:

CLIENT SCRIPT:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue == '') {
  return;
 }
 //Type appropriate comment here, and begin script below
 var ga = new GlideAjax('getStandardFields');//this is the script include
  ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
  ga.addParam("sysparm_asset",newValue);
  ga.getXML(getResponse);
}

function getResponse(response) {
  var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
  g_form.setValue('asset_use', values[0]);
  g_form.setValue('state', values[1]);
g_form.setValue('managed_by', values[2]);
}
}

SCRIPT INCLUDE:

var getStandardFields = Class.create();


getStandardFields.prototype = {
getFields : function() {
var assetID = this.getParameter('sysparm_asset');
var standardFields = new GlideRecord('alm_asset');
standardFields.addQuery('sys_id',assetID);
 standardFields.query();
if(standardFields.next()) {
return standardFields.asset_function.getDisplayValue()+ '|' + standardFields.install_status.getDisplayValue()+'|'+standardFields.managed_by.getDisplayValue();
}
return '';
},
type: 'getStandardFields'
};

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU

 

Thank You Mohith for sharing your knowledge, expert solution and educate me in my journey with AJAX script.  

asifnoor
Kilo Patron

Hi,

As Mohith suggested below, you need to use script include to fetch the data. In the SI, you can use getDispalyValue and return the actual values instead of sys_ids and can use them to show in your client script.

Also using g_form.getReference is not recommended.

Mark the comment as a correct answer and helpful if this has helped to solve the problem.