How to auto populate variables on portal

Fabian Mattheis
Kilo Contributor

Hello everyone,

I am currently developing a service portal in Service Now and i want to autpopulate fields depending on a selected user from a reference field (sys_user) (not the user logged into the portal)

This is my script:

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

  if(newValue == oldValue){      

  return;      

  }

var sysID_user = g_form.getReference('variables.ansprechpartner' , autofill);

var mail;

var tel;

function autofill (sysID_user){

mail = sysID_user.email;

tel = sysID_user.phone;

g_form.setValue('variables.Telefon_server', mail);

g_form.setValue('variables.mail_server', tel);

}

}

The UI Type is set to "All" and the script works perfectly on the Service Now Interface, but it doesn't do anything on the Service Portal.

It should automatically fill the fields phone number and email adress (single line texta variables) on the catalog item.

find_real_file.png

Thank's in advance for your help

1 ACCEPTED SOLUTION

chaselong
Mega Expert

I'm not sure you need those variables in the autofill function. Here's the script we use and it works in ServicePortal.


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



        var user = g_form.getReference('name', autoFill);



        function autoFill(user) {  


                  g_form.setValue('user_phone',user.phone);


                  g_form.setValue('user_location',user.location);


                  g_form.setValue('user_department',user.department);


        }



}


View solution in original post

2 REPLIES 2

chaselong
Mega Expert

I'm not sure you need those variables in the autofill function. Here's the script we use and it works in ServicePortal.


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



        var user = g_form.getReference('name', autoFill);



        function autoFill(user) {  


                  g_form.setValue('user_phone',user.phone);


                  g_form.setValue('user_location',user.location);


                  g_form.setValue('user_department',user.department);


        }



}


Ok i don't really see a big difference in the code (except the variables.) but this works for me.


Thank's a lot !