"Set value to null" client script only works once (Tokyo)

C_dric Chen
Tera Contributor

Greetings, everyone.

 

I'm dealing with a weird issue with a client script that's designed to clear the "Due date" field and generate an error message when the date in the "Due date" field is earlier than the date in the "Start date" field. The trouble is: This client script only works on the first time I enter an invalid date into the "Due date" field. On the seconds time, the third time, etc., the client script would stop clearing the value while still generating the error message.

 

Here are the details of the client script:

Type: onChange

Field name: Due date

UI type: All

Active: Yes

Inherited: No

Global: Yes

Scripts:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

	var start = new Date(g_form.getValue("start_date"));
	var due = new Date(g_form.getValue("due_date"));
	//Make sure that there is at least one day between the two.
	//Add one day to start to make sure Due date is after that.
	start.setDate(start.getDate() + 1); 

	if(start > due) {
		//Clear the field
		g_form.setValue("due_date", "");
		//Give user message.
		g_form.addErrorMessage("The due date cannot be earlier than the start date.");
	}
}

 

Would anyone have any idea on what's causing such issue?

 

Thanks in advance!

2 REPLIES 2

snowycode
Tera Contributor

Hi Cédric - I just mocked this up in my PDI and your code seems to be working for me. It clears the due date on the second, third etc. times.

 

Here are some screenshots of what I did. Does anything look different on your end?

 

Client Script.pngCatalog Item.png

Basheer
Mega Sage

Hi @C_dric Chen ,

It seems space missing in double quotes.

g_form.setValue("due_date", " ");

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.