1

i'm looking for some Lightning advice on saving multiple sObjects in a multi-record editor Lightning app.

I'm using 2 components in the app, an OpportunityLineItem List component and an OpportunityLineItem edit component.

The list component looks something like:

...
<aura:attribute name="olis" type="OpportunityLineItem[]"/>
...
<aura:iteration items="{!v.olis}" var="oli">
    <c:OpportunityLineItemEditRow oli="{!oli}" />
</aura:iteration>
...

A user can make changes to one or more records and then press a save button, which sends the records to the back-end controller and makes the save.

My question: What would be recommended for this scenario? Have the List component handle the save as [], or let the individual record components handle the save themselves?

The first option might be more efficient, the latter will make error handling easier.

Any links to code for a similar scenario would be appreciated.

1

Saving an entire list, is probably the better way to do it. You can have a List of Opportunity Line Items, in which you store all of the Line Items that have been edited. Then pass this list to the save method and attempt to save the entire list at once.

<aura:attribute name="olisToSave" type="OpportunityLineItem[]"/>

On edit action:

var olis = component.get("v.olisToSave");
olis.push(thisOli);//or however you add stuff to an sObject Array...
component.set("v.olisToSave", olis);

And finally in the save function:

var action = component.get("c.saveOlis");
action.setParams({
    "olis": component.get("v.olisToSave")
});
$A.enqueueAction(action);

Then in your Apex Controller:

@AuraEnabled
public static void saveOlis(List<OpportunityLineItem> olis){
    update olis;
}
6
  • Hi Moshe, thanks for your answer. This is similar to how I initially implemented this, but when I started to think about the error handling, I was inclined to move this logic to the individual component solution, so that partial saves would also work. How would you handle save errors in this scenario? – jheuvel Sep 22 '15 at 7:29
  • Depending on what kind of errors you're trying to catch, you may be able to catch them prior to the mass save (client side). If you can't, then I guess you would just save one at a time... – Moshe Karmel Sep 24 '15 at 13:03
  • I have implemented both methods, to see what would work best. An additional consideration here, is that when an event is sent to the individual line components to perform a record save, these remote actions will all end-up in the same execution context in apex. (due to the optimization the framework performs when sending calls to the back-end) This could result in hitting governor limits, depending on your situation. I found that you can enforce separate contexts by applying a small delay, but would like it better if the framework supported this. However, I have not found a way to do so yet. – jheuvel Sep 30 '15 at 13:15
  • Another finding is that exceptions thrown from Apex, do not show up as a useful error message in the response, but as the, by now infamous, "An internal server error has occurred Error ID: xxxx..." message... Anyone found a way around that yet? – jheuvel Sep 30 '15 at 13:20
  • Probably the easiest way of preventing these is to throw a try{}catch(){} block around your controller method and return a status string or an error string. Actually, I often return an Result object, with a success bool and a message. – Caspar Harmer Aug 30 '16 at 6:10

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.