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

Question

Question

Deleting table rows based on submission

asked on February 25 Show version history

Hi guys,

I've been trying to puzzle out this challenge on the modern designer, I need to make this work that whenever the user clicks on the submit button and there is an empty row in the table, in this case row 3 is empty. The script should delete the entire row , like in this image: 

This is how I scripted it but it does not function as expected: 

LFForm.onFormSubmission(function () {
var rowCount = LFForm.getFieldValues("Deletion").length; 
for (var i = rowCount - 1; i >= 0; i--) {
  console.log(LFForm);
    var rowData = LFForm.getFieldValues("Deletion")[i]; 
    
    // Check if the row is empty (modify condition based on actual fields)
    if (!rowData || Object.values(rowData).every(value => value === "" || value === null)) {
        LFForm.deleteRow({variableName: "Deletion"}, i);
    }
}
  });

 

0 0

Replies

replied on February 25 Show version history

Curious if this is a process you'll be using in production as clearing rows on submission is a slightly awkward user experience. Regardless there are three issues.

  1. The main issue is you need to "await" deletion of the row otherwise the onFormSubmission callback returns immediately and your form is submitted before rows are deleted
    1. It is worth doing a little light reading on JavaScript Promises (async/await) as they are essential to using the LFForm object
  2. line 2 of your example isn't using the appropriate field identification object so it cannot get a field value. You used the correct format on line 9.
  3. You are deleting from bottom to top which should be ok as it doesn't affect the row numbering of earlier rows, but a simpler approach would be to pass an array of row numbers and call delete row a single time

 

LFForm.onFormSubmission(async function () {
  // Get the entire deletion column
  const deleteRow = LFForm.getFieldValues({ variableName: "Deletion" });
  // Convert deletion column into a list of indexes to delete
  const deleteRowIdx = deleteRow.reduce((prev, rowData, i) => {
    if (
      !rowData ||
      Object.values(rowData).every((value) => value === "" || value === null)
    ) {
      prev.push(i)
    }
    return prev
  }, []);
  // Delete all empty rows
  await LFForm.deleteRow({ variableName: "Deletion" }, ...deleteRowIdx);
})

 

0 0
replied on February 26

Hi Zachary,

The script doesn't seem to work and it returns this error:

however when I declare the deleteRow instead of row, it somehow does not return an error and doesn't even delete the relevant rows.

LFForm.onFormSubmission(async function () { 

    const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const deleteRow = LFForm.getFieldValues({ variableName: "Deletion" });
 const deleteRowIdx = deleteRow.reduce((prev, rowData, i) => { 
      console.log("This is list of indexes to delete")
    
     if (!rowData || Object.values(rowData).every((value) => value === "" || value === null)){
        console.log("if statement executed")
        prev.push(i);
      }
      return prev;
    }, []);
  
    await delay(10000);
  
    await LFForm.deleteRow({ variableName: "Deletion" }, deleteRowIdx);
  });

 

0 0
replied on February 27

I updated the script, I had the wrong syntax for deleting multiple rows at once in the last line.

 

Curious though, why opt to automatically delete the rows instead of using required fields in the column to force the submitter to deliberately delete the rows?

0 0
replied on March 4

Incase anyone might need this in future, I have found a way out and it works well.

// Delete empty rows in the table based on submission
LFForm.onFormSubmission(function () {

    let values = LFForm.getFieldValues({ fieldId: 14 }, { fieldId: 15 }, { fieldId: 16 }, { fieldId: 18 });
    if (Array.isArray(values)) {
        let emptyRow = values.findIndex(row => Object.values(row).every(value => value === "" || value === null));
        if (emptyRow !== -1) {
            LFForm.deleteRow({ variableName: "Deletion" }, emptyRow);
            return { error: "Your form was not submitted because the is an empty row " };

        }
    }

});

 

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

Sign in to reply to this post.