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.

9
  • You're missing the 1st line (function declaration) Commented Sep 9, 2013 at 11:07
  • 1
    possible duplicate of Javascript: Determine whether an array contains a value Commented Sep 9, 2013 at 11:08
  • Why are you using a regex for an id? Better use plain string indexOf. Commented Sep 9, 2013 at 11:08
  • Is group_mem.query(); asynchronous? Commented Sep 9, 2013 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. Commented Sep 9, 2013 at 11:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.