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

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi Divya,

 

Use Below code:

 

function doesAssetExist(asset, sysid) {

var ga = new GlideAjax('Test');
ga.addParam('sysparm_name', 'asset');
ga.addParam('sysparm_asset', asset);
ga.addParm('sysparm_id',sysid); ga.getXMLWait(HelloWorldParse); function HelloWorldParse(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); alert(answer);
//Return ur I from here
return answer;
}


}

 

Script Include:

var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
asset: function()
{
var sysid = this.getParameter('sysparm_id');
var rec = new GlideRecord('alm_asset');
rec.addQuery('asset_tag',this.getParamter('sysparm_asset'));
rec.addQuery('install_status', '1');
rec.query();
while (rec.next()) {
if ( rec.sys_id != sysid ) {
i++;
}
}
return i;
},
type: 'Test'
});

 

Thanks,

Ashutosh Munot

 

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

Hi ashotosh,

i used below script but it is not working . can you please check and suggest me if anything need to modify in the script

 

script include:

var AsettagDuplicate = Class.create();
AsettagDuplicate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
asset: function()
 {
 var sysid = this.getParameter('sysparm_id');
 var rec = new GlideRecord('alm_asset');
 rec.addQuery('asset_tag', 'sysid'); //this.getParamter('sysparm_id')
 rec.addQuery('install_status', '1');
 rec.query();
 while (rec.next()) {
 if ( rec.sys_id != sysid ) {
 i++;
 }
 }
 return i;
 },
    type: 'AsettagDuplicate'
});

 

client script:

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 ga = new GlideAjax('AsettagDuplicate');
ga.addParam('sysparm_name', asset);
//ga.addParam('sysparm_asset', asset);
ga.addParam('sysparm_id',sysid);
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer);
return answer;
}
 
}

HI Divya,

 

Corrected Script:

 

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

var assetTag = this.getParameter('sysparm_asset');
 var rec = new GlideRecord('alm_asset');

//This is wrong
 rec.addQuery('asset_tag', 'sysid'); //this.getParamter('sysparm_id')

//It should be

rec.addQuery('asset_tag', assetTag);
 rec.addQuery('install_status', '1');
 rec.query();
 while (rec.next()) {
 if ( rec.sys_id != sysid ) {
 i++;
 }
 }
 return i;
 },
    type: 'AsettagDuplicate'
});


Thanks,
Ashutosh Munot

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

HI Divya,

 

I have one doubt?

 See below script of your's:

 

var ga = new GlideAjax('AsettagDuplicate');
ga.addParam('sysparm_name', asset);
//ga.addParam('sysparm_asset', asset); // Do you want this or not
ga.addParam('sysparm_id',sysid); // Which Sys Id is this?? Asset Record ? If you dont want asset tag then in script include
ga.getXML(HelloWorldParse);

 

 

In Script include:

 

rec.addQuery('asset_tag', 'sysid'); //this.getParamter('sysparm_id')

Asset tag is not a reference field so sysid cant be compare also 'sysid' in inverted comma's we cant use it.

 

Thanks,
Ashutosh Munot