3

I have an attribute in lightning component and it has a value like this:

<aura:attribute name="samListObj" type="Object"/>

[{"created":"","description":"","docId":"","docType":"Claims Documents","docTypeList":[],"fileName":""}, 
{"created":"","description":"","docId":"","docType":"Client Identification Documents","docTypeList":[],"fileName":""}, 
{"created":"","description":"","docId":"","docType":"Execution Only","docTypeList":[],"fileName":""}, 
{"created":"","description":"","docId":"","docType":"Invoice","docTypeList":[],"fileName":""},
{"created":"","description":"","docId":"","docType":"Ongoing Advice Agreements","docTypeList":[],"fileName":""}]

Now I am passing the value into my Apex Class Controller from my lightning controller, like this:

var action = component.get('c.controllerMethod');
action.setParams({
 'samValue':component.get('v.samListObj')
});
action.setCallBack(this,function(result){
  if(result.getState()){
   console.log('SUCCESS');
  }
});
$A.enqueueAction(action);

Now in Apex code I have a wrapper class with the same field from my attribute like this:

public class DocumentWrapper {
    @AuraEnabled 
    public String docId = ''; 
    @AuraEnabled 
    public String docType = ''; 
    @AuraEnabled 
    public String fileName = '';
    @AuraEnabled 
    public String created = '';  
    @AuraEnabled 
    public String description = '';   
    @AuraEnabled 
    public List<String> docTypeList = new List<String>();      
}

So for my method that calls to my lightning js controller:

@AuraEnabled
public static void updateAPDesciptionCFCheck(List<DocumentWrapper> samValue){
   System.debug(samValue);
}

Now I tried to debug the value from my lightning component but it returns empty in every fields:

DocumentWrapper:[created=, description=, docId=, docType=, docTypeList=(), fileName=], 
DocumentWrapper:[created=, description=, docId=, docType=, docTypeList=(), fileName=], 
DocumentWrapper:[created=, description=, docId=, docType=, docTypeList=(), fileName=], 
DocumentWrapper:[created=, description=, docId=, docType=, docTypeList=(), fileName=], 
DocumentWrapper:[created=, description=, docId=, docType=, docTypeList=(), fileName=]

For the docType field it should have a value base from the value of the attribute in my component. But I couldn't pull the data.

Any help please?

NOTE: I updated the actual display in my debug log.

2
  • Could you try sending serialized list and deserialize it on server side? And I believe 'DocumentWrapper' is defined as an outer class?
    – Raul
    Jun 24, 2019 at 15:58
  • My wrapper is inside in my Controller Class Jun 24, 2019 at 16:10

1 Answer 1

4

Did some quick testing, and there are 2 options to resolve the issue:

  1. Irrespective of whether your wrapper class is outer or inner, add {get; set;} to @AuraEnabled properties for data binding to work.

    public class DocumentWrapper {
        @AuraEnabled 
        public String docId { get; set; } 
        @AuraEnabled 
        public String docType { get; set; }
        @AuraEnabled 
        public String fileName { get; set; }
        @AuraEnabled 
        public String created { get; set; }
        @AuraEnabled 
        public String description { get; set; }
        @AuraEnabled 
        public List<String> docTypeList { get; set; }     
    }
    

    Quoting from documentation- Passing Data to an Apex Controller:

    Each property in the Apex class must have an @AuraEnabled annotation, as well as a getter and setter.

  2. Another safer option which I prefer to use is to send a serialized JSON string to server side and deserialize that in apex method.

    From client side controller, you can serialize the attribute to string with JSON.stringify:

    action.setParams({ 'samValue' : JSON.stringify(component.get('v.samListObj')) });
    

    Then in apex controller method, deserialize it as:

    public with sharing class Class_267042 {
        @AuraEnabled
        public static void updateAPDesciptionCFCheck(String samValueString){
            List<DocumentWrapper> samValue = (List<DocumentWrapper>) JSON.deserialize(samValueString, List<DocumentWrapper>.class);
        }
    }
    
8
  • How do I send a serialized JSON string into server side? That's what I am looking for. Jun 24, 2019 at 16:37
  • @LeorahSumarong added more details in the answer
    – Raul
    Jun 24, 2019 at 16:43
  • I tried using the 1st option, But I am getting an error, Do I need to convert the value from my controller like JSON.stringify? Jun 24, 2019 at 16:43
  • I got this error "Method does not exist or incorrect signature: void serialize(String, System.Type) from the type System.JSON", I tried to used the second option. Jun 24, 2019 at 16:49
  • is it serialize or deserialize? Jun 24, 2019 at 16:51

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.