how to make field mandatory for particular role,or users

SV17
Tera Contributor

hii all,

my requirement is 

in the company table, employee field is non-mandatory only for the program manager.

how to make this

4 REPLIES 4

Aman Kumar S
Kilo Patron

You have to functions which you can use:

gs.getUserID() // this will give you the sys_id of the logged in user to compare

gs.hasRole('itil')// this will return true or false if user has itil role or not for logged in user

 

Using these conditions you can check in onload client script to handle such aspects to make a field mandatory or read only or visible.

Ex:

if(gs.hasRole('itil')){

 g_form.setMandatory("priority",true);

}

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions šŸ™‚

Best Regards
Aman Kumar

palanikumar
Giga Sage

Hope Program Manager is title or role of the user.

You can create a Client Script and check whether current user is program manager. Set the field mandatory if true.

Thank you,

Palani

Thank you,
Palani

SV17
Tera Contributor

can you help me with the script

You can use the script. This is based on assumption that title field in user record has Program Manager to differentiate whether user is program manager or not.

Refer the inline comments to make any changes

function onLoad() {
    var user_ajax = new GlideAjax('RecordFieldGetter');
    user_ajax.addParam('sysparm_name', 'getValue');
    user_ajax.addParam('sysparm_table', 'sys_user');
    user_ajax.addParam('sysparm_sys_id', g_user.userID);
    user_ajax.addParam('sysparm_field_name', 'title'); // Update title to actual field that has the user title
    user_ajax.getXML(returnAnswer);
}

function returnAnswer(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if(answer.toLowerCase() == 'program manager'){ // Replace program manager with actual name used in table
		g_form.setMandatory('name',false); // replace name with actual field name for employee number
	}
	else{
		g_form.setMandatory('name',true); // replace name with actual field name for employee number
	}
}
Thank you,
Palani