- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 01:04 AM
Hello
In a service catalog item, I would like to make a field mandatory (or not) depending on the requested for’s country.
The problem of course is when displaying the formular of the item, we don’t know yet who is the requested for so I decided to make it dependent of the logged in user’s country.
I want then to write a catalog UI policy in which I would do the test if logged in user is from Country France, then variable not mandatory. If from other country, then let the variable mandatory.
How can I get the logged in user’s country from my catalog UI policy ?
Is there another way to manage my topic ?
Regards
Vanessa Heux
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 01:52 AM
Hi Vanessa,
As Kalai said, you can use an onLoad/onChange Client script which should call a Script Include to get logged in user's information.
I got one similar requirement where I have wrote an onLoad Client Script and called a script include through it.
Refer below code which will help you.
Client Script : AJ Auto-Populate Requestors fields
Type : onLoad
Applies to : Variable set // this variable set contains variables which stores requestor's details.
Script :
function onLoad()
{
var ga = new GlideAjax("requestor_details");
ga.addParam("sysparm_name","requestor_info");
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
var phone = result[0].getAttribute("phone");
var user = result[0].getAttribute("user");
var email = result[0].getAttribute("email");
var manager = result[0].getAttribute("manager");
var company = result[0].getAttribute("company");
var location = result[0].getAttribute("location");
//Populate variables
g_form.setValue('phone',phone);
g_form.setValue('requested_by',user);
g_form.setValue('email',email);
g_form.setValue('company',company);
g_form.setValue('location',location);
g_form.setValue('manager',manager);
g_form.setReadOnly('email',true);
g_form.setReadOnly('phone',true);
g_form.setReadOnly('requested_by',true);
g_form.setReadOnly('manager',true);
g_form.setReadOnly('company',true);
g_form.setReadOnly("phone_number",true);
}
}
Script Include : requestor_details
Client Callable : true
Script :
var requestor_details = Class.create();
requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var result = this.newItem("result");
var logged_user = gs.getUserID();
var user_detail = new GlideRecord('sys_user');
user_detail.addQuery('sys_id',logged_user);
user_detail.query();
while(user_detail.next())
{
result.setAttribute("user",user_detail.sys_id);
result.setAttribute("phone", user_detail.phone);
result.setAttribute("email",user_detail.email);
result.setAttribute("company",user_detail.company);
result.setAttribute("manager",user_detail.manager);
result.setAttribute("location",user_detail.location.getDisplayValue());
}
},
type: 'requestor_details'
});
Mark if Correct/Helpful
Regards,
Ajay

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 01:17 AM
Use an onload or an oonchange script instead of UI policy.
Do you have requester variable on the form ? If yes, you can either use ajax call or getReference on the variable.
In case you do not have any such variable on the form, then g_user.userID will return the sys ID of the current user. Use it via glideajax to query can get the country information,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 01:52 AM
Hi Vanessa,
As Kalai said, you can use an onLoad/onChange Client script which should call a Script Include to get logged in user's information.
I got one similar requirement where I have wrote an onLoad Client Script and called a script include through it.
Refer below code which will help you.
Client Script : AJ Auto-Populate Requestors fields
Type : onLoad
Applies to : Variable set // this variable set contains variables which stores requestor's details.
Script :
function onLoad()
{
var ga = new GlideAjax("requestor_details");
ga.addParam("sysparm_name","requestor_info");
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
var phone = result[0].getAttribute("phone");
var user = result[0].getAttribute("user");
var email = result[0].getAttribute("email");
var manager = result[0].getAttribute("manager");
var company = result[0].getAttribute("company");
var location = result[0].getAttribute("location");
//Populate variables
g_form.setValue('phone',phone);
g_form.setValue('requested_by',user);
g_form.setValue('email',email);
g_form.setValue('company',company);
g_form.setValue('location',location);
g_form.setValue('manager',manager);
g_form.setReadOnly('email',true);
g_form.setReadOnly('phone',true);
g_form.setReadOnly('requested_by',true);
g_form.setReadOnly('manager',true);
g_form.setReadOnly('company',true);
g_form.setReadOnly("phone_number",true);
}
}
Script Include : requestor_details
Client Callable : true
Script :
var requestor_details = Class.create();
requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
requestor_info: function() {
var result = this.newItem("result");
var logged_user = gs.getUserID();
var user_detail = new GlideRecord('sys_user');
user_detail.addQuery('sys_id',logged_user);
user_detail.query();
while(user_detail.next())
{
result.setAttribute("user",user_detail.sys_id);
result.setAttribute("phone", user_detail.phone);
result.setAttribute("email",user_detail.email);
result.setAttribute("company",user_detail.company);
result.setAttribute("manager",user_detail.manager);
result.setAttribute("location",user_detail.location.getDisplayValue());
}
},
type: 'requestor_details'
});
Mark if Correct/Helpful
Regards,
Ajay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 02:27 AM
Hello
I tried what you said Ajaykumar but I still do not get the loggedInuser Information.
Script Include "LoggedIn_UserDetails" client callable
var LoggedIn_UserDetails = Class.create();
LoggedIn_UserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
loggedIn_UserInfo: function() {
var result = this.newItem("result");
var logged_user = gs.getUserID();
var user_detail = new GlideRecord('sys_user');
user_detail.addQuery('sys_id',logged_user);
user_detail.query();
while(user_detail.next())
{
result.setAttribute("country", user_detail.country);
}
},
type: 'LoggedIn_UserDetails'
});
and catalog client script
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax("LoggedIn_UserDetails");
ga.addParam("sysparm_name","loggedIn_UserInfo");
ga.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
var country = result[0].getAttribute("country");
//Populate variables
alert (' alert message' + country);
if (country == 'a9fdbade18eb8000db5686f0e00a5c82')
{g_form.setMandatory('cost_center',false); }
}
}
To test, I can see that my alert returns only "alert message" and that's all which means country is not filled.
And in my test, the logged in user is myself and I know I have a country filled.
Do you have an idea why it iw not working ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-22-2018 02:37 AM