Date field not populating in UI Action

suryakant30
Tera Contributor

Hi Everyone,

 

Why is my Date field (approval_date) not getting populated in a server-side UI Action in ServiceNow, even when using gs.nowDate() and setValue()? The state and other fields update correctly, but the Date field remains empty. What is the correct way to set a Date field in a UI Action?

 

Client - unchecked 

Approval date field type - Date 

Code :

current.state = 'approved';
var today = new Date();
current.setValue('approval_date', gs.nowDate()); //not working
current.approval_date = gs.nowDate();  //not working
current.approved_by = gs.getUserID();
current.update();
action.setRedirectURL(current);
3 ACCEPTED SOLUTIONS

Vishal Jaswal
Tera Sage

Hello @suryakant30 

Try below:

// Below will set YYYY-MM-DD

current.setValue('state', 'approved');
var today = new GlideDate();
current.setValue('approval_date', today.getValue()); 
current.setValue('approved_by', gs.getUserID());
current.update();
action.setRedirectURL(current);

// Below will set YYYY-MM-DD HH:MM:SS

current.setValue('state', 'approved');
current.setValue('approval_date', new GlideDateTime());
current.setValue('approved_by', gs.getUserID());
current.update();
action.setRedirectURL(current);

20.jpg


Hope that helps!

View solution in original post

namanajain
Mega Guru
Date fields in ServiceNow server-side UI Actions should be set using a GlideDate object. Using gs.nowDate() returns a string and may not populate Date fields reliably. The correct approach is .current.setValue('approval_date', new GlideDate());
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards

View solution in original post

Tanushree Maiti
Tera Patron

Hi @suryakant30 

 

1. gs.nowDate() and gs.nowDateTime() are not available in Scoped Applications

Ensure you are using  GlideDateTime class instead

 

2. If you are not using scoped app , then it could be the case

Your approval_date is Date/Time field  where you are trying to store gs.nowDate() which  returns a date string

 

Ensure , in that case  - you are using gs.nowDateTime() for Date/Time fields. 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

6 REPLIES 6

Vishal Jaswal
Tera Sage

Hello @suryakant30 

Try below:

// Below will set YYYY-MM-DD

current.setValue('state', 'approved');
var today = new GlideDate();
current.setValue('approval_date', today.getValue()); 
current.setValue('approved_by', gs.getUserID());
current.update();
action.setRedirectURL(current);

// Below will set YYYY-MM-DD HH:MM:SS

current.setValue('state', 'approved');
current.setValue('approval_date', new GlideDateTime());
current.setValue('approved_by', gs.getUserID());
current.update();
action.setRedirectURL(current);

20.jpg


Hope that helps!

Thank you @Vishal Jaswal , its working 

namanajain
Mega Guru
Date fields in ServiceNow server-side UI Actions should be set using a GlideDate object. Using gs.nowDate() returns a string and may not populate Date fields reliably. The correct approach is .current.setValue('approval_date', new GlideDate());
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards

Thank you @namanajain , its working now