I am actually getting the error "Unable to read sobjet", when sending an object to my server even if I specify the sobjectType as follow :
sendforSave : function(component) {
var habitatInfo = component.get("v.habitatInfo");
var habitatTUP = {sobjectType : "Habitat__c"};
habitatTUP.Id = (habitatInfo.Id == "" ? null : habitatInfo.Id);
habitatTUP.Name = habitatInfo.Name;
console.log('## habitat before saving :', habitatTUP);
var action = component.get("c.saveIdentityInfo");
action.setParams({
hab : habitatTUP
});
action.setCallback(this, function(response) {
if (response.getState() === "SUCCESS") {
var result = response.getReturnValue();
console.log("## updated dossier info is :",result);
}
else{
component.find('xid-alert').showAlert("error",action.getError()[0].message);
}
});
$A.enqueueAction(action);
}
Apex code is as follow :
@AuraEnabled
public static DossierTechnique__c saveIdentityInfo(Habitat__c hab){
system.debug('## starting method saveIdentityInfo ');
system.debug('## habitat received :' + hab);
//case 1 : A new habitat has been created
if(hab.Id == null){
system.debug('## A new habitat has been created');
......
}
........
}
In the case the habitat object I am created has an Id it doesn't fail. But I want to send to my server an instance to insert, thus, it doesn't have an Id. How can I achieve this, without sending a wrapper or a json to my server ? Can't salesforce convert it to the right object with the sobjectType specified only?