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

Question

Question

Deleting a row in another table based on conditions in another table

asked on July 8

Greetings,

I have a scenario where a duplicate row in table 2 must be deleted based on a condition in table 1.

If invoice number and document name from table 1 matches with invoice number and document name in table 2 and the user has selected one of the option: (pre-approve or reject or review) from the radio button in table 1, then delete the row duplicate in table 2.

0 0

Replies

replied on July 8

Which form designer are you using? This action would require JavaScript

0 0
replied on July 8

I’m using the classic form designer, I think this has to be scripted in jquery, I might be incorrect though. 

0 0
replied on July 8

Ok this should work, or at least get you most of the way there. You should add a class to each table, I chose source/target invoice table. You should then add the same classes or update the selectors for the variables on lines 9-14 (I assumed all single line text fields).

You also need to determine when you want to delete the rows. right now i'm just doing it on doc load, but presumably you'd want to run this after some user event happens.

 

const sourceTable = $('.source-invoice-table')
const targetTable = $('.target-invoice-table')

// define business logic needed to determine if a row should be deleted in target

const shouldDeleteFromTarget = () => {
  const removeRows = [];
  // business logic here
  const sourceInvoiceNameCol = sourceTable.find('.invoice-name [type="text"]');
  const sourceDocNameCol = sourceTable.find('.doc-name [type="text"]');
  const sourceDesicionOptionCol = sourceTable.find('.decision-option [type="radio"]');

  const targetInvoiceNameCol = targetTable.find('.invoice-name [type="text"]');
  const targetDocNameCol = targetTable.find('.doc-name [type="text"]');

  if (targetDocNameCol.length === 0 || targetInvoiceNameCol.length === 0) {
    return removeRows;
  }
  for (let i = 0; i < sourceInvoiceNameCol.length; i++) {
    const sourceDesicionOption = sourceDesicionOptionCol[i].value;
    // if source decision is not one of the options, skip
    if (sourceDesicionOption !== "pre-approve" || sourceDesicionOption !== "reject" || sourceDesicionOption !== "review") {
      continue;
    }

    const sourceInvoiceName = sourceInvoiceNameCol[i].value;
    const sourceDocName = sourceDocNameCol[i].value;
    // if source invoice name and doc name are empty, skip
    if (!sourceInvoiceName || !sourceDocName || sourceInvoiceName === '' || sourceDocName === '') {
      continue;
    }

    // check if source invoice name and doc name are in target
    for (let j = 0; j < targetInvoiceNameCol.length; j++) {
      const targetInvoiceName = targetInvoiceNameCol[j].value;
      const targetDocName = targetDocNameCol[j].value;
      if (sourceInvoiceName === targetInvoiceName && sourceDocName === targetDocName) {
        // push the row index to removeRows
        removeRows.push(j);
      }
    }
  }
  return removeRows;
}

// on document ready
$(() => {
  // TODO: determine when you want to run deletion
  const removeRows = shouldDeleteFromTarget();
  // remove rows from target table
  const deleteRowButton = targetTable.find('.cf-collection-delete,.cf-table-delete');
  removeRows.forEach((rowIndex) => {
    deleteRowButton.get(rowIndex)?.click();
  });
});

 

0 0
replied on July 9

Hi Zachary,

I tried to replicate this code in my scenario but it doesn't do the work.

So this is the idea behind the scenario, lets say row 1 on the main table has a radio button checked as rejected and the values matches with another row in the cash table, the entire row in the cash table must be removed because its found in the main table.

0 0
replied on July 9

Did you update the selectors to match what you have on the form?

Also, can you check the conditions on line 22 and make sure they match exactly what is in the values for the Option column? You could put "console.log(sourceDesicionOption)" on line 21 to see what the value is returning for that field.

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

Sign in to reply to this post.