The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to query reference field??

priyanayak
Kilo Contributor

HI,

I have created a table, where one column is reference to sys_user. I have added 2 users data to it. I want to query that table to check if particular user is present in that table or not.

var submittedBy = gs.getUser();

var leave_balance = new GlideRecord('u_leave_balance');

Here submittedBy is a user. and i want to query my table u_leave_balance to check if that user is present in that or not. What will be my addQuery condition??Untitled.png

7 REPLIES 7

neetusingh
Giga Guru

I hope this would help --



var leave_balance = new GlideRecord('u_leave_balance');


leave_balance.addQuery('u_user',gs.getUserID()); // gs.getUserID() returns the sys_id of the current logged in user and assuming 'u_user' is the database name of the field -                                                                                                                                                     "user" present on the leave balance form.


leave_balance.query();


if(leave_balance.next())


gs.addInfoMessage("User Exists in the Leave Balance table");


else


gs.addInfoMessage("User doesn't exist in the Leave Balance table");


Thank u soo much neetu. leave_balance.addQuery('u_user',gs.getUserID()); This is the line i wanted . It helped me.


I am glad it worked for you.


If you don't mind, please mark the response as helpful/correct.


Gurpreet07
Mega Sage

Don't know what you are trying to do. I think you are trying to apply a validation to prevent duplicate entry for a user in the table u_leave_balance. If so then you could create a before insert Business Rule on u_leave_balance table and use following code in Script.


function onBefore(){


var gr = new GlideRecord('u_leave_balance');


if(gr.get(current.u_user)){             // backend name of user


current.setAbortAction(true);


gs.addErrorMessage('User Already Exist');


}


}