0

Hey everyone i hope you can help me with a issue that i am having:

i use a function to get a list of groupmembers from a group. this is being returned to a variable. from this variable i need to see if my current user (current.sys.id) is in this list. if not do something if so do nothing. unfortunately my .search always returns -1 even if the record is in it.

i used the gs.log command to see diffrent outcomes and i see that the regexp is present in the var and even then it gives me a -1. can someone see what i am doing wrong?

here is my code

    var list = group_members();
    var check = new RegExp(current.sys_id);
    var control = list.search(check);
    if (control = -1) {
        set_group();
        gs.log("conf is :" + conf);
        gs.log("check is :" + check);
        gs.log("control is: " + control + "de lijst ziet er zo uit: " + list);
    } else {
        gs.log("check is :" + check);
        gs.log("control is: " + control + "de lijst ziet er zo uit: " + list);
    }
}

function group_members() {
    gs.log("functie check groep is aangeroepen");
    var answer = ' ';
    var group = "794ac672d4a301006027eb6da8731188";
    var group_mem = new GlideRecord('sys_user_grmember');
    group_mem.addQuery('group', group);
    group_mem.query();

    while (group_mem.next()) {
        if (answer.length > 0) {
            answer += (',' + group_mem.user.sys_id);
        } else {
            answer = group_mem.user.sys_id;
        }
    }
    return answer.toString();
}

function set_group() {
    gs.log("functie set_groep is aangeroepen");
    var rec1 = new GlideRecord('sys_user_grmember');
    rec1.initialize();
    rec1.user = current.sys_id;
    rec1.group.setDisplayValue('TFPP Users');
    rec1.insert();
    gs.log("groep is nu gezet");
}

thanks in advance.

Joren
  • 3,068
  • 25
  • 44
bas
  • 1
  • You're missing the 1st line (function declaration) – Joren Sep 09 '13 at 11:07
  • 1
    possible duplicate of [Javascript: Determine whether an array contains a value](http://stackoverflow.com/questions/1181575/javascript-determine-whether-an-array-contains-a-value) – Martijn Sep 09 '13 at 11:08
  • Why are you using a regex for an id? Better use plain string `indexOf`. – Bergi Sep 09 '13 at 11:08
  • Is `group_mem.query();` asynchronous? – Bergi Sep 09 '13 at 11:10
  • thanks joren but i do have the first line but the site doesnt show it the first line = var list=group_members(); so list will be the list generated from the function. – bas Sep 09 '13 at 11:41
  • does .search work with a string? Bergi? i thought it will only work with a regexp? for example if i want to search bas in the string bas is wrong i need to use /Bas/ regexp wright? – bas Sep 09 '13 at 11:43
  • @bas: Yes, it searches for a regexp. However, you *want* to search for a string: `.indexOf("Bas")` – Bergi Sep 09 '13 at 11:54
  • ok but if the string is a var? so lookup = current.sys_id then list.indexOf(lookup) will work? or do i need to convert it to a regexp: lookup2 = new RegExp(lookup); – bas Sep 09 '13 at 12:04
  • It doesn't matter what expression (variable, literal, something else) yields the string value. As I said, you do not need any regex. – Bergi Sep 09 '13 at 13:19

2 Answers2

1

Your question states that you're looking for current.sys.id, but your code uses current.sys __id. Is your problem just this typo?

As an aside, I would use indexOf rather than search, since you're not really using your regex that you're creating. Just do:

list.indexOf(current.sys.id);

And if it returns -1, it's because it's just not in there.

David Downes
  • 1,145
  • 10
  • 24
  • sorry i did do a typo in my first explination it needs to be current.sys_id not current.sys.id but the code only hase the current.sys_id so this isnt the solution. i changed to index of but no luck still keeps showing -1 var check = new RegExp(current.sys_id); var control = list.indexOf(check); – bas Sep 09 '13 at 11:51
  • what is the value of current.sys_id? are you sure it actually has a value? Because if it's null or undefined, that could be the why it's never found. Also, if you're not using a regex (you're not), then don't create a RegExp object. Just use list.indexOf(current.sys_id) – David Downes Sep 09 '13 at 12:08
  • current.sys_id is a value that is being generated by the system (servicenow) it is a script that runs when a new user is created. so the current.sys_id is dynamic. the value is not empty because i use the log to see if it is and it produces a value everytime. – bas Sep 09 '13 at 12:24
0

ok i had a night sleep and tried a diffrent approache:

in stead of returning a complete list of users i added the check in the function.

function group_members(){
    var answer = ' ';
    var group = "794ac672d4a301006027eb6da8731188";
    var group_mem = new GlideRecord('sys_user_grmember');
    group_mem.addQuery('group',group);
    group_mem.query();

        while(group_mem.next()){
            var user = current.sys_id;
            if (user == group_mem.user.sys_id){
                return ("1")
            }
        }
    }

now if my current user exists in the list it will return 1 otherwise it will be undefined. This will remove the whole search part. Thanks for the replies.

bas
  • 1