This article has been prompted by a question I was asked at a recent Developer training course in Johannesburg, South Africa. I wrote some time ago an article called "Field Level Scripting in Search Screens" that discussed scripting in search screens. One of the tips discussed in the article is that we can use validation rules in search screens.
Checking that the search screen has a value can be very important to prevent users creating an unnecessary interrogation of all records in a table.
The validation rules are checked on submission of a search screen even though no data is inserted or updated.
If the example below is added to the comp_name field of the companysearchbox screen it will ensure that it can't be null when the form is submitted.
if (!Values("comp_name")) { Valid = false; ErrorStr = "you must fill in this field"; }
This allows you add a check on an individual field. But what if you want to check that the whole screen has at least one value entered not just specific fields?
It is possible to add a rule on a single field that will check all the values in a search screen to ensure that at least one value is submitted.
This example code can be added to the comp_name field of the companysearchbox.
var myBlock = CRM.GetBlock("CompanySearchBox"); var myE = new Enumerator(myBlock); Valid = false; while (!myE.atEnd()) { if (Values(myE.item())) { Valid = true; } myE.moveNext(); } if (Valid == false) {ErrorStr = "At least one field must have a value";}
This single rule will check all fields. However if are there no values submitted the error will be indicated on the comp_name field.
The code uses the fact that a screen (EntryGroup) can be enumerated and permits each field submitted to be evaluated.