Autopopulate the variables in the catalog item based on the Requestor name field

Servicenow de11
Tera Contributor

Hello,

 

In the catalog item variables if the user selected the name i want to autopopulate the location and company  both are reference field in the sys_user table. Can anyone sugget the scriptinclude and client acript to autopulate.

 

Thanks in advance.

 

 

2 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @Servicenow de11 

Try the below trick.

  1. Define your user variable. Sample below as "Requested for"
  2. Create the Location variable
  3. At the section "Auto-populate", set similar to the below screenshot.

TaiVu_0-1697007851589.png

 

You can do the same for the Company variable. 

 

Let me know if it works for you!

 

Cheers,

Tai Vu

View solution in original post

Hi @Servicenow de11 

 

As you mentioned. location & company fields are of type "Reference".

It should not populate sys_id.

 

Can you check , both variables on catalog item are of type "reference" or Single line text.

 

If its single line text then update script include below :

 

  if (grUser.next()) {
            /*4. If user present then store values in JSON object */

            result.location = grUser.location.name;  //updated
            result.company = grUser.company.name; //update
            
        }

		/*5. Stringify the object and then return */
        return JSON.stringify(result);

 

 

 

OR you can go with Auto-populate option suggested by @Tai Vu you just need to dot-walk on "Name" field.

like location.name  & company,name

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

8 REPLIES 8

Tried your suggested trick but sys_id is populating in the variable

Hi @Servicenow de11 

You mean we are able to get the Location and Company from the User Profile. However the Location and Company variables are populated as sys_ids?

Anubhav24
Mega Sage
Mega Sage

As already stated by our colleague in the below script includes and client script , if there are only two fields and less data you can also try to use getreference() function however there is a disadvantage to it related to performance :

 

You can refer below links :

https://www.servicenow.com/community/developer-forum/what-is-the-use-of-getreference/m-p/1433807/pag...

https://www.servicenow.com/community/developer-forum/getreference-best-practice-or-not/m-p/1522500

https://developer.servicenow.com/dev.do#!/reference/api/utah/client/c_GlideFormAPI#r_GlideForm-GetRe...

 

Please mark helpful/correct if my response helped you.

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Servicenow de11 ,

 

Pls look at the below link,

 

https://www.servicenow.com/community/now-platform-forum/auto-populate-service-catalog-variable-with-...

 

Mark my answer helpful & accepted if it helps you.

 

Thanks,

Danish