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

Question

Question

addSet not adding rows to variable table

asked on April 20, 2023

In my code I am using the addSet method to add two sets to my collection. It is showing the additional sets on the screen, but is not displaying the values I have inserted in my javascript. 

Upon further inspection when I console.log() fieldId:111, the collection, values it is only showing 1 row.

fieldId:118 is a single_line element that comes in via the URL: NLTCC%20-%20Northwest%20Louisiana%20Technical%20Community%20College,Yes,Shreveport%20-%20US06105900,05/06/2023,NLTCC%20-%20Northwest%20Louisiana%20Technical%20Community%20College,Yes,Minden%20-%20US06106600,05/05/2023,NLTCC%20-%20Northwest%20Louisiana%20Technical%20Community%20College,Yes,Mansfield%20-%20US06101400,05/04/2023

rowCnt is: 3

There are 3 collection sets in fieldId:111. The collection consists of School{fieldId:112},HasCampus{fieldId:113},Campus{fieldId:114},Date{fieldId:115}.

my code:

This is the screen view:

 

This is the console:

Am I missing something?

Thank you in advance for any help.

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on April 20, 2023

Try this out:
 

async function expandParam(rowCnt) {
  var arrayAS = [];

  if (rowCnt > 1) {
    await LFForm.addSet({ fieldId: 111 }, rowCnt - 1);
  } // if value > 1 close
  else {
    arrayAS = LFForm.getFieldValues({ fieldId: 118 }).split(",");
    LFForm.setFieldValues({ fieldId: 115 }, { dateStr: arrayAS.pop() });
    LFForm.setFieldValues({ fieldId: 114 }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 113 }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 112 }, arrayAS.pop());
  } // else close
} // function expandParam

function expandParamUpdate(rowCnt) {
  var arrayAS = [];

  arrayAS = LFForm.getFieldValues({ fieldId: 118 }).split(",");

  for (var i = 0; i < rowCnt; i++) {
    LFForm.setFieldValues(
      { fieldId: 115, index: i },
      { dateStr: arrayAS.pop() }
    );
    LFForm.setFieldValues({ fieldId: 114, index: i }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 113, index: i }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 112, index: i }, arrayAS.pop());
  } // for var = i close
} // function expandParam

LFForm.onLookupDone(
  function () {
    // do not await 
    expandParam(LFForm.getFieldValues({ fieldId: 119 }));
    disableCoorOnly();
  },
  { lookupRuleId: 1 }
);

 

2 0

Replies

replied on April 20, 2023

Update: I added an onFieldBlur to another field and set it's function to console.log() the fieldId:111. When I move off of that field that has the onFieldBlur I now have 3 rows available for the variable. Could this be some type of timing issue?

Any thoughts?

0 0
replied on April 20, 2023 Show version history

Hi Lloyd, it is definitely a timing issue. The spec for the LFForm object returns a promise when you use the "addSet" or "addRow" functions. This has to be "awaited" in order for the DOM to update before adding the fields to the collections. I have made some updates to your code that should work for you. NOTE: I believe you wanted to use "arrayAs.shift()" to pull the first element from your array as opposed to the last, but that is the only big change I made outside of adding await.

 

async function expandParam() {
  const fieldsPerRow = 4;
  const arrayAs = LFForm.getFieldValues({ fieldId: 89 }).split(",");
  const rowCnt = Math.floor(arrayAs.length / fieldsPerRow);
  if (rowCnt > 1) {
    await LFForm.addSet({ fieldId: 83 }, rowCnt - 1);
    for (let i = 0; i < rowCnt; i++) {
      LFForm.setFieldValues({ fieldId: 86, index: i }, arrayAs.shift());
      LFForm.setFieldValues({ fieldId: 90, index: i }, arrayAs.shift());
      LFForm.setFieldValues({ fieldId: 91, index: i }, arrayAs.shift());
      LFForm.setFieldValues({ fieldId: 92, index: i }, arrayAs.shift());
    }
  } else {
    LFForm.setFieldValues({ fieldId: 86, index: i }, arrayAs.shift());
    LFForm.setFieldValues({ fieldId: 90, index: i }, arrayAs.shift());
    LFForm.setFieldValues({ fieldId: 91, index: i }, arrayAs.shift());
    LFForm.setFieldValues({ fieldId: 92, index: i }, arrayAs.shift());
  }
}
expandParam(3);


That being said, we do have plans to make the API more robust and easier to use so feedback like this is very helpful. Thanks again!

0 0
replied on April 20, 2023

Zachary, 

When I try using the await function I receive the error:

What I ended up doing was to split the function into two functions below the addSet statement. Then below the addSet statement insert the statement:

setTimeout(function() {expandParamUpdate(LFForm.getFieldValues({fieldId : 119}))},2000);

This makes it wait for 2 seconds and then execute the second function where the code populates the fields.

Thank you for all your help.

0 0
replied on April 20, 2023

I would try to avoid setTimeout as much as possible because it will artificially increase the load time of your form. You are correct, you cannot use await outside of a function. If you could post your whole code I could help you refactor it, but if you look at the code I posted, I am not awaiting the "expandParam" function. You should be able to have one main function that is defined asynchronous and invoke it without awaiting it (like on line 20 in my example). This should avoid the error you are seeing while running the form as quickly as possible.

0 0
replied on April 20, 2023

function expandParam (rowCnt) {
  var arrayAS = [];
    
  if (rowCnt > 1) {
    LFForm.addSet({fieldId : 111}, rowCnt - 1);
    setTimeout(function() {expandParamUpdate(LFForm.getFieldValues({fieldId : 119}))},5000);
  } // if value > 1 close
  else {
      arrayAS = LFForm.getFieldValues({fieldId : 118}).split(",");
      LFForm.setFieldValues({fieldId : 115},{dateStr : arrayAS.pop()});
      LFForm.setFieldValues({fieldId : 114},arrayAS.pop());
      LFForm.setFieldValues({fieldId : 113},arrayAS.pop());
      LFForm.setFieldValues({fieldId : 112},arrayAS.pop());
  }   // else close 
} // function expandParam


function expandParamUpdate (rowCnt) {
  var arrayAS = [];
  
  arrayAS = LFForm.getFieldValues({fieldId : 118}).split(",");

  for (var i = 0; i < rowCnt; i++) {
    LFForm.setFieldValues({fieldId : 115, index : i},{dateStr : arrayAS.pop()});
    LFForm.setFieldValues({fieldId : 114, index : i},arrayAS.pop());
    LFForm.setFieldValues({fieldId : 113, index : i},arrayAS.pop());
    LFForm.setFieldValues({fieldId : 112, index : i},arrayAS.pop());
  }  // for var = i close
} // function expandParam
 

 

This is how I get the function to fire on loading the form.

LFForm.onLookupDone(function () {expandParam(LFForm.getFieldValues({fieldId : 119})); disableCoorOnly();},{lookupRuleId:1});
 

0 0
replied on April 20, 2023

Wonderful! Glad you were able to resolve your issue without setTimeout!

APPROVED ANSWER SELECTED ANSWER
replied on April 20, 2023

Try this out:
 

async function expandParam(rowCnt) {
  var arrayAS = [];

  if (rowCnt > 1) {
    await LFForm.addSet({ fieldId: 111 }, rowCnt - 1);
  } // if value > 1 close
  else {
    arrayAS = LFForm.getFieldValues({ fieldId: 118 }).split(",");
    LFForm.setFieldValues({ fieldId: 115 }, { dateStr: arrayAS.pop() });
    LFForm.setFieldValues({ fieldId: 114 }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 113 }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 112 }, arrayAS.pop());
  } // else close
} // function expandParam

function expandParamUpdate(rowCnt) {
  var arrayAS = [];

  arrayAS = LFForm.getFieldValues({ fieldId: 118 }).split(",");

  for (var i = 0; i < rowCnt; i++) {
    LFForm.setFieldValues(
      { fieldId: 115, index: i },
      { dateStr: arrayAS.pop() }
    );
    LFForm.setFieldValues({ fieldId: 114, index: i }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 113, index: i }, arrayAS.pop());
    LFForm.setFieldValues({ fieldId: 112, index: i }, arrayAS.pop());
  } // for var = i close
} // function expandParam

LFForm.onLookupDone(
  function () {
    // do not await 
    expandParam(LFForm.getFieldValues({ fieldId: 119 }));
    disableCoorOnly();
  },
  { lookupRuleId: 1 }
);

 

2 0
replied on April 21, 2023

Nice. Adding ASYNC worked. Thank you Zachary.

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

Sign in to reply to this post.