Catalog Client Script to set date 90 days in future when certain field choice is made

richelle_pivec
Mega Guru

I have a catalog item with a field called "staff_type." There are four choices for the field. When "Contractor" is chosen, another field appears called, "c_end_date" (Contractor End Date). The ask is that I put a date in that field 90 days out when Contractor is chosen. However, when Contractor is not chosen, I do not want a date to be put in that field.

So, I have two scripts that I'm trying to cobble together into something that works for this field. I know it is not working because the UI Policy that would display the field is currently not working.

This is an On Change (of staff_type) catalog client script:

 function onChange(control, oldValue, newValue, isLoading) {
var staff = g_form.getValue('staff_type');
	if (oldValue == newValue){
	}
	
	if (staff == "Contractor") {
	var gdt = new GlideDate();gdt.addDays(90);gdt.getDisplayValue();
		g_form.setValue('c_end_date', gdt);
		return;
	}
	
}

the "var gdt" portion is what I found in another thread about how to set a date field's default value. And that works if I put it in for the default value of the "c_end_date" field, however, I can't have it put the date in for Employee, Volunteer, or Affiliate. So, I need to come up with a script that will only put it in when the "staff_type" is Contractor.

Thanks,

Richelle

1 REPLY 1

Brian Lancaster
Tera Sage

You may want to take a look at this.  A lot of the Date / Time functions do no work in client script without calling GlideAjax.

https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad