Listening for Dynamic Data Events to Show an Error at the right time
Dynamic Data is often coupled with a free text heading element to show an error when no results are returned from the lookup. An example of showing a Free Text Heading element via a dependency can be found here. However, sometimes, you will not want to show the error message (when the Dynamic Lookup returns zero results), until after the user has entered the entire search string or even completed entry on multiple fields (the fields used in the lookup).
The JS action code in this article will listen to dynamic data load events and check the number of records returned and also the number of chars in the fields used in the lookup. If there are X number of chars entered (indicating that the user has typed enough to make the lookup valid/make sense) and no data returned, then show a free text heading element with the error message.
This solves the problem of the error being shown as soon as the user types 1 letter and triggers the lookup.
Form Action Example 1 (Using a single field in the lookup query)
// This function will receive the field and form objects. function(field,form){ var ordernumfield = form.getFieldByName('Order Number'), lookupDDfield = form.getFieldByName('OrderNumberLookup'), errorMsgField = form.getFieldByName('ErrorOnLookup1'); lookupDDfield.dynamicMgr.eventMgr.addListener( 'load', function(dd,result){ if (!dd.isRunning){ if (ordernumfield.getValue().toString().length >=9 && result.data.length == 0){ errorMsgField.show(); }else{ errorMsgField.hide(); } }else{ errorMsgField.hide(); } }, this ) }
In this example, the Order Number field must contrain at least 9 characters before the result from the Dynamic Lookup is evaluated.
Form Action Example 2 (Using multiple fields in the lookup query)
In this example, the code checks that two fields have both been completed (and zero results are returned) before showing an error.
// This function will receive the field and form objects. function(field,form){ var lname = form.getFieldByName('Last Name'), orgState = form.getFieldByName('OrgState'), destState = form.getFieldByName('DestState'), lookupDDfield = form.getFieldByName('addressLookup'), errorMsgField = form.getFieldByName('ErrorOnLookup2'); lookupDDfield.dynamicMgr.eventMgr.addListener( 'load', function(dd,result){ if (!dd.isRunning){ if (lname.getValue() != '' && orgState.getValue() != '' && destState.getValue() != '' && result.data.length == 0){ errorMsgField.show(); }else{ errorMsgField.hide(); } }else{ errorMsgField.hide(); } }, this ) }
0 Comments