Expand my Community achievements bar.

SOLVED

dynamically generate multiple instances of same subform

Avatar

Level 3

I have checkbox fields where the user can select up to 10 accounts.  For each account, I need to have a certain subform displayed.  So if the user selects 5 accounts I need 5 occurrences of the same subform.

Can this be done programmatically?  Or do I have to have 10 subforms and just hide/show what I need?  I prefer the former, if can be done.

Please advise,

Thanks,

Elaine

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Sure, it's not too hard once you know what you're doing.  First, make sure all the "parent" subforms of your subform are set to be "Flowed" in the Object tab, and that your subform is set to "Repeat Subform For Each Data Item" in the Binding tab.  So, your user inputs the number of accounts they have in a numeric or text field (we'll call it Number_of_Accounts).  At this point, you could have the form generate the number of instances of the subform you want when the user exits this field, or you could have a button that says something like "Generate Accounts".  Either way, your script will be something like this:

_Accounts_Subform.count = Number_of_Accounts.rawValue;

This will generate the requested number of instances of the subform.  A warning-if the person changes the value of Number_of_Accounts after they've put one in (i.e. at first they said 5 but then changed it to 3), the instances at the end go away (so in our example the 4th and 5th instances would be deleted).  I'd suggest putting a warning in the same place you put the above script, so the whole thing will look like

if (_Accounts_Subform.count > Number_of_Accounts.rawValue) {

     if (xfa.host.messageBox("Warning! You are about to permanently delete all the accounts after number" + Number_of_Accounts.rawValue + ".  Are you sure you wish to proceed?", "Warning!", 3, 2) === 4) {

          _Accounts_Subform.count = Number_of_Accounts.rawValue;

     }

      else {

          Number_of_Accounts.rawValue = _Accounts_Subform.count

     }
}

else {

     _Accounts_Subform.count = Number_of_Accounts.rawValue;

}

Additionally, I would suggest putting a delete button with each instance of the subform, so they can delete instances one at a time and specifically (i.e. delete the first instance without deleting all subsequent instances).  One warning-make sure your "addresses" are correct (in this case Accounts_Subform and Number_of_Accounts).  If you're unsure if an address is correct, select the object you're scripting on, go into the script editor, hold Ctrl, and click, in the Design View, on the object you're trying to reference.  I'm sorry if this is unclear, but I can't think of a better way to explain this right now.

View solution in original post

3 Replies

Avatar

Correct answer by
Former Community Member

Sure, it's not too hard once you know what you're doing.  First, make sure all the "parent" subforms of your subform are set to be "Flowed" in the Object tab, and that your subform is set to "Repeat Subform For Each Data Item" in the Binding tab.  So, your user inputs the number of accounts they have in a numeric or text field (we'll call it Number_of_Accounts).  At this point, you could have the form generate the number of instances of the subform you want when the user exits this field, or you could have a button that says something like "Generate Accounts".  Either way, your script will be something like this:

_Accounts_Subform.count = Number_of_Accounts.rawValue;

This will generate the requested number of instances of the subform.  A warning-if the person changes the value of Number_of_Accounts after they've put one in (i.e. at first they said 5 but then changed it to 3), the instances at the end go away (so in our example the 4th and 5th instances would be deleted).  I'd suggest putting a warning in the same place you put the above script, so the whole thing will look like

if (_Accounts_Subform.count > Number_of_Accounts.rawValue) {

     if (xfa.host.messageBox("Warning! You are about to permanently delete all the accounts after number" + Number_of_Accounts.rawValue + ".  Are you sure you wish to proceed?", "Warning!", 3, 2) === 4) {

          _Accounts_Subform.count = Number_of_Accounts.rawValue;

     }

      else {

          Number_of_Accounts.rawValue = _Accounts_Subform.count

     }
}

else {

     _Accounts_Subform.count = Number_of_Accounts.rawValue;

}

Additionally, I would suggest putting a delete button with each instance of the subform, so they can delete instances one at a time and specifically (i.e. delete the first instance without deleting all subsequent instances).  One warning-make sure your "addresses" are correct (in this case Accounts_Subform and Number_of_Accounts).  If you're unsure if an address is correct, select the object you're scripting on, go into the script editor, hold Ctrl, and click, in the Design View, on the object you're trying to reference.  I'm sorry if this is unclear, but I can't think of a better way to explain this right now.

Avatar

Level 3

Wow!  Thanks for the great explanation!  So in other words, a dynamic subform behaves in the same way that a dynamic table does, I get it.  So to reference a field in the first subform, or a field in the second subform, I would just index into the array, perhaps something like _Accounts_Subform[0].fieldName?

Also, I've never referred to an object using the underscore (_Accounts_Subform vs. Accounts_Subform).  What does the leading underscore mean?  Usually I just create a variable that resolves to the node.

Thanks again!

Elaine

Avatar

Former Community Member

"So to reference a field in the first subform, or a field in the second subform, I would just index into the array, perhaps something like _Accounts_Subform[0].fieldName?"

I think that's how it's supposed to work, but my luck with that has been mixed.  If there's only one instance of a subform you don't need to use the [0] to reference it.

"Also, I've never referred to an object using the underscore (_Accounts_Subform vs. Accounts_Subform).  What does the leading underscore mean?  Usually I just create a variable that resolves to the node."

The underscore invokes the instanceManager, which allows you to change the number of instances of the subform.  The other way of invoking the instanceManager (Accounts_Subform.instanceManager) works exactly the same except if your subform has 0 instances.  In that case, you need to use the underscore method to go from 0 instances to 1 instance.  After that they're the same, but I always use the underscore.  Could you explain what you mean by creating a variable that resolves to the node?

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----