How to compare a hardcoded date time value with a date field value in client script?

sameer S
Tera Contributor

Hi All ,

 

I have to write an onSubmit client script to make attribute changes to old tickets. e.g tickets created before 25th Sep 2022 ,convert the mandatory fields to non-mandatory , so users will be able to close the tickets with/without entering values . here is my client script which doesn't work. below is my script. can someone please advise?

 

function onSubmit() {
//make the historic tickets as non-mandatory
var state = g_form.getValue("state");
var openedDate = g_form.getValue("opened_at");
g_form.addInfoMessage("openedDate :"+openedDate);
var gdt1 = new GlideDateTime();
gdt1.setDisplayValue(openedDate, "dd/MM/yyyy K:mm:ss a");
var opened = gdt1.getDisplayValue();
g_form.addInfoMessage("opened :"+opened);
var finalDate = '2022-10-10 00:00:00';
var gdt = new GlideDateTime();
gdt.setDisplayValue(finalDate, "dd/MM/yyyy K:mm:ss a");
var finaldt = gdt.getDisplayValue();
if (opened > finaldt && state == 3) {
g_form.setMandatory("is_there_specific_population_impacted", false);
g_form.setMandatory("MAS_need_following_select_apply", false);
g_form.setMandatory("business_need_or_problem_statement", false);
g_form.setMandatory("is_there_specific_population_impacted", false);
g_form.setMandatory("id_card_support_open_enrollment", false);
g_form.setMandatory("select_all_applicable_option", false);
g_form.setMandatory("type_of_inserts_needed_id_mailing", false);
g_form.setMandatory("is_mass_reissue_mailing_needed", false);
g_form.setMandatory("id_card_support_open_enrollment", false);
g_form.setMandatory("special_considerations", false);
g_form.setMandatory("will_documents_printed_single_or_double", false);
}
}

2 REPLIES 2

Claude DAmico
Kilo Sage

There are some helpful snippets here:

https://www.servicenow.com/community/developer-articles/date-validations-client-scripts/ta-p/2298860 

Claude E. D'Amico, III - CSA

Brian Lancaster
Tera Sage

I believe GlideDateTime is Server side script and cannot be used in client scripts. You will more then likely need to create a client callable script include and call it via GlideAjex.

You can also try this No Code Date Validation