I have a lightning component with an apex controller that wraps generic records in an inner class.
When I send the wrapped records to apex and receive them as generic SObjects, I get this error message:
System.JSONException: Nested object for polymorphic foreign key must have an attributes field before any other fields.
MyCtrl.cls
public class MyCtrl {
@AuraEnabled
public static WrappedSObject getWrappedSObject() {
return new WrappedSObject([SELECT Name FROM Account LIMIT 1]);
}
@AuraEnabled
public static void saveSObjectJSON(String sobjectJson) {
JSON.deserialize(sobjectJson, WrappedSObject.class);
}
// INNER
public class WrappedSObject {
@AuraEnabled
public SObject record;
public WrappedSObject(SObject record) {
this.record = record;
}
}
}
my.app
<aura:application controller="myCtrl" extends="force:slds">
<aura:attribute name="wrappedSObject" type="Map" />
<aura:handler name="init" action="{!c.init}" value="{!this}" />
<lightning:button label="send JSON" onclick="{! c.sendJSON }"/>
</aura:application>
myController.js
({
init : function(cmp, evt, hlp) {
var action = cmp.get("c.getWrappedSObject");
action.setCallback(this, function(response) {
cmp.set("v.wrappedSObject", response.getReturnValue());
});
$A.enqueueAction(action);
},
sendJSON : function(cmp) {
var action = cmp.get("c.saveSObjectJSON");
action.setParams({
sobjectJson : JSON.stringify(cmp.get("v.wrappedSObject"))
})
$A.enqueueAction(action);
},
})
But receiving it as typed Accounts works.
public class WrappedAccount {
@AuraEnabled
public Account account;
public WrappedAccount(Account account) {
this.account = account;
}
}
Is there a way to send my wrapper with generic SObjects from lightning to apex?