How to get the requested for user name from a catalog form

juliochacon23
Tera Expert

Hi, I am working on a request catalog form with orchestration.  I need to find a way to pull the user name field from the requested for field instead of the logged-in user.  I am currently using a catalog client script with the code below to pull the user name but this gives me the logged in user name instead of the requested for.  Any advice on how to get the user name for requested for?

 

juliochacon23_0-1679073954341.png

 

juliochacon23_1-1679074013144.png

 

 

function onLoad() {

                                var userID = g_user.userName;

 

g_form.setValue('user_name',userID);

  

  

}

 

2 ACCEPTED SOLUTIONS

I got the mistake that you are doing @juliochacon23 In client script code of 7th line change sysparam_sys to sysparm_sys and also change script include 5th line sysparam_sys to sysparm_sys .Then it will definitely work please check onces 

If my solution helps you please mark my answer as accepted and helpful.

Thanks and Regards

Uday Kumar Valapudasu 

 

View solution in original post

Amazing support.  Thank!!!!

View solution in original post

14 REPLIES 14

Sagar Pagar
Tera Patron

 Hi @juliochacon23,

I would suggests to go with onchange catalog client scripts based on Requested for variable.

 

Scripts:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		getUserName();
		return;
	}
	getUserName();
}

function getUserName() {
	var user = g_form.getReference('requester_user', getDetails); 

	function getDetails(user) {
		g_form.setValue("user_name", user.user_name);
	}
}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Thanks, I tried that and I did not get anything for user_name to match the requested for user name.  The user name field is now blank.  The field is called requested_for.  I would like to call the user name of the user in this field and assign it to the field called user_name.

 

juliochacon23_1-1679090867723.png

 

 

juliochacon23_0-1679090709236.png

 

You need to change that "Variable name" dropdown to be the 'requested_for' field so the script knows when to run and that's when the Requested For field changes

 

StevenParker_0-1679332672414.png

 

I would also advise adding the "Isolate Script" checkbox to your form....that field at times will need to be checked or unchecked depending on your use case with your script.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Uday_Kumar
Giga Guru

Hi @juliochacon23 How are you ?

You can achieve your requirement using Script Include and GlideAjax. I tried in my personal instance and working absolutely very good for your reference i am posting my code make change as per your field names @juliochacon23 .

Script Include:

var user_name = Class.create();
user_name.prototype = Object.extendsObject(AbstractAjaxProcessor, {
username:function()
{
	var sys_id=this.getParameter('sysparam_sys');
	var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',sys_id);
gr.query();
	if(gr.next())
	{
		return gr.first_name+" "+gr.last_name;
	}
},
    type: 'user_name'
});

Onchange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;}
   var usersys=g_form.getValue('requested_for');
   var ga = new GlideAjax('user_name');
    ga.addParam('sysparm_name', 'username');
    ga.addParam('sysparam_sys', usersys);
    ga.getXML(callBackFunction);
    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('user_name', answer);}}

The Output of mine is here:

Uday_Kumar_0-1679120238212.png

Uday_Kumar_1-1679120272273.png

 

Please mark my response as helpful and accepted solution incase of any doubt please ask me @juliochacon23 

Thanks and regards

Uday Kumar Valapudasu