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

Question

Question

How to Automatically Set Dropdown Value Based on Field Value in Laserfiche Forms?

asked two days ago

Hi everyone,

I'm working on a Laserfiche Forms process and need help with implementing a specific condition using JavaScript.

In my form, I have a table with two fields:

  • Risk Level (Field ID: 80)

  • Significant (Dropdown, Field ID: 82)

What I want to achieve is:

If the Risk Level is 16 or higher, the Significant dropdown should automatically be set to "YES" for that row.

The fields are inside a repeating table, and I'm looking for the best way to handle this using the LFForm JavaScript API.

Has anyone done something similar or have a working script for this use case?

Thanks in advance!

0 0

Replies

replied two days ago Show version history

Hi Rafael, Try this code, is for Modern Design Form. 

LFForm.onFieldChange(() => {
  const numericValue = parseFloat(LFForm.getFieldValues({ fieldId: 1 })); // Replace with your actual Risk Level numeric field ID
  const dropdownField = { fieldId: 2 }; // Replace with your actual dropdown Significant field ID

  if (numericValue > 16) {
    LFForm.setFieldValues(dropdownField, "Yes");
  } else {
    LFForm.setFieldValues(dropdownField, "No");
  }
}, { fieldId: 1 }); // This triggers the logic when the Risk Level numeric field changes

Let me know if works.

0 0
replied two days ago

Hi Luis, I tried the code but it's not working. is there any work-around for this if the javascript is not working? 

0 0
replied one day ago

I tried it in different scenarios, and it worked for me. Look at the image I posted here. And try this code, and open the inspection code in your browser in the console tab, and see what error you can see there related to the JavaScript, and share here. I included codes to debug in case have an error. 

LFForm.onFieldChange(() => {
  try {
    const fieldIdRisk = 1; // Replace with your actual Risk Level numeric field ID
    const fieldIdDropdown = 2; // Replace with your actual dropdown Significant field ID

    const rawValue = LFForm.getFieldValues({ fieldId: fieldIdRisk });
    console.log(`Raw value from fieldId ${fieldIdRisk}:`, rawValue);

    const numericValue = parseFloat(rawValue);
    if (isNaN(numericValue)) {
      console.error(`Error: The value from fieldId ${fieldIdRisk} is not a valid number. Received:`, rawValue);
      return;
    }

    const dropdownField = { fieldId: fieldIdDropdown };
    const newValue = numericValue > 16 ? "Yes" : "No";

    console.log(`Setting dropdown fieldId ${fieldIdDropdown} to:`, newValue);
    LFForm.setFieldValues(dropdownField, newValue);
  } catch (error) {
    console.error("An unexpected error occurred:", error);
  }
}, { fieldId: 1 });

Let me know how it went. 

testing21.gif
testing21.gif (3.7 MB)
0 0
replied one day ago

The code works for 1 row, but not for any additional rows.  @████████ Since the table is repeating are there a specific number of rows or can the end user add as needed?

1 0
replied one day ago

Hi All, on Rafael screen shot, shouldn't the last line that trigger the field be 80 and not 82?

0 0
replied one day ago

Yes

0 0
replied one day ago

Following up on what @████████ mentioned about whether the fields are inside a table, I'm trying to provide a solution with the following code:

/* ------------ CONFIG ------------- */
const tableId         = 7;  // (Replace this with your Table ID)
const numericFieldId  = 80;  // Your Risk Level ID: 80
const dropdownFieldId = 82;  // Yes/No dropdown Significant ID: 82
/* --------------------------------- */

/* ---- core function: sync every row ---- */
function syncRows() {
  let nums = LFForm.getFieldValues({ fieldId: numericFieldId });

  // nums is a string if there’s just one row; make it an array
  if (!Array.isArray(nums)) nums = [nums];

  nums.forEach((val, idx) => {
    const num      = parseFloat(val) || 0;
    const desired  = num > 16 ? "Yes" : "No";
    const ddSel    = { fieldId: dropdownFieldId, index: idx };

    if (LFForm.getFieldValues(ddSel) !== desired) {
      LFForm.setFieldValues(ddSel, desired);
    }
  });
}

/* ---- bind once ---- */
// while user types
LFForm.onFieldChange(syncRows, { fieldId: numericFieldId });
// when user tabs/clicks away
LFForm.onFieldBlur (syncRows, { fieldId: numericFieldId });

// run once at start-up (prefilled forms or first blank row)
syncRows();

PS: I'd like to clarify that I'm not a programming expert. Although I understand the code, I use ChatGPT and the AI ​​implemented at doc.laserfiche.com to find some solutions like these. If you find any errors or anything, I apologize.

Perhaps some professional JavaScript developers can provide better solutions.

1 0
replied one day ago

Perfect, that worked Luis!

1 0
replied one day ago Show version history

I'm glad it works @████████. I hope it works for @████████ too.

Thank you for your support! 

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

Sign in to reply to this post.