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 

creating records for child objects within repeat values

So I have a visualforce page that kind of displays a to do list, and I have two objects - Items and Activities. Activities are the children of the Items, and have a few custom fields. I want to display all the existing items and their activities and then be able to create new items or activities on each item. I can't figure out how to create activities for the specific Item they're under... any help would be nice. Here's my code: 

 

Visualforce Page:

<body>
    <div id="pageBlock" class="container_12">
        <div id="Header">
            <div class="grid_12">
                <p id="title">Whiteboard</p>
                <p id="newWBItem">New Whiteboard Item</p>
            </div>
            <apex:form >
                <apex:pageBlock >
                      <apex:pageBlockButtons location="bottom" styleClass="save_button">
                          <apex:commandButton action="{!save}" value="Save" rerender="Items"/>
                      </apex:pageBlockButtons>
                      <apex:pageBlockSection showHeader="false" id="wb_item_input">
                          <apex:inputField value="{!wbitem.Name}"/>
                      </apex:pageBlockSection>
                  </apex:pageBlock>
            </apex:form>
        </div>
        <div id="Items">
        <apex:form >
        <apex:pageBlock >
            <div id="itemSpec" class="container_12">
            <apex:repeat value="{!whiteboard}" var="item" id="itemList">
                <div id="ItemHeading">
                    <div class="grid_8">
                        <apex:outputText value="{!item.Name}" id="itemName"/><br/>
                    </div>
                    <div class="grid_3">
                        <apex:commandLink value="New Activity" id="newAct"/>   |   
                        <apex:commandLink action="{!deleteItem}" value="Delete Item" id="theCommandLink">
                            <apex:param name="ItemId" value="{!item.Id}"/>
                        </apex:commandLink>
                    </div>
                </div>
                <apex:repeat value="{!item.Whiteboard_Item_Activities__r}" var="act" id="actList">
                    <div id="Item Activities">
                        <div class="grid_9">
                             <apex:outputText value="{!act.Name}" id="actSub"/><br/>
                        </div>
                        <div class="grid_3">
                            <apex:outputText value="{!act.Due_Date__c}" id="actDue"/><br/>
                        </div>
                        <div class="grid_9">
                            <apex:outputText value="{!act.Activity_Description__c}" id="actDes"/><br/>
                        </div>
                    </div>
                </apex:repeat>
                <div class="grid_12">
                <apex:pageBlock >
                      <apex:pageBlockButtons location="bottom" styleClass="save_button_act">
                          <apex:commandButton action="{!newAct}" value="Save" rerender="Items">
                              <apex:param name="ItemId" value="{!item.Id}"/>
                          </apex:commandButton>
                      </apex:pageBlockButtons>
                      <apex:pageBlockSection showHeader="false" id="wb_item_act_input">
                          <apex:inputField value="{!wbitemAct.Name}"/>
                          <apex:inputField value="{!wbitemAct.Due_Date__c}"/>
                          <apex:inputField value="{!wbitemAct.Activity_Description__c}"/>
                      </apex:pageBlockSection>
                </apex:pageBlock>
                </div>
            </apex:repeat>
            </div>
        </apex:pageBlock>
        </apex:form>
        </div>
    </div>
</body>

 

 

Controller:

public class WhiteboardCtrl {

    private List<Whiteboard_Item__c> wboard {get; private set;}

    public WhiteboardCtrl() {
        wboard = [SELECT Name, Id,
                     (SELECT Name, Due_Date__c, Activity_Description__c
                      FROM Whiteboard_Item_Activities__r ORDER BY CreatedDate)
                  FROM Whiteboard_Item__c 
                  ORDER BY CreatedDate];
    }
    
    public Whiteboard_Item__c wbitem {
        get{
            if (wbitem == null)
                wbitem = new Whiteboard_Item__c();
            return wbitem;
        }
        set;
    }
    
    public Whiteboard_Item_Activity__c wbitemAct {
        get{
            if (wbitemAct == null)
                wbitemAct = new Whiteboard_Item_Activity__c();
                
            return wbitemAct;
        }
        set;
    }
    
    public PageReference deleteItem() {
        Whiteboard_Item__c goner = [SELECT id, name from Whiteboard_Item__c where id = :ApexPages.currentpage().getparameters().get('ItemId')];
        
        try {
            delete goner;
        } catch (DmlException e) {
        }
        PageReference curPage = ApexPages.currentPage();
        curPage.setRedirect(true);
        return null;
    }
    
    public Whiteboard_Item__c[] getWhiteboard() {
        return wboard;
    }

    public PageReference save() {
        try {
            insert wbitem;
        } catch(DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
      return null;
    }

    public PageReference newAct() {
    
       Whiteboard_Item__c item = [SELECT Name, Id,
                     (SELECT Name, Due_Date__c, Activity_Description__c
                      FROM Whiteboard_Item_Activities__r ORDER BY CreatedDate)
                  FROM Whiteboard_Item__c 
                  WHERE id = :ApexPages.currentpage().getparameters().get('ItemId')
                  ORDER BY CreatedDate];
                  
        List <Whiteboard_Item_Activity__c> acts = item.Whiteboard_Item_Activities__r;
        acts.add(wbitemAct);
        try{upsert item;} catch(DMLException e) {ApexPages.addMessages(e); return null;}
        return null;

    }

}

 

I just cant figure out how to save the Activities to the right item, at least when I try to do it nothing shows up on the refresh. Maybe I'm just not showing the values correctly? Not sure. Thanks in advance

jeremyyjeremyy

 

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

Instead of binding to item.Id, it looks it should be wbitemAct.ItemId__c. I'm assuming the field is called ItemId__c. Replaces this with the correct field name.

jeremyyjeremyy

Scratch that.. before upserting, you need to set  the  parent of wbitemAct from the ItemId parameter.

woodmanzeewoodmanzee

I don't really get what you're saying... I should change the apex:param? or something else? Anything more specific would be helpful

jeremyyjeremyy

You have to set the parent Item of wbitemAct.