How to build an array with GlideRecord and remove duplicates before returning to catalog client script?

patricklatella
Mega Sage

Hi all,

finding this oddly frustrating...I've got a GlideAjax calling a script include to look up the sys_user_delegate table and return values based on the GlideRecord to populate a read only field on a form, and remove duplicates before sending back.

anyone see what's wrong with this script?  thanks!

Script include:

var AjaxGetDelegates = Class.create();
AjaxGetDelegates.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDelegates:function(){

//var delList = [];

var user = this.getParameter('sysparm_user');

var delegates = new GlideRecord('sys_user_delegate');
delegates.addQuery('user',user);
delegates.query();

var delList = [];

while(delegates.next()){
var dels = delegates.delegate; //field on the delegate table I want to return
delList.push(dels);
//delList = delList + delegates.delegate+',';
}
var arrayUtil = new ArrayUtil();
delList = arrayUtil.unique(delList);
return delList;
}
});

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

Figured it out...got some internal help and got the solution by creating an object and then building the array off the key, see script include below.  This then removes the duplicates before returning the array to the client script.  works perfectly to populate a list collector type field on the form and accommodates for duplicate records on the delegate table.

 

script include:

var AjaxGetDelegates = Class.create();
AjaxGetDelegates.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDelegates:function(){

var delList = '';
var tempList = {};

var user = this.getParameter('sysparm_user');

var delegates = new GlideRecord('sys_user_delegate');
delegates.addQuery('user',user);
delegates.query();

while(delegates.next()){
tempList[delegates.delegate] = true;
}

for(var key in tempList) {
delList += key + ',';
}
return delList;
}
});

 

catalog client script:

function onLoad() {

//look up sys_user_delegate table and populate "my_delegates" field


var user = g_form.getValue('current_user');

var gr = new GlideAjax('AjaxGetDelegates');
gr.addParam('sysparm_name','getDelegates');
gr.addParam('sysparm_user',user);
gr.getXML(setDelegates);

//populate the my_delegates field
function setDelegates(response){
var answer = response.responseXML.documentElement.getAttribute("answer");

//alert('answer is '+answer);
if(answer == '' || answer == undefined){
return false;
}else{
var arr = answer.split('||');
g_form.setValue('my_delegates',arr);
}
}
}

View solution in original post

19 REPLIES 19

siva_
Giga Guru

If you can even mention your client script code that would be a bit more helpful 

 

Thanks,

Siva

Hi Siva,

here's the client script:

 

function onLoad() {

//look up sys_user_delegate table and populate "my_delegates" field

var user = g_form.getValue('current_user');

var gr = new GlideAjax('AjaxGetDelegates');
gr.addParam('sysparm_name','getDelegates');
gr.addParam('sysparm_user',user);
gr.getXML(setDelegates);

//populate the my_delegates field
function setDelegates(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('answer is '+answer);
if(answer == '' || answer == undefined){
return false;
}else{
var arr = answer.split('||');

if (window === null)

g_form.setValue('my_delegates',arr);
}
}

siva_
Giga Guru

Just after running an initial run in Background script i think the issue you are facing is getting a single sys id in your result if i am not wrong 

Just modify your code a little bit as below 

var dels = delegates.delegate.toString(); //field on the delegate table I want to return 

and that should work

 

i have run this in my background script and its working 

var delList = [];

while(delegates.next()){
var dels = delegates.delegate; //field on the delegate table I want to return
delList.push(dels);
//delList = delList + delegates.delegate+',';
}
var arrayUtil = new ArrayUtil();
delList = arrayUtil.unique(delList);
return delList;
}

 

Hope this helps 

Mark this response as correct if that resolves your query 

Thanks,

Siva

patricklatella
Mega Sage

so with a gs.info log I'm seeing the correct array get built, but this is what the alert in the client script is showing.  Odd???

 

find_real_file.png