How to show fields based on user criteria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-12-2022 09:44 AM
Hi All,
We have a custom table which contains details of groups from external source. There is a catalog item to modify these groups. If the logged in user is either an owner or deputy of the group, then certain options should be visible on the form otherwise they should be hidden. How do we write the client script for the same.
The fields owner and deputy are user reference fields.
Thanks you
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-13-2022 10:59 AM
You could also use an ACL to achieve this or a UI Policy (with Run script enabled).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-13-2022 11:09 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-19-2022 03:29 AM
Hi Saurav, I have given the following script but it is not working . Could you please let me know where I have written wrong
Client script (On load) - I have used the same client script for all on load conditions, can we do that ?
function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_form.getReference('requested_for_variable',populateInfo);
function populateInfo(user) {
g_form.setValue('email', user.email);
//g_form.setValue('manager', user.manager);
}
g_form.setDisplay('new_group_name', false);
g_form.setDisplay('group_name', false);
g_form.setDisplay('business_justification', false);
g_form.setDisplay('group_name', false);
g_form.setDisplay('group_id', false);
g_form.setDisplay('group_deputies', false);
g_form.setDisplay('group_owner', false);
g_form.setDisplay('escalation_contact', false);
g_form.setDisplay('deputy', false);
var ga = new GlideAjax('ChecktheRequestor');
ga.addParam('sysparm_name', "checkUserPresent");
ga.getXMLAnswer(function(answer){
if(answer.toString() != 'true'){
g_form.removeOption('request_for','update');
g_form.removeOption('request_for','Delete an Entitlement');
}
});
Script include:
var ChecktheRequestor = Class.create();
ChecktheRequestor.prototype = {
initialize: 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);
}
},
checkUserPresent: 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
},
checkRecordPresent: function(){
var myGroups = new GlideRecord('u_aps_entitlements');
var groups=[];
//var myUser = '687ac00c97f330105e69341e6253afe8';
var myUser = gs.getUser();
myGroups.addEncodedQuery('u_group_owner='+myUser+'^ORu_deputyLIKE'+myUser+'^ORu_escalation_contact=myUser');
myGroups.query();
while (myGroups.next()){
gs.log("Group Name is " + myGroups.u_group_name);
groups.push(myGroups.sys_id);
}
gs.log(groups);
return groups ;
},
type: 'ChecktheRequestor'
};
Thank you