How to query reference field??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2015 11:47 PM
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2015 12:07 AM
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");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2015 03:00 AM
Thank u soo much neetu. leave_balance.addQuery('u_user',gs.getUserID()); This is the line i wanted . It helped me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2015 03:50 AM
I am glad it worked for you.
If you don't mind, please mark the response as helpful/correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2015 12:18 AM
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');
}
}