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

Santosh_Ksagar
Mega Sage
Mega Sage

Hi Patrick,

You can use below syntax to remove duplicates:-

 var  names = ['D', 'B', 'A', 'A', 'C', 'A'];
 var  unique = [...new Set(names)];
 console.log(unique); 
 // Output---> ["D", "B", "A", "C"]

It's working correctly for me.

Mark Answer as Correct/Helpful.

Regards

Santosh Kshirsagar

find_real_file.png

www.dxsherpa.com

Hi Santosh,

not sure how to use that syntax with my script?

patricklatella
Mega Sage

so I've added a gs.info to see what the array ends up being before being returned, and it is showing correctly in the system log.  However the client script is taking the return and not reading it properly.

 

find_real_file.png

 

but here's what the alert is showing as see in my last post:

 

find_real_file.png

 

 

so the client script is messing up I believe

Hey patrick , 

I tried to use logged in user id instead of the field names which u used in your code please have a look at this below and let me know if it works for you. It worked for me 

On Load Client Script:

------------------------

function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_user.userID;
var gr = new GlideAjax('fetchUniqueDelegates');
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");
g_form.addInfoMessage('answer is '+answer);
if(answer == '' || answer == undefined){
return false;
}else{
var finalAns = JSON.parse(answer);
g_form.addInfoMessage(finalAns);
for(var i = 0; i<finalAns.length;i++){
g_form.setValue('watch_list',finalAns[i]);
}
}
}
}

Script Include : 

-----------------

var fetchUniqueDelegates = Class.create();
fetchUniqueDelegates.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.toString(); //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 JSON.stringify(delList);
},

type: 'fetchUniqueDelegates'
});

 

Hope this helps. 

Mark this response as correct if that really helps. 

 

Thanks,

Siva

poyntzj
Kilo Sage

There are a couple of ways to do this including constructing an XML (there is an old Fruition blog on that somewhere)

Personally, I do the above and then I simply JSON.stringify the data and return that

My client side then parses the JSON and goes from there

as mentioned elsewhere, I would also change your

delList.push(dels)

to be

delList.push(dels.toString())  or delList.push(dels+'')