Autopopulate field in a catalog using Client Scripts

EGUZMANHERRERA
Tera Contributor

Hi everyone, I want to use Clients Scripts to autopopulate the "Requested By: " field with the name of the user who is making the request in the catalog, The field is set as Read Only because I want the Client Script to autopopulate without user intervention. Can anyone please help me, I'm new using ServiceNow so my knowledge is very limited. Request By.png

Also, the user must be active

Type specifications.png

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@EGUZMANHERRERA Here is the onLoad client script you need to create on your Catalog Item.

 

Screenshot 2024-03-09 at 8.38.33 AM.png

 

Here is the script.

 

 

function onLoad() {
    //Type appropriate comment here, and begin script below    
	g_form.setValue('requested_by',g_user.userID);

}

 

 

Hope this helps.

View solution in original post

Sumanth16
Kilo Patron

Hi @EGUZMANHERRERA ,

 

Configure onload catalog client script and use below code:

 

g_form.setValue('requested_for',g_user.userID);// replace variable with your variable name

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

View solution in original post

8 REPLIES 8

Sandeep Rajput
Tera Patron
Tera Patron

@EGUZMANHERRERA Here is the onLoad client script you need to create on your Catalog Item.

 

Screenshot 2024-03-09 at 8.38.33 AM.png

 

Here is the script.

 

 

function onLoad() {
    //Type appropriate comment here, and begin script below    
	g_form.setValue('requested_by',g_user.userID);

}

 

 

Hope this helps.

Here Replace name of Catalog item with your Catalog item name.

Thank you very much for the help, it worked perfectly!

Luiz Lucena
Mega Sage

That should be easy, we did that.

 

In the Default Value tab.

Add this Javascript code.

IMG_3529.jpeg

 

 You can also add a check to validate if user is active, I'm on my phone but will get more examples when I'm on the computer.

 

EDIT: actually it should work exactly like you want.

 

In the type specification tab, you add the filter to retrieve only active users.

 

And in the default value tab, the code to automatically populate the logged in user.

 

If my answer is correct, don't forget to mark it as solved/helpful. 😊