0
votes

I've been able to do this in PHP but it's not translating to Salesforce very well.

I have a form to input an Opportunity. It has an Account, a Contact, and a variable number of fields that will be used to create custom objects (For my purposes an Opportunity is a trip, and the custom objects are legs of that trip). The Salesforce controller needs to create a new Opportunity with the Account and Contact (that's the easy part) but then it needs to create a new custom object (Leg__c) for each leg of the trip.

My form looks like this:

<input type="text" name="Account" />
<input type="text" name="Contact" />
<div id="leg0">
  <input type="text" name="dep[0]" />
  <input type="text" name="arr[0]" />
</div>
<div id="leg1">
  <input type="text" name="dep[1]" />
  <input type="text" name="arr[1]" />
</div>
...

I'm not even sure where to begin on this one...

1

1 Answers

1
votes

Assuming you know how many legs you need, you can simply create a list of them in your visualforce controller:

public list<Leg__c> liLegs {get; set;};

// upon oppty creation:
liLegs = new list<Leg__c>();

for (integer i = 0; i < iNumLegs; i++)
{
    liLegs.add(new Leg__c());
}

Then you can just loop over these in your page like so:

<apex:repeat var="v" value="{!liLegs}">
    <apex:inputField value="{!v.Dep__c}"/>
    <apex:inputField value="{!v.Arr__c}"/>
</apex:repeat>

The input fields will correspond to the fields in each entry in the list, so then in your Save action or whatever you're using you can just insert the list insert liLegs;.

Hope this is of help and I haven't missed the mark, let me know if so! PS. I've just written this code directly in here so it may not be 100% syntactically correct ;)