0

I,m trying to save data into a custom object from a repeated form in visualforce page. This is the visualforce code. Can I do do this using array? or list? or map?.. any idea?..

<apex:form>
<apex:pageMessages />
<div class="details">
<div class="form-horizontal">
    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
        <apex:variable value="{!1}" var="rowNum"/>
        <apex:repeat var="selectedproduct" value="{!selectedproducts}">  
        <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="heading{!selectedproduct.Id}{!rowNum}">
                <h5 class="ticket-type">Ticket Type</h5>
                <h4 class="panel-title">{!selectedproduct.ProntoEvents_Product_Name__c}</h4>
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse{!selectedproduct.Id}{!rowNum}" aria-expanded="false" aria-controls="collapse{!selectedproduct.Id}{!rowNum}">
                    <div class="icon-group">
                        <i class="gear-icon"></i><i class="glyphicon glyphicon-triangle-top"></i><i class="glyphicon glyphicon-triangle-bottom"></i>
                    </div>
                </a>
            </div>
            <div id="collapse{!selectedproduct.Id}{!rowNum}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading{!selectedproduct.Id}{!rowNum}">
                <div class="panel-body">

                    <apex:outputPanel rendered="{!selectedproduct.Show_Attendee_Name__c == true}">
                    <div class="form-group">
                        <label for="firtname" class="col-sm-4 control-label"><i class="asterisk">*</i> Attendee Name</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Attendee_Name__c}" styleClass="form-control"/>
                        </div>
                    </div>
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!selectedproduct.Show_Email_Address__c == true}">
                    <div class="form-group">
                        <label for="emailaddress" class="col-sm-4 control-label"><i class="asterisk">*</i> Email Address</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Email_Address__c}" styleClass="form-control"/>
                        </div>
                    </div>
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!selectedproduct.Show_Title__c == true}">
                    <div class="form-group">
                        <label for="confirmemail" class="col-sm-4 control-label"><i class="asterisk">*</i> Title</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Title__c}" styleClass="form-control"/>                                                        </div>
                    </div>
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!selectedproduct.Show_Company__c == true}">
                    <div class="form-group">
                        <label for="confirmemail" class="col-sm-4 control-label"><i class="asterisk">*</i> Company</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Company__c}" styleClass="form-control"/>
                        </div>
                    </div>
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!selectedproduct.Show_Dietary_Requirements__c == true}">
                    <div class="form-group">
                        <label for="confirmemail" class="col-sm-4 control-label"><i class="asterisk">*</i> Dietary Req.</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Dietary_Requirements__c}" styleClass="form-control"/>
                        </div>
                    </div>
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!selectedproduct.Show_Comments__c == true}">
                    <div class="form-group">
                        <label for="confirmemail" class="col-sm-4 control-label"><i class="asterisk">*</i> Comments</label>
                        <div class="col-sm-8">
                            <apex:inputField value="{!pea.Comments__c}" styleClass="form-control"/>
                        </div>
                    </div>
                    </apex:outputPanel>

                </div>
            </div>
        </div>
        <apex:variable var="rowNum" value="{!rowNum + 1}"/>
        </apex:repeat>   
    </div>
    <div class="form-group">
    <label for="confirmemail" class="col-sm-4 control-label"></label>
        <div class="col-sm-8">
            <apex:commandButton action="{!SaveAttendee}" value="PROCEED" styleClass="btn btn-success btn-proceed"/>
        </div>
    </div>
</div>
</div>
</apex:form>

But when I debug, it displays only the last data/form values.

DEBUG|ProntoEvents_Attendee__c:{Attendee_Name__c=zzxc, Email_Address__c=zxc@gmail.com, Title__c=zxc, Company__c=zxc, Dietary_Requirements__c=qwe, Comments__c=qwe}

How could I save the the values from multiple form?

enter image description here

This is my apex code.

public List<ProntoEVents_Product__c> getSelectedProducts() {

          List<ProntoEvents_Booking_Product__c> bookingproducts;
          List<ProntoEvents_Product__c> selectedproducts;
          List<ProntoEVents_Product__c> listproducts;

          listproducts = [SELECT Name, ProntoEvents_Product_Name__c, Show_Attendee_Name__c, Show_Comments__c, Show_Company__c,Show_Dietary_Requirements__c,Show_Email_Address__c,Show_Title__c  FROM ProntoEvents_Product__c Where Id IN('a0T9000000Jple3','a0T9000000JplfL')];

          bookingproducts =  [SELECT Name, Quantity__c, ProntoEvents_Booking__c, ProntoEvents_Product__c  FROM ProntoEvents_Booking_Product__c Where Id IN('a0e9000000Bp7Ug','a0e9000000BpADo')];

         selectedproducts = new List<ProntoEVents_Product__c>();
          for (Integer i = 0; i < listproducts.size(); i++) { 

              if(listproducts[i].Id == bookingproducts[i].ProntoEvents_Product__c)
              {
                  for(Integer j=0; j<bookingproducts[i].Quantity__c; j++){

                      selectedproducts.add(listproducts[i]);
                  }
              }
          }
          return selectedproducts;

      }

This is the visualforce code for the SaveAttendee.

public PageReference SaveAttendee(){      


         system.Debug(pea);

        return null;  
     }

Initialization of pea

public ProntoEvents_Attendee__c pea{get;set;}
  • It would help if you posted your controller code; otherwise we can't tell where your debug output has come from. – Rob Jul 24 '15 at 8:21
  • @SpongeBob updated sir. – Lennon Leopoldo Jul 24 '15 at 8:23
  • That's helpful, but it still doesn't show where you're using System.debug() to get the output above – Rob Jul 24 '15 at 9:02
  • @SpongeBob already added sir.. – Lennon Leopoldo Jul 24 '15 at 9:20
  • 1
    Please include the code where pea is defined and populated – Rob Jul 24 '15 at 9:25
1

After looking at this code for a bit, I identified the issue. It will not work the way you are doing it.

Cause of issue

  • apex repeat is on a list of ProntoEVents_Product__c list while you are trying to gather details on a single instance of ProntoEvents_Attendee__c
  • Single instance of an object will always save the last value of your repeat, that is why you are getting the last value only

What you can do

I am not sure of complete use case here but this statement -

<apex:repeat var="selectedproduct" value="{!selectedproducts}"> 

Needs to iterate over a List so that whatever changes you make will directly be reflected in your controller list variable.

Hope this helps. Let me know for further doubts in same..!!

Thanks,

Ray

  • What would I do to make it multiple data instantiate?. is public ProntoEvents_Attendee__c[] pea{get;set;} applicable?.. or List<ProntoEvents_Attendee__c>[] pea{get;set;}?.. I tried them already.. But still I got error's.. – Lennon Leopoldo Jul 27 '15 at 9:38
  • Yes you will need to use List<ProntoEvents_Attendee__c>[] pea{get;set;} along with your page side Apex repeat iterating over this list somehow, if we are updating this list. – Kaushik Ray Jul 27 '15 at 9:45
  • how about in vf page?.. I will not change something?.. {!pea.Attendee_Name__c} still works?.. I will try this.. Hope it works.. – Lennon Leopoldo Jul 27 '15 at 9:46
  • still no hope.. I got this error unexpected token: '[' at line 11 column 36.. – Lennon Leopoldo Jul 27 '15 at 9:58
  • Ok 2 things you will need to do. Use first apex repeat on ProntoEVents_Product__c list and second apex repeat on ProntoEvents_Attendee__c list to get the inputs. If you can create a structure like that then you can possibly get all the inputs in controller – Kaushik Ray Jul 27 '15 at 10:07
1

Let's suppose the relationship field in your ProntoEvents_Attendee__c object pointing to ProntoEVents_Product__c is called ProntoEVents_Product__c.

In your controller:

public List<ProntoEvents_Attendee__c> getAttendeeList() {
    List<ProntoEvents_Attendee__c> attendeeList = new List<ProntoEvents_Attendee__c>();
    List<ProntoEVents_Product__c> selectedProducts = getSelectedProducts();

    for(ProntoEVents_Product__c selectedProduct : selectedproducts) {
        ProntoEvents_Attendee__c attendee = new ProntoEvents_Attendee__c(ProntoEVents_Product__c = selectedProduct);
        attendeeList.add(attendee);
    }
}

In your page:

<apex:repeat var="pea" value="{!AttendeeList}">  
        <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="heading{!pea.ProntoEVents_Product__r.Id}{!rowNum}">
                <h5 class="ticket-type">Ticket Type</h5>
...

Replace all the corresponding selectedproduct into pea.ProntoEVents_Product__r.

Haven't tested this and not quite sure whether this will cause field not queried issue. But hopefully this will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.