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
Paul LightningPaul Lightning 

How to create multiple local objects with lightning components

Dear Salesforce experts,

I am building a lightning component where I am using a form with fields in order to create multiple opportunities.

I am already able to create multiple opportunities in my database but I am not able to create multiple local opportunities yet.
I want to first create multiple local opportunities that I can display as a list with certain fields that can be manually adjusted.

I very much appreciate help, since I couldn't find any solution although I was searching for it for one week!

Here is my code:

createMultipleOpportunities.cmp

<!-- form with fields that are used to create multiple opportunities, i don't think this code is relevant, therefore not shown here-->

<!--variable that is supposed to generate a collection or array where i can store my new local objects-->
<aura:attribute name="newChildOpportunities" type="Opportunity[]"/>

<!--Button that is supposed to create local opportunities-->
<ui:button label="Create child Opportunities" press="{!c.createChildOpportunitieslocally}"/>

<!--this part is supposed to iterate through all child opportunities, doesn't display them yet-->
 <table class="slds-table slds-table--bordered">
      <thead>
        <tr>
           <th scope="col"><span class="slds-truncate">Name</span></th>
           <th scope="col"><span class="slds-truncate">Child Deal Size</span></th>
        </tr>
      </thead>
      <tbody>
<aura:iteration items="{!v.newChildOpportunities}" var="opportunity">
   <tr>
     <td>{!opportunity.Name}</td>
     <td><ui:inputCurrency aura:id="childAmount" value="{!v.childDealSize}"/></td>    
  </tr>
</aura:iteration>
</tbody>
</table>

createMultipleOpportunitiesController.js
({
    createChildOpportunitieslocally: function(component, event, helper) {
        var i=0;
        for(i=0;i<5;i++){
            component.set("v.newChildOpportunity.Name", "child " + i);
            component.set("v.newChildOpportunity.Deal_Size__c", 500);
        }
    },
})

Let me know if you need further information.

Thank you in advance for your help :)!

Best regards,
Paul
jprichterjprichter

I changed Deal_Size__c to Amount, for demonstrative purposes, but you should be able to figure it out from here.

Change out your <tbody> to

 

<tbody>
    <aura:iteration items="{!v.newChildOpportunities}" var="opportunity">
        <tr>
            <td>{!opportunity.Name}</td>
        </tr>
        <tr>
            <td>
                <ui:inputCurrency value="{!opportunity.Amount}" />
            </td>
        </tr>
    </aura:iteration>
</tbody>


and your controller.js to something like

 

createChildOpportunitieslocally: function(component, event, helper) {
    var i = 0;
    var childOpportunities = [];
    var opp;
    for (i = 0; i < 5; i++) {
        opp = {};
        opp.sObjectType = "Opportunity";
        opp.Name = "child " + i;
        opp.Amount = 500;
        childOpportunities.push(opp);
    }
    component.set("v.newChildOpportunities", childOpportunities);
}