You are viewing limited content. For full access, please sign in.

Question

Question

Forms - Multiple Level Lookups occasionally fail

asked on December 7, 2016

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);
  }
});

 

0 0

Answer

SELECTED ANSWER
replied on December 15, 2016

As I continued working through this and continued to have problems, I finally had to stop an evaluate if I was going about this in the best manner.  I asked myself two questions:

  • What problem am I actually trying to solve?
    • I'm trying to do a database lookup, that involves a table join (getting values from two tables): the admin ID from the systems table and the employee name that matches that employee ID from the employees table.
  • Where is this failing?
    • Because the Lookup functionality of Forms cannot do table joins, it can only do lookups from one table at a time (enhancement idea?) - I'm using Javascript to fire off multiple different lookups and updates to get the admin ID from the systems table, and then using that to lookup the employee name from the employees table, and then using that to select the appropriate value in the dropdown list of all employee names.  This works 97-98% of the time, but the other 2-3% of the time, the secondary lookup is not getting triggered.

 

It was at that point, that I finally figured out the solution.  I realized that this was a lot of steps in Javascript to do something that the MSSQL database does naturally - table joins.  So I went back to the database and set-up a new database view.  This view displayed all the contents of the systems table, except for one change, instead of returning the employee ID of the admin, it joined in the employees table and returned the name of the admin.  Then I tweaked my form to use this view for the lookup instead of the systems table and changed my steps from: loading the admin ID and then looking up their name, and to instead just loading their name.

And just like that, my form works as expected.  No more blank fields!

0 0

Replies

replied on December 8, 2016

Check Forms 10.1 Update 3 patch release notes as there were updates for Lookups in Forms that may help

0 0
replied on December 8, 2016

The problem is, I think I'm already on Update 3...

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.