2

How to pass the array value from the form to an apex controller?.. Meaning this form gonna display 2 or more times depend on the product quantity.. But the problem is that I need to pass the values to apex data in array type. How can I achieve this?..

This is the vf page.

<apex:inputField value="{!pea.Attendee_Name__c}" styleClass="form-control"/>
<apex:inputField value="{!pea.Email_Address__c}" styleClass="form-control"/>
<apex:inputField value="{!pea.Title__c}" styleClass="form-control"/>                                                       
<apex:inputField value="{!pea.Company__c}" styleClass="form-control"/>
<apex:inputField value="{!pea.Dietary_Requirements__c}" styleClass="form-control"/>
<apex:inputField value="{!pea.Comments__c}" styleClass="form-control"/>

This is the apex code.

public ProntoEvents_Attendee__c pea{get;set;}
2

If you know the quantity upfront in your controller, then you can instantiate multiple objects to serve as placeholders and chuck them in a map with the key being an Integer index (1 to MAX_QUANTITY). Then you can iterate through the indexes and pull the corresponding placeholder object instance from the map and display it on the page. A quick starting point example:

Controller:

public Map <Integer, ProntoEvents_Attendee__c> peas {get;set;}

// In your constructor or your page action method
public YOURCLASS()
{
    peas = new Map <Integer, ProntoEvents_Attendee__c> ();

    // Create multiple instances (placeholders) 
    for (Integer i = 1; i <= PRODUCT_QUANTITY; i++)
    {
        peas.put(i, new ProntoEvents_Attendee__c());
    }
}

VisualForce Page:

<apex:repeat value="{!peas}" var="index">
    <apex:outputText value="Attendee {!index}" />
    <apex:inputField value="{!peas[index].Attendee_Name__c}" styleClass="form-control"/>
    <apex:inputField value="{!peas[index].Email_Address__c}" styleClass="form-control"/>
    <apex:inputField value="{!peas[index].Title__c}" styleClass="form-control"/>                                                       
    <apex:inputField value="{!peas[index].Company__c}" styleClass="form-control"/>
    <apex:inputField value="{!peas[index].Dietary_Requirements__c}" styleClass="form-control"/>
    <apex:inputField value="{!peas[index].Comments__c}" styleClass="form-control"/>
    <br />
</apex:repeat>

Controller Save Method:

public pageReference save()
{
    List <ProntoEvents_Attendee__c> attendeesToInsert = new List <ProntoEvents_Attendee__c> ();

    for (ProntoEvents_Attendee__c attendeee : peas.values())
    {
        // Check only for populated fields (blank should be ignored I guess)
        if (attendee.Attendee_Name__c != null)
        {
            attendeesToInsert.add(attendee);
        }
    }

    insert attendeesToInsert;
}

This is not the only and not necessarily the best approach, but it's one of the many possible solutions. Tweak it to suit your needs.

  • Thanks anyway to your reply.. I will try this.. For further explanation about my problem sir.. Here is the link.. salesforce.stackexchange.com/questions/84731/… – Lennon Leopoldo Jul 28 '15 at 2:03
  • In order to fit this in your solution, I reckon you'll need to extend it a little bit and create a map of a map in order to keep track of attendees related to a product: Map <Id, Map <Integer, ProntoEvents_Attendee__c>>. I'll leave that exercise to you. – Boris Bachovski Jul 28 '15 at 3:23

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.