Check for unique match possible via indexOf()?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:23 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:28 AM
You'll need to do this with a Regular Expression.
This thread may be helpful.
regex - Matching exactly one occurance in a string with a regular expression - Stack Overflow
FWIW, we also covered regular expressions on episodes 31 and 32 of TechNow.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:42 AM
That sounds to complicated to be true. Or I do not get it.
I need to know if the opened_by is part of exactly ONE group that matches a specific type ...
Script Include ...
...
var getout = 'false';
var relType = 'TESTING';
// check all group memberships of user
var gr=new GlideRecord('sys_user_grmember');
gr.addQuery('user',opened_by);
gr.query();
while(gr.next())
{
if(getout)
break;
// get all types of the mebership groups
var types=gr.group.type.getDisplayValue();
var array = types.split(",");
// check if EXACTLY on group is found with the relType
if (types.indexOf(relType) == 1) {
grp_sys_id = gr.group;
getout=true;
break;
}
...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:47 AM
It seems like you are looking for a count of exactly 1 where user=opened_by and group is anything, correct? If that's the case, then I recommend using GlideAggregate (for performance reasons.)
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.query();
var count = 0;
if (ga.next()) {
count = ga.getAggregate('COUNT');
if (count == 1) {
// There's your answer
} else {
// user belongs to 0, 2 or more groups
}
}
// User belongs to no groups or GlideAggregate didn't return right.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:55 AM
that sounds logical. Thank you.
Could you please tell me additionally how I add the check for the relevant group type?
Need to know if groupmember in ONE group of specific type ... THANK YOU