How to check if a record is existing in a custom table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2022 11:25 PM
Hi All,
We have a custom table where there are 3 reference fields dep, owner and escalation which refer to the user table.
Requirement is that whenever logged in user tries to access the catalog item , if user is a part of any of these then certain variables should be visible otherwise no.
Could you help me with the code for querying the custom table and checking if the logged in user record is present in any of the fields.
Thank you
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 12:10 AM
Hi,
you can use onLoad catalog client script and use GlideAjax
In the script include check if logged in user is present in any of those fields
Script Include: It should be client callable
I added gr.hasNext()
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRecordPresent: function(){
var user = gs.getUserID();
var gr = new GlideRecord('u_custom_table');
gr.addQuery('u_dep', user).addOrCondition('u_owner', user).addOrCondition('u_escalation', user);
gr.setLimit(1);
gr.query();
return gr.hasNext(); // added this line
},
type: 'checkRecords'
});
Client Script: onLoad; UI Type - ALL
function onLoad(){
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.getXMLAnswer(function(answer){
if(answer.toString() != 'true'){
// hide variables you want
}
});
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 01:00 AM
hi
Thank you for the quick help, however I am not able to hide the fields.
I have created a client script for auto populating the user details and also to check user is present as follows:
Script include :
var FillRequestorDetails = Class.create();
FillRequestorDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var result = this.newItem("result");
var logged_user = this.getParameter("sysparm_user");
var user_detail = new GlideRecord('sys_user');
if (user_detail.get(logged_user)) {
result.setAttribute("user_name", user_detail.user_name);
result.setAttribute("name", user_detail.name);
}
},
checkRecordPresent: function(){
var userid = gs.getUserID();
var gr = new GlideRecord('custom_table');
gr.addQuery('u_escalation_contact', userid).addOrCondition('u_group_owner', userid);
gr.setLimit(1);
gr.query();
},
type: 'FillRequestorDetails'
});
Catalog client script on load is as follows:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('FillRequestorDetails');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.getXMLAnswer(function(answer){
if(answer.toString() != 'true'){
g_form.setVisible('escalation_contact',false);
g_form.setVisible('group_id',false);
g_form.setVisible('group_owner',false);
g_form.setVisible('deputy',false);
}
});
}
However the fields are not hidden
Could you please let me know where the issue is.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 01:07 AM
I updated my earlier comment and added this line as it was missing from my side
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRecordPresent: function(){
var user = gs.getUserID();
var gr = new GlideRecord('u_custom_table');
gr.addQuery('u_dep', user).addOrCondition('u_owner', user).addOrCondition('u_escalation', user);
gr.setLimit(1);
gr.query();
return gr.hasNext();
},
type: 'checkRecords'
});
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 01:20 AM
Hi
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 01:44 AM
what came in alert for answer?
Did you give correct table name, correct field name to query?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader