I need to create a variable set with dynamic variables, i.e., they should be autopopulated with the logged in user details like name, email and phone

AirSquire
Tera Guru

I have created a variable set "requestor Info". there are 3 variables, i.e., requestor's name, email ID and phone number as variables in it. I want to autopopulate these variables with the logged in user details correspondingly. How can I achieve it?

1 ACCEPTED SOLUTION

arnabwa
Giga Guru

Hi Dhruv,



This is easily achievable by just putting in the default value of each variable :


1. Variable "Requestor" : Right Click on the variable name >> Go to Configure variable >> Locate the "Default Value" tab >> put this line: "javascript:gs.getUserID();"



2. Variable "Email ID" : same as above process >> just put this line: "javascript:gs.getUser().getEmail();"



3. Variable "Phone number" : same as above process >> just put this line: "javascript:gs.getUser().getMobileNumber();"



You are Done here !!!



Thanks,


Arnab



Please mark Helpful / Correct according to the worthiness of my solution.


View solution in original post

4 REPLIES 4

michal29
Mega Guru

Hello Dhruv,



Best bet would be to query sys_user for the basic info like:



var user = top.window.NOW.user_id || top.window.NOW.user.userID


var gr = new GlideRecord('sys_user');


gr.get(user)



<your_variable> = gr.name


......


..


and so on.



But better would be, to have in that variable set 1 variable that would be user.


In this variable go and set default value to javascript:gs.getUserID();



Then instead of what is above,


Do like:



var gr = new GlideRecord('sys_user');


gr.get(g_form.getValue(<your_user_field_with_that_javascript:gs.getUserID()_thing>))



<your_variable> = gr.name


......


..


and so on.




Regards,


Michal


arnabwa
Giga Guru

Hi Dhruv,



This is easily achievable by just putting in the default value of each variable :


1. Variable "Requestor" : Right Click on the variable name >> Go to Configure variable >> Locate the "Default Value" tab >> put this line: "javascript:gs.getUserID();"



2. Variable "Email ID" : same as above process >> just put this line: "javascript:gs.getUser().getEmail();"



3. Variable "Phone number" : same as above process >> just put this line: "javascript:gs.getUser().getMobileNumber();"



You are Done here !!!



Thanks,


Arnab



Please mark Helpful / Correct according to the worthiness of my solution.


Sir your solution is very informative for me, as, I didnt know the use of Javascript in Default value. But sir I am unable to fetch the value of mobile phone in the variable. i have a business phone and a mobile phone feilds in the user table, it is not fetching value from either of them


mansoor3
Tera Contributor

Hi Dhruv,



In client script,



  var userID = g_form.getValue('requested_for');//gets the user ID


  var ga = new GlideAjax('SI name'); //this is the script include  


  ga.addParam('sysparm_name', 'SI function'); //this is the function within the script include  


  ga.addParam('sysparm_userId', userID);  


  ga.getXML(getResponse);  


 


  function getResponse(response) {  


var name,email,phone;


  var values = response.responseXML.documentElement.getAttribute('answer');  


if(values.length>-1){


name = values[0];


email = values[1];


phone =values[2];


}


g_form.setValue('name',name);


g_form.setValue('email',email);


g_form.setValue('phone_number',phone);



  }



In Server Script (Script Include),



function name : function() {  


 


  var userSrc = this.getParameter('sysparm_userId');  


  var user = new GlideRecord('sys_user');  


  var result = [];  


  user.addQuery('sys_id', userSrc);  


  user.query();  


 


  if(user.next()) {  


result.push(user.name);


result.push(user.email);


result.push(user.mobile);


}


  return result;


  },  



--Mansoor



PS - Please mark Helpful, Like, or Correct Answer if applicable.