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

Sharique Azim
Mega Sage

i have two question and few observation:

  1. Question1: in the client script that you have provided in the comments, whats is the use of? if (window === null)
  2. Question2: is the field my_delegates  capable of storing list/array type values?
  3. Observation:why 

    var arr = answer.split('||'); was used although in the script include was not designed to return array separated in '||' format. Although you can use the line return delList.join('||');  for the said operation.

  4. can you declare and return another variable instead of delList  in  delList  =arrayUtil.unique(delList);  you can use the traditional method using another array variable to filter out the extra duplicate values.

patricklatella
Mega Sage

Hi everyone, thanks for the assistance.

So I've got it working with the following scripts...however if there happen to be 2 records on [sys_user_delegate] for the same user, then it breaks the g_form.setValue('my_delegates',arr); in the catalog client script.  My scripts below will populate the "my_delegates" variable (which is a list collector type variable) on the form in the portal, but only if there are no duplicate sysIDs returned from the script include.  

So what I need is for the script include to remove the duplicates, is this possible with this script?

Script Include:

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

getDelegates:function(){

var delList = '';

var user = this.getParameter('sysparm_user');
//gs.info('dels hello1 and user is '+user);
var delegates = new GlideRecord('sys_user_delegate');
delegates.addQuery('user',user);
delegates.query();

while(delegates.next()){
if(delList.length == 0){
delList = delegates.delegate+',';
}else{
delList = delList + delegates.delegate+',';
}
}
return delList; //need to remove duplicates from this array
}
});

 

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);
}
}
}

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);
}
}
}

glad that you got it, Even my solution was working 🙂 

Anyways 

Thanks

Siva

Thanks Siva for your help, definitely appreciate it!