Need to Clear field on a Validation Business Rule

DeepThinker
Giga Contributor

The following BR message is checking to make sure the actual start date is not before the Planned start date on a change. then is throws a message. Can someone help with how I would clear the work_start field in addition to this message. 

I've tried "g_form.clearValue('work_start');" before the Abort but this did not work. I think I'm missing an if statement but I don't know how to write it. 

Here is the script:

(function executeRule(current, previous /*null when async*/) {
gs.addErrorMessage(gs.getMessage("{0} must be after {1}", [ current.work_start.getLabel(), current.start_date.getLabel() ]));

current.setAbortAction(true);


})(current, previous);

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Can you try setting it simply current.work_start='';

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

AbhishekGardade
Giga Sage

Hello Deep,

You can not use g_form in server side code 

Try with below line of code:

current.setValue('work_start','');

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

Jaspal,

 

This worked exactly how I need it to work, however I'm concerned about what Lukasz stated.

Lukasz Bojara
Kilo Sage

Hi,

The GlideForm (g_form) works only on client-side so you can not use it in Business Rule as those run on server-side. You could use current.work_start='' and then current.update() but is highly not recommended to update current, because you will trigger all business rules once again.

I would suggest creating an OnChange client script to validate dates while still on the form, it is more user-friendly.

E.g. you can use the already existing function validateStartDateBeforeEndDate to compare dates:

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

   
	var showErrorMsg = function(errorMsg){
		g_form.clearValue('work_start');
		g_form.showErrorBox("work_start",errorMsg);
	};

	g_form.hideFieldMsg("work_start", true);
	validateStartDateBeforeEndDate("start_date", "work_start", showErrorMsg);

   
}

find_real_file.png

 

The result will be like this when the user will put the Actual Start date before the Planned Start Date.

 

find_real_file.png

 

Hope this helped.

Best regards,
Łukasz