Auto-Populate name in Service Catalog using script

zuroslawski
Tera Contributor

please help me with the script, after selecting 'other employee' for the variable 'request for' and selecting the user in 'who is the request for?', I would like it to complete my First Name and Last name.

 

Opera Zrzut ekranu_2023-11-09_125244_dev59017.service-now.com.png

Opera Zrzut ekranu_2023-11-09_125311_dev59017.service-now.com.png

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@zuroslawski You need to create an onChange Script on the who_is_requested_for field. Script can be written as follows.

 

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

    //Type appropriate comment here, and begin script below
   var requested_for = g_form.getReference('who_is_requested_for',callbackFunc);
   function callbackFunc(caller){
	g_form.setValue('first_name',caller.first_name);
	g_form.setValue('last_name',caller.last_name);

   }

}

Hope this helps.

 

Vamsi G
Giga Guru

Hello@zuroslawski

you can write a simple onChange catalog client script to achieve this functionality.

here we go :

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

   var requested_for = g_form.getReference('who_is_requested_for',callbackFunc);

   function callbackFunc(caller){

               g_form.setValue('fname',caller.first_name);

               g_form.setValue('lname',caller.last_name);

 

   }

 

}

 

for onLoad -use the below script .

function onLoad() {

g_form.setValue('fname',g_user.firstName);

g_form.setValue('lname',g_user.lastName);

}

 

for more info visit -
https://docs.servicenow.com/bundle/utah-application-development/page/script/client-scripts/concept/c...

 

Please mark my answer as "Accept as Solution" and "Helpful." If it works for you,


Regards

Vamsi

Ankur Bawiskar
Tera Patron
Tera Patron

@zuroslawski 

if you are on Utah and above you can use auto-populate feature and no scripting is required

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Dr Atul G- LNG
Tera Patron
Tera Patron

HI @zuroslawski 

 

You can use Auto Populate option in catalog.

 

AtulyaLNG_0-1699956222876.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************