how to use asynchronous glideajax call

divya123
Giga Contributor

i used below code for avoiding duplicate asset tab with state is in-use, here instead of using gliderecord how can i user asynchronous glide ajax call, can anyone plerase rspond how can i user asynchronous glideajax call for below code

function onSubmit() {
 //no duplicate asset in state "in use" is allowed
 var sysid = g_form.getUniqueValue();
 var asset = g_form.getValue('asset_tag');
 var state = g_form.getValue('install_status');
 g_form.hideFieldMsg('asset_tag');
 if ( doesAssetExist(asset, sysid) > 0 && state == '1' ) {
  g_form.showFieldMsg('asset_tag','Only one asset can be in state "In Use"!','error');
  return false;
 }
 return true;
}

function doesAssetExist(asset, sysid) {
 var i=0;
 var rec = new GlideRecord('alm_asset');
 rec.addQuery('asset_tag',asset);
 rec.addQuery('install_status', '1');
 rec.query();
 while (rec.next()) {
  if ( rec.sys_id != sysid ) {
   i++;
  }
 }
 return i;
}

14 REPLIES 14

HI,

 

ga.addParam('sysparm_name', asset); this is your script include function name.

 

 if ( rec.sys_id != sysid ) this sysid use to come from your client script glide ajax but now we dont need that right? If you need it then we have to bering it back from GLide ajax as we did it earlier.

 

So it will look like below script:

Script include:

var AsettagDuplicate = Class.create();
AsettagDuplicate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
asset: function()
 {
 var assetTag= this.getParameter('sysparm_id');

var sysid = this.getParameter('sysparm_sys');
 var rec = new GlideRecord('alm_asset');

rec.addQuery('asset_tag', assetTag);

 rec.addQuery('install_status', '1');
 rec.query();
 while (rec.next()) {
 if ( rec.sys_id != sysid ) {
 i++;
 }
 }

//Try putting log here and see value of i

gs.log('Return I '+i);
 return i;
 },
    type: 'AsettagDuplicate'
});

Client script:

function doesAssetExist(assetTag, sysid){
 
 var ga = new GlideAjax('AsettagDuplicate');
ga.addParm('sysparm_name', asset); // Here there was mistake in Parm name as well check spellings as well
ga.addParm('sysparm_id',assetTag);

ga.addParm('sysparm_sys',sysid);
ga.getXMLWait(HelloWorldParse); //Try making it syncronous
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); //What alert is coming here?
return answer;
}


Thanks,

Ashutosh Munot

 

Please Hit Correct, Helpful if you are satisfied with this response.

i checked it and its not working , if possible can you try it in your instance n let me know if it is working.

Using GlideAggregator, rather than GlideRecord, seems more appropriate. Make sure to add a query of 

// alternate contents of asset function
var sysid = this.getParameter('sysparm_id');
var recAgg = new GlideAggregate('alm_asset');
recAgg.addQuery('asset_tag', this.getParameter('sysparm_asset');
recAgg.addQuery('install_status', '1');
recAgg.addQuery('sys_id', '!=', sysid);
recAgg.addAggregate('COUNT');
recAgg.query();
if (recAgg.next()) {
   return recAgg.getAggregate('COUNT');
}
return 0;

Cheers,

Dave

Harsh Vardhan
Giga Patron

Hi Divya,

 

you can write glide record part in your client callable script include and call that script include in your client script by using glide ajax.

if you are going with async then use getXMLWait().

 

Kindly refer the link below.

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

 

nayanawadhiya1
Kilo Sage

Refer this link - 

 

Write a Client Callable Script Include and call it in Client Script using GLIDEAJAX.

Async GlideAjx

Glide Ajax