1

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?

3
  • can you provide apex code? Commented Mar 10, 2019 at 19:47
  • I modified to add the apex code also
    – vanessen
    Commented Mar 10, 2019 at 19:54
  • Strange, same code works at my end. WHich browser are you using? Commented Mar 10, 2019 at 20:29

1 Answer 1

2

Please verify that the sobjectType parameter is spelled correctly. If you're using an object in a namespace, you may also need the namespace for it to operate correctly (e.g. "packagens__objectname__c").

The following code works in my org:

({
    doInit: function(component, event, helper) {
        var action = component.get("c.method");
        action.setParams({record: {sobjectType:"Account", Name:"Test", Id: null}});
        action.setCallback(this, function(result) {
            // No error here!
            console.log(result.getError());
        });
        $A.enqueueAction(action);
    }
})

But the following code will not run in my org:

({
    doInit: function(component, event, helper) {
        var action = component.get("c.method");
        action.setParams({record: {sobjectType:"Account411", Name:"Test", Id: null}});
        action.setCallback(this, function(result) {
            // Unable to read SObject error...
            console.log(result.getError());
        });
        $A.enqueueAction(action);
    }
})
3
  • My sobjectType is correct, because i don't get the error when an Id is specified. It only generate error when no Id specified. Does it check agains sobjectType only when no id specified?
    – vanessen
    Commented Mar 11, 2019 at 8:04
  • @vanessen Correct. If an ID is specified, the type is inferred from there. Therefore, your sobjectType field is incorrect.
    – sfdcfox
    Commented Mar 11, 2019 at 10:07
  • Its not this. Its so strange, this lightning thing is making me loosing more than a day to debug that stupid error. the sobjectType is good sure about it, yet it another thing.
    – vanessen
    Commented Mar 11, 2019 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .