Autopopulate date in a filed on submission in a form.

Aditya Sinha
Tera Contributor

Hello! I have a requirement where I have to autopopulate a field in the form only when it is submitted . How to write a client script for this? Are there any other ways to implement it?

1 ACCEPTED SOLUTION

Hi @Aditya Sinha,

 

To auto-populate logged in user in field. I mean, in reference field you can set default value as

javascript:gs.getUserID();

 

For String field, use default value as:

javascript:gs.getUserDisplayName();

 

Refer attached text file.

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

4 REPLIES 4

Sagar Pagar
Tera Patron

Hi @Aditya Sinha,

 

You can try it by two ways.

On Submit Client scripts:

unction onSubmit() {

		g_form.setValue("field_name", new Date());
	}

Before Insert/update Business rule:


current.setValue("field_name", new GlideDate());
 or

current.setValue("field_name", new GlideDateTime());

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

 

The world works with ServiceNow

Aditya Sinha
Tera Contributor

Hi Sagar. 

I have a variant of this requirement where I have to populate the current logged in user in a filed of the form. Can you help with the code of this?

 

Your answer above was very helpful

Hello @Aditya Sinha ,

 

Just a small twig to the code suggested by @Sagar Pagar 

var currentUser = gs.getUser();

var record = new GlideRecord('table_name');
record.initialize();
record.setValue('field_name', currentUser.getID());
record.insert();

 

Try this 

 

Thanks,

Amarjeet Pal

Hi @Aditya Sinha,

 

To auto-populate logged in user in field. I mean, in reference field you can set default value as

javascript:gs.getUserID();

 

For String field, use default value as:

javascript:gs.getUserDisplayName();

 

Refer attached text file.

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow