The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Business Rule to display an error message

Vaishali12
Kilo Contributor

Hi Folks,

I have a custom field "When Needed" on the incident in which I need validation if user tries to inout today's date or date in the past It should show an error message while submitting. How can it can be achieved using BR.

 

1 ACCEPTED SOLUTION

Danish6
Giga Expert

Configure the Business Rule as below

Name: XYZ
Table: incident ["incident"]
Active: Selected (checked)
Advanced: Selected (checked)
Switch to the When to run section and continue configuring the Business Rule:
When: Before
Insert: Selected (checked)

// rightnow stores the current time
	var rightnow = new GlideDateTime();
	// Create a GlideDateTime object for the When needed date
	var whenNeeded = new GlideDateTime(current.needed_field_name_here);
	var today = rightnow.getLocalDate();
	var istoday = whenNeeded.getLocalDate();
	// If the When needed date is before rightnow, do not write the record to the database and Compare today and istoday to see if they are the same day
	// show an error message to the screen
	if(whenNeeded.before(rightnow) || (today.compareTo(istoday) == 0)){
		gs.addErrorMessage("Your message");
		current.setAbortAction(true);
	}

 

Kindly let me know it helped your queries.

View solution in original post

4 REPLIES 4

Danish6
Giga Expert

Configure the Business Rule as below

Name: XYZ
Table: incident ["incident"]
Active: Selected (checked)
Advanced: Selected (checked)
Switch to the When to run section and continue configuring the Business Rule:
When: Before
Insert: Selected (checked)

// rightnow stores the current time
	var rightnow = new GlideDateTime();
	// Create a GlideDateTime object for the When needed date
	var whenNeeded = new GlideDateTime(current.needed_field_name_here);
	var today = rightnow.getLocalDate();
	var istoday = whenNeeded.getLocalDate();
	// If the When needed date is before rightnow, do not write the record to the database and Compare today and istoday to see if they are the same day
	// show an error message to the screen
	if(whenNeeded.before(rightnow) || (today.compareTo(istoday) == 0)){
		gs.addErrorMessage("Your message");
		current.setAbortAction(true);
	}

 

Kindly let me know it helped your queries.

Thank you Danish the code worked successfully.

Jaspal Singh
Mega Patron
Mega Patron

Hi Ashwini,

 

In this case better would be client script or UI policy as Business rule would run when form is tried to saved or submitted thus at times being frustating for user as it would be after filling all details error would be popped.

A simple UI Policy would suffice where you can specify the conditin as

When Needed before Today

& in the script you can add

function onCondition()

{

alert('When Date cannot be before Today or Past date');

g_form.setClearValue('u_when_date');

}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

Hi Jaspal,

Thanks for your valuable time,I wanted to show the message on submission.