I'm running a form that allows a user to edit or add records to a database. Many of the records in the database table are foreign keys to other tables. To make it work, I have multiple levels of lookups.
lookups 1 and 2 occur on form load.
(lookup 1) - The list of list of system IDs are loaded from the database into a hidden drop-down field.
(lookup 2) - A list of all employees are loaded into a dropdown field that appears in each record of a collection. This allows the user to select the desired employee for each record. Unfortunately, selecting the existing record from the database table is a little tricky because the user needs to see the employee name, but it's stored in the DB as employee ID. This is addressed a little later.
(step A) - The next step adds records to the collection, once for each item in the dropdown list, then it copies each item from the dropdown list into a field (system_id) in each record of the collection, and triggers a change call so that the next set of lookup rules is initiated.
(lookup 3) - based on the system IDs that were imported in lookup 1 and loaded in step A, this lookup populates all the other fields on the collection. This includes a hidden field where the employee ID is loaded.
(lookup 4) - this is triggered by the loading of the employee ID in lookup 3, this looks up the employee ID from a different database table and returns the employee name associated with that ID, this name value is then stored in another hidden field.
(step B) - this step is triggered by the change event of the hidden name field that was loaded in lookup 4. This step takes the name from the hidden name field and searches for it in the dropdown field and selects it.
This results in the field displaying the selected name based on the value in the database table, but still provide the full list of employee names if the user wants to change the selected name, and provides the full list of names available when the user adds a new record to the collection.
This works wonderfully about 99% of the time. However, occasionally it breaks down and doesn't select the employee name from the dropdown list.
When I show the hidden fields, I can see that the breakdown is occurring with lookup 4. The hidden ID field is displayed, but the hidden name field is not. So then step B doesn't work either.
As near as I can tell, what is actually occurring is that lookup 4 is either getting triggered on some records in the collection when all it does with all the other records (maybe the change event isn't triggered for some reason); or perhaps the lookup 4 is being triggered but somehow before lookup 3 has finished.
Has anyone else encountered an issue like this, and if so, do you have any recommendations about how to avoid it?
Thank you!
$(document).ready(function () { //When the employee name of the administrator is populated by the lookup rules //into the hidden field, select that name from the employee list dropdown $('.systemsList').on('change','.admin1NameHidden input', function () { var s = $(this).closest("ul").find('.admin1NameHidden input').val(); var a = ''; a = $(this).closest("ul").find('.admin1 option').filter(function () { return $(this).html() == s; }).prop('selected', true); }) }); //When the form with initial lookup rules has finished loading, complete these steps: //1. add a record to the collection for each systemID in the hidden dropdown menu //2. copy the systemIDs from the hidden dropdown to the systemID field in each //record in the collection. //3. disable the delete button on each collection record (user can only delete records //they have added during the task session). //4. trigger the change event so the next set of lookup rules will occur. //5. make the systemID field read only so it can't be edited (user can only edit records //they have added during the task session). $(document).on("onloadlookupfinished", function () { var resultNumber = $('.filledField option').size(); for (var i = resultNumber - 2; i > 0; i--) { $('#q29 .cf-collection-append').trigger('click'); } for (var i = 1; i <= resultNumber; i++) { var k = i + 1; $('.systemID input').eq(i-1).val($('.filledField option:nth-child(' + k + ')').text()); $('.systemID input').eq(i-1).parent().parent().parent().siblings('.cf-collection-delete').addClass('hideBtn'); $('.systemID input').eq(i-1).trigger('change'); $('.systemID input').attr('readonly', true); } });