3

I have created a class with several fields like this:

    public class Tier{
        @AuraEnabled
        public String Name {get; set;}
        @AuraEnabled
        public integer Min {get; set;}
        @AuraEnabled
        public integer Max {get; set;}
        @AuraEnabled
        public Decimal Amount {get; set;}
        @AuraEnabled
        public integer cnt {get; set;} 
}

This is made in the apex controller and linked to a lightning component.

The lightning component has an attribute wich contains multiple "Tiers" as an array.

<aura:attribute name="tiers" type="StaffelPricingController.tier[]"/>

Than after editing it should be send back to the apex controller class for further processing like this:

    changeTiers : function(component){
    var tiers = component.get("v.tiers");
    var recordId = component.get("v.recordId");
    var action = component.get("c.getSave");
    action.setParams({
        "recordId": recordId,
        "tiers": tiers
    })
    $A.enqueueAction(action);
}

after this I want to have the list of tiers available for editing in apex so I did this:

public static string getSave (list<tier> tiers, string recordId){
//do some stuf here and return a string
}

now the value of tiers is not the correct type for processing, it seems to come in like some sort of JSON string that I can't process with de deserialize options.

Does anyone have any idea how te get a propper list of class back from a lightning component?

any help is appreciated, hints, tips, whatever

  • The string I'm getting back looks like this: ({cnt=1, Name=-}, {cnt=2, Name=-}, {cnt=3, Name=-}, {cnt=4, Name=-}) – Henk3000 Apr 29 '16 at 13:35
  • If I stringify it first it looks like this: [{"cnt":1,"Name":"-"},{"cnt":2,"Name":"-"},{"cnt":3,"Name":"-"}] Is there a way I can process it like this? – Henk3000 Apr 29 '16 at 13:53
3

You should be simply be able to use the list .

Do not try to DeSerialize ,its already deserialized and you can consume the list as it is

public static string getSave (list<tier> tiers, string recordId){
  //do some stuf here and return a string
  for(StaffelPricingController.tier t:tiers){

  }
}
  • I'm affraid it wasn't. A propper list looks like this: (Tier:[Amount=null, Max=null, Min=null, Name=-, cnt=1], Tier:[Amount=null, Max=null, Min=null, Name=-, cnt=2], Tier:[Amount=null, Max=null, Min=null, Name=-, cnt=3]) I finaly made it work by using the "Stringify" command in JAVA to create the propper JSON than I used this: tier[] records = (tier[])JSON.deserialize(tiers, List<tier>.class); – Henk3000 Apr 29 '16 at 14:06
  • 1
    Aura Enabled class should have done for you automatically .Annotating a class with auraenabled is meant to be doing this ,if not then i would log a case with SFDC. – Mohith Shrivastava Apr 29 '16 at 16:04
  • @MohithShrivastava I'm having an issue like this, it gives no compile errors. But getting FATAL_ERROR System.UnexpectedException: Salesforce System Error: 1996097431-19512 (-1753594556) (-1753594556) when it tries to loop the wrapper class. – Pasan Eeriyagama May 3 '17 at 11:08
1

You're not setting the callback of your action in the JS controller.

changeTiers : function(component){
  var tiers = component.get("v.tiers");
  var recordId = component.get("v.recordId");
  var action = component.get("c.getSave");
  action.setParams({
      "recordId": recordId,
      "tiers": tiers
  });

  action.setCallBack(this, function(a){
    if(a.getState() === 'SUCCESS'){
      component.set('v.result', a.getReturnValue());
      // a.getReturnValue() is the value returned from the apex class
    }

  });

  $A.enqueueAction(action);
}

Here is a link to the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.