function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
woodmanzeewoodmanzee 

inputfield problems within nested repeat values

So I have a visualforce page that displays something like this (very generalized)

 

<apex:repeat>// repeats the "Item" Object

<apex:repeat>// repeats the "Activity" Object, a child of "Item"

</apex:repeat>

<apex:pageBlock>

<apex:pageBlockButtons>

<apex:commandButton action="{!newActivity}">

<apex:param name="ItemId" value="{!item.id}"/>

</apex:commandButton>

</apex:pageBlockButtons>

<apex:pageBlockSection>

<apex:inputField value="{!activity.Name}">

<apex:inputField value="{!activity.Date}">

</apex:pageBlockSection>

<apex:pageBlock>

</apex:repeat>

 

All the syntax is right, the page shows fine, but the problem is when I try to create a new Activity, it only works on the first iteration of the repeat. I.E., if I try to create an activity for Item 1, it works fine, but on any other Item it creates a record as if nothing was entered in the inputField. Any ideas why this is? How can I get it to save the correct values on every iteration? 

 

 

bob_buzzardbob_buzzard

I'd imagine this is because you are backing each input field on the page with the same object.    I.e. the activity.Name inputfield means every row in the repeat points at the same field. You may be able to get round this with actionregion tags, or you may need a wrapper class that combines the item with its potential new activity.

woodmanzeewoodmanzee

Yeah I guess that makes sense... so how would I use action region tags to fix that? Or a wrapper class? Any extra help would be appreciated, I'm fairly new at this. 

bob_buzzardbob_buzzard

A wrapper class is simply a custom class that encapsulates multiple pieces of information.  In this case it would wrap an instance of an Item object from the database and a new instance of an activity.  When you pull your items back from the database, create a new instance of the wrapper class for each one, plus a new activity and store in a list.  Then change the page so that the outer repeat is backed by the list of wrapper classes, item and new activity merge fields point at the contents of the wrapper class.  Finally, when the user clicks save, iterate the list of wrapper classes and insert only those activities that have fields populated.