Skip to content

Instantly share code, notes, and snippets.

@martyychang
Last active May 16, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martyychang/76867c92f19808b6a7b7 to your computer and use it in GitHub Desktop.
Save martyychang/76867c92f19808b6a7b7 to your computer and use it in GitHub Desktop.
Why does passing an array of Lead objects to an Apex controller action cause an "internal salesforce.com error"?
<aura:component controller="ccmt.OneLeadController">
<aura:attribute name="items" type="Lead[]"/>
<aura:attribute name="newLead" type="Lead"
default="{ 'sobjectType': 'Lead' }"/>
<!-- Event handlers -->
<aura:handler name="init" value="{!this}" action="{!c.handleInit}"/>
<ui:inputText aura:id="firstName"
label="First Name" value="{!v.newLead.FirstName}"
required="false"/>
<ui:inputText aura:id="lastName"
label="Last Name" value="{!v.newLead.LastName}"
required="true"/>
<ui:inputText aura:id="company"
label="Company Name" value="{!v.newLead.Company}"
required="true"/>
<ui:inputEmail aura:id="email"
label="Work Email" value="{!v.newLead.Email}"
required="true"/>
<ui:button label="Add Lead" press="{!c.addLead}"/>
<ui:button label="Submit Lead" press="{!c.save}"/>
</aura:component>
({
addLead : function(component, event, helper) {
var leads = component.get("v.items");
leads.push(component.get("v.newLead"));
component.set("v.items", leads);
helper.initNewLead(component);
},
handleInit : function(component, event, helper) {
helper.initNewLead(component);
},
save : function(component, event, helper) {
var self = this;
var createLead = component.get("c.createLead");
createLead.setParams({
"newLead": component.get("v.newLead")
});
createLead.setCallback(self, function(a) {
console.log("returned: %o", a.getReturnValue());
});
$A.enqueueAction(createLead);
}
})
({
initNewLead : function(component) {
var self = this; // safe reference
var getNewLead = component.get("c.newLead");
getNewLead.setCallback(self, function(a) {
console.log("returned: %o", a.getReturnValue());
component.set("v.newLead", a.getReturnValue());
});
$A.enqueueAction(getNewLead);
}
})
<aura:application controller="ccmt.OneLeadController">
<aura:attribute name="leads" type="Lead[]"/>
<h1>Lightning-to-Lead</h1>
<ccmt:inputLeadList items="{!v.leads}"/>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Company</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.leads}" var="lead">
<tr>
<td>{!lead.FirstName}</td>
<td>{!lead.LastName}</td>
<td>{!lead.Company}</td>
<td>{!lead.Email}</td>
</tr>
</aura:iteration>
</tbody>
</table>
<ui:button label="Submit Leads" press="{!c.save}"/>
</aura:application>
public class OneLeadController {
@AuraEnabled
public static Id createLead(Lead newLead) {
insert newLead;
return newLead.Id;
}
@AuraEnabled
public static List<Id> createLeads(List<Lead> newLeads) {
insert newLeads;
List<Id> newLeadIds = new List<Id>();
for (Lead eachLead : newLeads) {
newLeadIds.add(eachLead.Id);
}
return newLeadIds;
}
@AuraEnabled
public static Lead newLead() {
return (Lead)Lead.sObjectType.newSObject(null, true);
}
}
({
save : function(component, event, helper) {
var self = this;
var createLeads = component.get("c.createLeads");
createLeads.setParams({
"newLeads": component.get("v.leads")
});
createLeads.setCallback(self, function(a) {
console.log("returned: %o", a.getReturnValue());
})
$A.enqueueAction(createLeads);
}
})
@amulhai
Copy link

amulhai commented May 16, 2016

I am seeing one gap inputLeadListController.js..file. while clicking on first Save button record was not creating. and then I did some small changes in it. and its working perfectly.

Now this js look like this.

({
addLead : function(component, event, helper) {
var leads = component.get("v.items");
leads.push(component.get("v.newLead"));
component.set("v.items", leads);

    helper.initNewLead(component);
},
handleInit : function(component, event, helper) {
    helper.initNewLead(component);
},
save : function(component, event, helper) {
     var self = this;
     var led=component.get("v.newLead");
     led.sobjectType='Lead';

    var createLead = component.get("c.createLead");
    createLead.setParams({"newLead": led});
    createLead.setCallback(self, function(a) {
        console.log("returned: %o", a.getReturnValue());
    });

    $A.enqueueAction(createLead);
}

})

@amulhai
Copy link

amulhai commented May 16, 2016

I added this sobjectType

var led=component.get("v.newLead");
led.sobjectType='Lead';
..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment