function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EldonEldon 

Passing sobject instances from JScontroller to apex and upserting them.

Hi,

I have an attribute named 'cart' which is of type mycart__c(custom object) in my component and i get it using
cartJS=component.get("v.cart"); in my js controller.

Now i am trying to pass it to my apex class using 

var action3=component.get("c.getOldcart");
action3.setParams({ "currentcart" : cartJS});


where getOldcart is my apex function.

 @auraenabled
    public static void getOldcart (list<mycart__c> currentcart){
        
        system.debug('currentcart'+currentcart);
        for(mycart__c singleitem:currentcart){
               system.debug('singleitem'+singleitem);
        }
}



I am getting the values in my currentcart when i debug but when i iterate through it i am getting the following error,

" FATAL_ERROR System.UnexpectedException: Salesforce System Error: 612434560-135286 (152924272) (152924272)"

Is there anything wrong in the above codes?

 
Veena Sundara-HeraguVeena Sundara-Heragu
One problem I see is that you mention that the attribute 'cart' is of type mycart__c but your controller method is expecting the parameter of type List<mycart__c>.  So, if you want to pass 'cart' as a parameter, either you whould change the type of the attribute to 'my_cart__c[]' or create a list in javascript and "push" 'cart' into that list before calling the controller method.

Also, I have noticed that I get such errors when passing anything other than primitives to apex controllers.  So, the way to saolve this is, JSON.stringify your obejct and pass to controller as a string.  Then, in your apex controller, use JSON.deserializeStrict

So, in your case (assuming that 'cart' is of type 'mycart__c[]':

var action3=component.get("c.getOldcart");
action3.setParams({ "currentcart" : JSON.stringify(cartJS)});



@auraenabled
    public static void getOldcart (String currentcart){
        
        system.debug('currentcart'+currentcart);
        List<mycart__c> carts = (List<mycart__c>)System.JSON.deserializeStrict(currentcart, List<mycart__cr>.Class);
        for(mycart__c singleitem: carts){
               system.debug('singleitem'+singleitem);
        }
}

Let me know if that works