Checking if reference field has any selections in it

codedude
Mega Expert

I have 2 reference fields, one is requester the other is requester group(s). The requester group gets filled with the requester's group(s). It is currently filling them with no problem, what I would like it to is to throw an alert to the window if the requester group(s) reference field does not any selections in it to select. In other words, if the requester currently is not in any groups, throw the message to the window, else do nothing....


This is the current scripting I have in my workflow in Additional Approvers Script section :


answer = [];  

var approvers = new GlideRecord('sys_user_grmember');  

approvers.addQuery('group', current.u_requestor_group.sys_id); //Assumption: u_requestor_group is not null

approvers.query();  

while(approvers.next()) {  

    if(approvers.user.toString() != current.requested_by.toString())

    {  

          answer.push(approvers.user.toString());  

    }  

}

Can anyone help me with this?

1 ACCEPTED SOLUTION

I think you need the users sys_id


try


var uID =   g_user.userID;



As the 'sys_user_grmember' tables user field is a reference field to the sys_user table


View solution in original post

8 REPLIES 8

Deepak Ingale1
Mega Sage

answer = [];  


var approvers = new GlideRecord('sys_user_grmember');  


approvers.addQuery('group', current.u_requestor_group.sys_id); //Assumption: u_requestor_group is not null


approvers.query();  


while(approvers.next()) {  


    if(approvers.user.toString() != current.requested_by.toString())


    {  


          answer.push(approvers.user.toString());  


    }  


}


if(answer.length == 0) // array length of zero indicates user is not in a single group


{


// you can populate message here


}


This can go in the workflow, doesn't have to be a client side script does it?


Hi Josh,


Workflow scripting is server side so if you want to display some error message to end user, then it wont help.


You will have to write down either onChange or onSubmit catalog client script ( I assume you are working on catalog item) or onChange / onSubmit client script.



In that case, you will have to check if your variable / field does not contain a null value or it is not empty. If it is you can simply throw error or warning or alert message.


Hi Deepak,



I ended up writing an onLoad client side script because I saw that the message would not be displayed until the change had been submitted. I need the message to appear on page load, so I need to check if the user is in any groups as the page is loading. End product is that the user can not submit a change ticket if they are not in a group. I scripted what I have below and it is messing up at the beginning of the while loop :



function onLoad() {


    //Get the username of the current user


    var uID =   g_user.userName;


    //Get all active groups where user is a member


    var grmember = new GlideRecord('sys_user_grmember');


    grmember.addQuery('user', uID);


    grmember.addQuery('group.active', true);


    grmember.query();


    while(grmember.next()){


    alert('User is in group(s)');


    }  


}



Any suggestions?