Checking group membership via client script

YenGar
Mega Sage

Hi all,

I am trying to check a user's group membership using a client script but I am not having any luck... below is a screen shot of what I have written but it is not working. Could some one please point me in the right direction to achieve this.

onChange client script

table: incident

field name: caller

script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

//Checks to see if user is a member of the "App-SNIncident-StoreUser".

//If true, "is a Store" field is marked as true.   Used for determing what CIs to show in the incident form.

var ourUser = g_form.getValue('caller_id');

//var group = 'ec00d5fd6fff11006bceaf512e3ee4a7';

//var caller = ourUser;

var storeUser = ourUser.isMemberOf('ec00d5fd6fff11006bceaf512e3ee4a7');   //Storeuser

if(storeUser){

    g_form.setValue('u_is_a_store',true);

} else{

      g_form.setValue('u_is_a_store', false);

}

}

I keep getting this error message saying   'isMemberOf' is not a function, but I am not sure what other function to use... I need to be able to check the caller's group membership if it changes to check and uncheck the 'is a store' field... any ideas? Please help!!

store.PNG

Thank you,

Yeny.

1 ACCEPTED SOLUTION

Ken83
Mega Guru

It looks like you are trying to utilize .isMemberOf which is a method only available to the Server-side gs object. I think you need to just run an it in a GlideRecord



var person = g_form.getValue('caller_id');


var ir = new GlideRecord('sys_user_grmember');


ir.addQuery('group', 'App-SNIncident-StoreUser');


ir.addQuery('user', person);


ir.query();


if(ir.next()){


g_form.setValue('u_is_a_store', true);


}else{


g_form.setValue('u_is_a_store', false);


}


}


View solution in original post

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Yeny,



You have to run this at server side.You can do the glideajax and pass the parameters to get the output.


http://wiki.servicenow.com/index.php?title=GlideAjax


Ken83
Mega Guru

It looks like you are trying to utilize .isMemberOf which is a method only available to the Server-side gs object. I think you need to just run an it in a GlideRecord



var person = g_form.getValue('caller_id');


var ir = new GlideRecord('sys_user_grmember');


ir.addQuery('group', 'App-SNIncident-StoreUser');


ir.addQuery('user', person);


ir.query();


if(ir.next()){


g_form.setValue('u_is_a_store', true);


}else{


g_form.setValue('u_is_a_store', false);


}


}


Thank you so much Kenneth, this worked for me to achieve what I needed to do. It wasn't working at first but I changed the group to be the sys_id instead of the display name, shown below:


script:


var person = g_form.getValue('caller_id');


var ir = new GlideRecord('sys_user_grmember');


ir.addQuery('group', 'ec00d5fd6fff11006bceaf512e3ee4a7');


ir.addQuery('user', person);


ir.query();


if(ir.next()){


g_form.setValue('u_is_a_store', true);



}


      else{


g_form.setValue('u_is_a_store', false);


}


}



Do you think I can query the CMDB based on this script to only show specific CIs when the 'is a store' button is selected. If so, how can I do that? Should I do a GlideRecord in the client script... I've tried using a script include but I can't use a dictionary override for the same table (incident) on the cmdb_ci field... Do you have any thoughts on that?


Please let me know, thank you so much for your help!



Yeny


Hi Yeny,



You can use the reference qualifier to restrict the data as per the condition.Here is the more info on it.


http://wiki.servicenow.com/index.php?title=Reference_Qualifiers