How to show fields based on user criteria

Indira8
Kilo Sage

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

3 REPLIES 3

harun_isakovic
Mega Guru

You could also use an ACL to achieve this or a UI Policy (with Run script enabled).

Saurav11
Kilo Patron
Kilo Patron
Hello Indira, Yiu need to wite a onload catalog client script and script include In the catalog clinet script get the logged in user using g_user.userID(); Pass it to tye script include In the script include query that record and check if the user a manager of the group if yse pass back true in client script Then in clinet acript add If returned true g_form.setVisible(fieldname,true) Else g_form.setVisible(fieldname,false) Please mark answer correct/helpful based on impact.

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