Check for unique match possible via indexOf()?

Zod
Giga Guru

Hi,

I'm using a indexOf Function, but need to know if there is exactly only one match?

Something like

if (variable.indexOf(key) == 1) {

was what came into my mind ... but it does not work ;-(

Could you please help out?

Thank you

17 REPLIES 17

Add the query and you've got it. You can dot-walk in the query.



e.g.



ga.addQuery('group.type.name', 'Approval');


😉 .. sure.



Seems to be ok - just this way I'm not able to give back the group sys_id ... as this would be the outcome required. If found once - give back sys_id of group.


Any idea how to add this properly?



 


Thank you for the clarification. It would have been helpful to know that up front. GlideAggregate does counts, sums, avg, min, and max, but doesn't contain detail about the exact records found. It relies on the database functionality to get the values that it does.



A bit of a wrinkle. I just noticed that the group "type" field is a list field and not a reference field. That means the above addQuery() I gave you won't work. You would need to know the sys_id of the group type and do something like this:



If you need that information, you can do the same query to GlideRecord to get that group ID.



var groupID = '';


var ga = new GlideAggregate('sys_user_grmember');


ga.addAggregate('COUNT');


ga.addQuery('user', opened_by); // be sure this variable has a real value like current.getValue('opened_by')


ga.addQuery('group.type', 'LIKE', 'SYS_ID_OF_GROUP_TYPE');


ga.query();



var count = 0;


if (ga.next()) {


        count = ga.getAggregate('COUNT');


        if (count == 1) {


                  var grm = new GlideRecord('sys_user_grmember');


                  grm.addQuery('user', opened_by);


                  grm.addQuery('group.type', 'LIKE', 'SYS_ID_OF_GROUP_TYPE');


                  grm,query();


                  if (grm.next()) {


                            groupID = grm.getValue('group'); // Here's the sys_id of the one and only group the user belongs to


                  }


        } else {


                  // user belongs to 0, 2 or more groups


        }


}


Very close 😉



What it still misses is that a group might have more than one type assigned. This group will be ignored.


The rest works.


This line says "Does the group contain the sys_id of the type indicated."



grm.addQuery('group.type', 'LIKE', 'SYS_ID_OF_GROUP_TYPE');



For example, if type has approval in it somewhere, then it will match. It doesn't matter if approval is the only type or one of several.