Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trying to query list collector field on Incident form

alhicks
Tera Guru

I'm trying to change our Assignment Based workload script to query a list collector field instead of our select box field.   Tried several times (script below) but it's not working...:(

var assignTo = getLowestUser();

//gs.addInfoMessage("assigning to is " + assignTo);

current.assigned_to = assignTo;

function getLowestUser() {

var userList = new Array();

var cg = new GlideRecord('sys_user_grmember');

cg.addQuery('group', current.assignment_group);

if (current.assignment_group.getDisplayValue() == 'SD - Collaboration/Mobile') {

//cg.addQuery('user.u_service_desk_workload', 'SD-Collaboration/Mobile');     HERE'S THE PREVIOUS DROP DOWN SELECT FIELD

NOW TRYING TO QUERY A LIST FIELD.   IF THE LIST COLLECTOR CONTAINS THIS ASSIGNMENT GROUP.

//cg.addQuery('user.u_service_dsk_workload', 'LIKE', '591f507cf07d4d0c0f323fc297cb7d10');  

//cg.addOrCondition('user.u_service_dsk_workload', 'CONTAINS', '591f507cf07d4d0c0f323fc297cb7d10');  

//cg.addQuery(user.u_service_dsk_workload.toString().indexOf("591f507cf07d4d0c0f323fc297cb7d10") >-1);

cg.query();

while(cg.next()) {

var tech = cg.user.toString();

                                                                  TRIED TO QUERY HERE.

//if (cg.user.u_service_dsk_workload.toString().indexOf("591f507cf07d4d0c0f323fc297cb7d10") >-1){

//if (cg.user.u_service_dsk_workload, 'LIKE', '591f507cf07d4d0c0f323fc297cb7d10'){

var cnt = countTickets(tech);

//gs.addInfoMessage("Tech counts " + cg.user.name + ' ' + cnt + " " + tech);

userList.push( { sys_id: tech, name: cg.user.name, count: cnt } );

}

for(var i=0; i < userList.length; i++) {

//gs.addInfoMessage(userList.sys_id + " " + userList.name + " " + userList.count);

}

userList.sort(function(a, b) {

//gs.addInfoMessage("Sorting: " + a.sys_id + "(" + a.count + "); " + b.sys_id + "(" + a.count + ")");

return a.count - b.count; });

if (userList.length <= 0)

return "";

return userList[0].sys_id;

}

function countTickets(tech){

var ct = new GlideRecord('incident');

ct.addQuery('assigned_to',tech);

ct.addQuery('active',true);

ct.query();

return ct.getRowCount();

}

}

1 ACCEPTED SOLUTION

Hello Andrea,


I think the problem (if I'm understanding the desired outcome correctly) might lie in both these lines:


cg.addQuery('user.u_service_dsk_workload', 'CONTAINS',   '591f507cf07d4d0c0f323fc297cb7d10');



cg.addQuery(user.u_service_dsk_workload.toString().indexOf("591f507cf07d4d0c0f323fc297cb7d10") > -1);



Am I understanding correctly that you only want members that are just in this group?   If so, I think the first query might just want to do a straight equals comparison by removing the 'CONTAINS' parameter, so just cg.addQuery('user.u_service_dsk_workload',   '591f507cf07d4d0c0f323fc297cb7d10');



It looks like the 2nd query is doing the same thing as the first from what I can see, so it could probably just be removed.   Also, if I'm incorrect in my understanding and you do want to you use the 'CONTAINS' parameter, I might still remove the 2nd line as it seems a bit redundant.   I also think for the 2nd line if you do want to use it, you would want to split that comparison out into 3 comparison parameters.   For instance:   cg.addQuery(user.u_service_dsk_workload.toString().indexOf("591f507cf07d4d0c0f323fc297cb7d10"), '>', -1);



Let me know if trying either of these changes produces the desired results.


View solution in original post

17 REPLIES 17

bcronrath
Kilo Guru

Hello Andrea,



Out of curiosity when you had your debugging glidesystem messages being written, did it point to the area in the script that is not working as expected?   If so what was the output versus what you would expect it to be?


It's assigning to a user that has a group in this field but it's not the group I'm querying.       For example:   right now for testing, I have four service desk users with a group in the service dsk workload.   Three of them in this group and one in another.     It's assigning to the one person that is not in this group I'm looking for.   If I take that group name out of the list box for that one person, then the incident doesn't assign at all, not even to one of the other three.




I did get the undefined message on the gs.addInfoMessage(userList.sys_id + " " + userList.name + " " + userList.count);. The other messages just listed the assigned to persons name and sys id.




undefined undefined undefined






var assignTo = getLowestUser();


//gs.addInfoMessage("assigning to is " + assignTo);


current.assigned_to = assignTo;



function getLowestUser() {


var userList = new Array();


var cg = new GlideRecord('sys_user_grmember');


cg.addQuery('group', current.assignment_group);


if (current.assignment_group.getDisplayValue() == 'SD - Collaboration/Mobile') {


cg.addQuery('user.u_service_dsk_workload', 'IN',   '591f507cf07d4d0c0f323fc297cb7d10');


//cg.addQuery('user.sys_user.u_service_dsk_workload', 'IN',   '591f507cf07d4d0c0f323fc297cb7d10');


//cg.addQuery('user.u_service_desk_workload', 'SD-Collaboration/Mobile');


cg.query();


while(cg.next()) {


var tech = cg.user.toString();


var cnt = countTickets(tech);


//gs.addInfoMessage("Tech counts " + cg.user.name + ' ' + cnt + " " + tech);


userList.push( { sys_id: tech, name: cg.user.name, count: cnt } );


}


for(var i=0; i < userList.length; i++) {


gs.addInfoMessage(userList.sys_id + " " + userList.name + " " + userList.count);   //UNDEFINED HERE.


}


userList.sort(function(a, b) {


//gs.addInfoMessage("Sorting: " + a.sys_id + "(" + a.count + "); " + b.sys_id + "(" + a.count + ")");


return a.count - b.count; });



if (userList.length <= 0)


return "";


return userList[0].sys_id;


}



function countTickets(tech){


var ct = new GlideRecord('incident');


ct.addQuery('assigned_to',tech);


ct.addQuery('active',true);


ct.query();


return ct.getRowCount();


             


}


}




Hmm so if I'm understanding this correctly, on that early debug line:


gs.addInfoMessage("Tech counts " + cg.user.name + ' ' + cnt + " " + tech);




Are you saying that this gives usernames that should not belong to the criteria defined by:




cg.addQuery('group', current.assignment_group);


cg.addQuery('user.u_service_dsk_workload', 'IN',   '591f507cf07d4d0c0f323fc297cb7d10');




If so, I might try to just eliminate the rest of the script (in a copy of the script) and just focus on this first query, and try to output the value of the assignment_group and u_service_dsk_workload to make sure it isn't doing something weird like returning groups outside of what you are querying.   If it is, there might be something it doesn't like about the query.   I do notice that 2nd query is using an "IN" so perhaps it is including users from other groups that also match on this string.




Best regards,


Brian


It's gs.addInfoMessage(userList.sys_id + " " + userList.name + " " + userList.count);     It comes back with undefined listed 3 times, I have three people in this group.   So, it seems to be bringing back an undefined for those three in that group.


Hello Andrea,



This could be valid as I'm not entirely sure what javascript can handle - but one thing that stands out to me in that for loop is there is no index being specified while iterating through the array.   For example, do you get different results if you instead used gs.addInfoMessage(userList[i].sys_id + " " + userList[i].name + " " + userList[i].count) ?