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

Hi Pradeep,


I tried adding a script include to the attributes field in the 'incident' table dictionary override for the 'cmdb_ci' field... I don't think I am doing this correctly... Am I writing the script include correctly?



Script include:


function CheckIsMemberOfStore() {


    var a = current.u_is_a_store;


    //var b = current.cmdb_ci.sys_class_name;


  // return a query string


      if(a == false){


              return;


      }


      else if(a == true){


        // return applications that are marked as 'store applications' and hardware items that have a class of 'store hardware'


            return 'sys_class_name=u_cmdb_ci_store_hardware^ORref_cmdb_ci_appl.u_store_application=true';


              //^ORsys_class_name=cmdb_ci_application_software.u_store_application=true


      }


}



This is where I added the script include... not sure if this is the best way to do it...


dictionary override.PNG


Thank you Kenneth for helping me with this script!



Yeny


I tried this, but not all users have access to the sys_user_grmember table, so some would return false for "hasNext()", despite them being in a group. How should i fix that?


Thank you, I was struggling with this and an ACL was the answer for me.


Steven1
Tera Expert

GlideAjax makes it possible to have the client script call the server to retrieve that information in an efficient and effective manner. You can return simple or complex data types and then use that information on the client script. http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#gsc.tab=0