How to get logged in user infomation from catalog service ui policy ?

vanessaheux
Tera Contributor

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

1 ACCEPTED SOLUTION

Ajaykumar1
Tera Guru

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

View solution in original post

14 REPLIES 14

Hi Vanessa,

Do you have the field named "country" on your user form..?

while(user_detail.next())
{
result.setAttribute("country", user_detail.country); // use location instead of country
}

 

Regards,
Ajay

You are right, it is u_country and not country.

I changed but that did not change anything.

 

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("u_country");

//Populate variables
alert (' alert message' + country);
if (country == 'a9fdbade18eb8000db5686f0e00a5c82')
{g_form.setMandatory('cost_center',false); }
}

}

 

and script include

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("u_country", user_detail.u_country);
}
},

type: 'LoggedIn_UserDetails'
});

Hi Vanessa,

In your script Include try u_country.getDisplayValue()

while(user_detail.next())
{
result.setAttribute("u_country", user_detail.u_country);
}

 

Can you tell me what's your alert (' alert message' + country); displaying ?

Hello 

 

I tried also with u_country.getDisplayValue() but that does not change anything.

Firthermore I'm testing the sysid so i don't think getDisplayValue is necessary.

 

The alert message only displays "alert message".

There is no "undefined" nor anything else.