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

Question

Question

Modern Forms Checkbox Lookup

asked on April 8, 2024 Show version history

Hello,

 

I have a form which I would like to populate a checkbox based on a lookup value. If the field with CSS Class OnetoOne populates with the word True, I would like the checkbox field Field205 to check the first checkbox in the field. This is the script I'm using, but it's not checking the checkbox. Any suggestions? 

 

$(function() {
  $(document).ready(function () {
    var value = $('.OnetoOne input').val();
    switch (value){
      case "True": 
          document.getElementById('Field205-0').checked = true;
          break;
      
      default:
        alert('value');
    }
  });
});

 

0 0

Answer

SELECTED ANSWER
replied on April 10, 2024

Hey @████████- if you want to populate multiple checkboxes, you'll need to create an array of the values to set to the checkbox.  Setting one at a time like that will reset the checkboxes to just the single item each time.

Here's your code rewritten to call a function from any of the three completed lookups.  The function creates an empty array and then adds to the array for each of the field values it is checking.  Once the array has been fully built, it pushes the array to the checkbox field as the full list of values.

LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 23});
LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 29});
LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 25});

function PopulateCheckboxes() {
  var checkBoxArray = [];
  if (LFForm.getFieldValues({variableName: "Consultation"}) == "True") {
    checkBoxArray.push("Consultation");
  }
  if (LFForm.getFieldValues({variableName: "One_to_One"}) == "True") {
    checkBoxArray.push("One_to_One_Intervention");
  }
  if (LFForm.getFieldValues({variableName: "Push_In"}) == "True") {
    checkBoxArray.push("Push_In");
  }
  LFForm.setFieldValues({fieldId: 7}, {value: checkBoxArray});
}

 

0 0

Replies

replied on April 8, 2024
0 0
replied on April 9, 2024
LFForm.onLookupDone(function () {
    if (LFForm.getFieldValues({variableName: "One_to_One"}) === "True") {
        LFForm.setFieldValues({fieldId: 205(1)}, "True");
    }
}, {lookupRuleId: 30}); 

I can get this code to work when updating a text field, but I cannot figure out the language for a checkbox. 

0 0
replied on April 9, 2024 Show version history

@████████- are you using Laserfiche Cloud or are you on Prem?

Although they are similar, there are a few differences, so it helps to be precise regarding which you are using.  The Help topic that Zhiyong shared is for Laserfiche Cloud.  If you are on Prem, you might want to refer to this one instead (they are similar, but there are a couple differences).

The fact that you used {fieldId: 205(1)} makes me think you might need to use a selector like this: {fieldId: 205, index: 1}.  And checkboxes expect their value to be indicated like this: {value: ["Choice_1", "Choice_3"]}.  Put those together, and line 3 of your code will probably work like this: 

LFForm.setFieldValues({fieldId: 205, index: 1}, {value: ["True"]});

 

Note that I didn't put together a form to test this out, I'm just putting it together based on the instructions in the Help page.

1 0
replied on April 9, 2024

Thanks for your response. We are on prem. I tried your script, but it still didn't work. I'm very new to javascript so I've provided some more context. 

 

 

When Variable Name: One_to_One populates with true after the lookup, 

 

 

I would like the One to One Intervention box to be checked. This is field 205. 

 

0 0
replied on April 9, 2024
LFForm.onLookupDone(function () {
    if (LFForm.getFieldValues({variableName: "One_to_One"}) === "True") {
        LFForm.setFieldValues({fieldId: 205}, {value: ["One_to_One_Intervention"]});
    }
}, {lookupRuleId: 30});

I got it to work with this code. Thank you so much!

1 0
replied on April 9, 2024
LFForm.onLookupDone(function () {
    if (LFForm.getFieldValues({variableName: "Consultation"}) === "True") {
        LFForm.setFieldValues({fieldId: 205}, {value: ["Consultation"]});
    }
}, {lookupRuleId: 23});

LFForm.onLookupDone(function () {
    if (LFForm.getFieldValues({variableName: "One_to_One"}) === "True") {
        LFForm.setFieldValues({fieldId: 205}, {value: ["One_to_One_Intervention"]});
    }
}, {lookupRuleId: 29});

LFForm.onLookupDone(function () {
    if (LFForm.getFieldValues({variableName: "Push_In"}) === "True") {
        LFForm.setFieldValues({fieldId: 205}, {value: ["Push_In"]});
    }
}, {lookupRuleId: 25});

I can get the code to work for one criteria. Do you know what the field ID is for each checkbox option? For example, if I only want it to update the first option, how would I add that to my code? 

0 0
replied on April 10, 2024 Show version history

Hello,

I have string with format like "Choice_1,Choice_2,Choice_3" .

I want to set this dynamic values inside the checkbox but here dynamic variable won't be work and only able to set any of the values.

I want to checked values based on string as its dynamic so sometimes it has value like "Choice_1,Choice_3".

 

Can someone has logic for the same ?

Thanks,

Pratik

0 0
SELECTED ANSWER
replied on April 10, 2024

Hey @████████- if you want to populate multiple checkboxes, you'll need to create an array of the values to set to the checkbox.  Setting one at a time like that will reset the checkboxes to just the single item each time.

Here's your code rewritten to call a function from any of the three completed lookups.  The function creates an empty array and then adds to the array for each of the field values it is checking.  Once the array has been fully built, it pushes the array to the checkbox field as the full list of values.

LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 23});
LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 29});
LFForm.onLookupDone(PopulateCheckboxes, {lookupRuleId: 25});

function PopulateCheckboxes() {
  var checkBoxArray = [];
  if (LFForm.getFieldValues({variableName: "Consultation"}) == "True") {
    checkBoxArray.push("Consultation");
  }
  if (LFForm.getFieldValues({variableName: "One_to_One"}) == "True") {
    checkBoxArray.push("One_to_One_Intervention");
  }
  if (LFForm.getFieldValues({variableName: "Push_In"}) == "True") {
    checkBoxArray.push("Push_In");
  }
  LFForm.setFieldValues({fieldId: 7}, {value: checkBoxArray});
}

 

0 0
replied on April 10, 2024

Thank you so much Matthew; you have saved my time and it was works like charm :)

This is what I have modified in our logic as we have comma separated string like "Choice_1, Choice_3, Choice_2"

++++++++++++++++++++++++++++

LFForm.onFieldChange(
   async () => 
  {
     var checkBoxArray = [];
     let checkboxValues = LFForm.getFieldValues({ fieldId: 7 });
     var arr = checkboxValues.split(', ');
     var str = "";
     for (var i = 0; i < arr.length; i++)
     {
       checkBoxArray.push(arr[i]);
     }
    //console.log('Selected Specialties1:', checkBoxArray);
    LFForm.setFieldValues({fieldId: 6}, {value: checkBoxArray});
  },
  { fieldId: 7 }
);
++++++++++++++++++++++++++

 

Thanks,

Pratik

 

 

0 0
replied on April 10, 2024

Thank you so much! That was just what I needed. 

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

Sign in to reply to this post.