GlideAjax returns null value

Lon Landry4
Mega Sage

I have an onSubmit catalog client script & script includes. I am trying to populate the State (install_status) with asset matching a catalog variable. I have checked like posts but the solutions have not worked for me.

 
Client script (isolate script is false)
 
function onSubmit() {
  // Get the sys_id of the first_asset_returning variable
  var firstAssetSysId = g_form.getValue('first_asset_returning');
 
  // Use GlideAjax to call the server-side script
  var ga = new GlideAjax('PopulateFirstState'); 
  ga.addParam('sysparm_name', 'getInstallStatus');
  ga.addParam('sysparm_first_asset_returning_sys_id', firstAssetSysId);
 
  // Callback function for success
  ga.getXML(function (response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
 
    // Set the value of the first_state variable with the received answer
    g_form.setValue('first_state', answer);
alert(answer);
 
  });
}

 

Script includes (client callable is true, Accessible from All application scopes)

 

var PopulateFirstState = Class.create();
PopulateFirstState.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getInstallStatus: function () {
    // Get the sys_id parameter from the client-side
    var firstAssetSysId = this.getParameter('sysparm_first_asset_returning_sys_id');
 
    // Query the alm_hardware table to get the install_status value
    var hardwareGr = new GlideRecord('alm_hardware');
    if (hardwareGr.getValue(firstAssetSysId)) {
      var InstallStatus = hardwareGr.getValue('install_status');
 
      return InstallStatus;
    }  
  } 
});
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Lon Landry4 Please update your script include script as follows and you will start getting the data on the client side.

 

var PopulateFirstState = Class.create();
PopulateFirstState.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getInstallStatus: function () {
    // Get the sys_id parameter from the client-side
    var firstAssetSysId = this.getParameter('sysparm_first_asset_returning_sys_id');
 
    // Query the alm_hardware table to get the install_status value
    var hardwareGr = new GlideRecord('alm_hardware');
    if (hardwareGr.get(firstAssetSysId)) { //Used get instead of getValue();
      var InstallStatus = hardwareGr.getValue('install_status');
      return InstallStatus;
    }  
  } 
});

Hope this helps.

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Lon Landry4 Please update your script include script as follows and you will start getting the data on the client side.

 

var PopulateFirstState = Class.create();
PopulateFirstState.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getInstallStatus: function () {
    // Get the sys_id parameter from the client-side
    var firstAssetSysId = this.getParameter('sysparm_first_asset_returning_sys_id');
 
    // Query the alm_hardware table to get the install_status value
    var hardwareGr = new GlideRecord('alm_hardware');
    if (hardwareGr.get(firstAssetSysId)) { //Used get instead of getValue();
      var InstallStatus = hardwareGr.getValue('install_status');
      return InstallStatus;
    }  
  } 
});

Hope this helps.

Thanks Sandeep! I am also using this as a training tool, as I learn to write my own script includes (when one is not available). I changed to an onChange client script due to issues mentioned by other.

-Lon

shubhamdubey
Mega Sage

@Lon Landry4 

Good day !!

 

This is not good practise to using the onSubmit client script with GlideAjax.

Kindly check this articles for your reference.

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-...

Amit Verma
Kilo Patron
Kilo Patron

Hi @Lon Landry4 

 

If you want to call a script include from an on submit client script , it must be a Synchronous call using getXMLWait instead of getXML, otherwise the Catalog item will be submitted before you get a response from the script include.

 

For Service Portal getXMLWait is not supported. You can refer below articles and configure your script include and client accordingly to make it work across Native/Service Portal view:

 

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta...

https://www.servicenow.com/community/developer-blog/servicenow-getxmlwait-alternative-for-service-po...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.