0

I want to write a lightning component. The lightning component should show some informations about uploaded documents. Firstly I created the following class:

public class DocInformations {
    @AuraEnabled
    public integer numberOfSelectedDocs { get; set; }
    @AuraEnabled
    public integer numberOfUploadedDocs { get; set; }
    @AuraEnabled
    public boolean hasSelectedDocs { get; set; }

    public DocumentInformations() {
        this.numberOfSelectedDocs = 0;
        this.numberOfUploadedDocs = 0;
        this.hasSelectedDocs = false;
    }
}

Secondly I created an object and fill some values. And finally I want to return the object as a json string.

DocInformations docInfos = new DocInformations();
docInfos.numberOfSelectedDocs = 1;
docInfos.hasSelectedDocs = true;
return JSON.serialize(docInfos);

But the field numberOfUploadedDocs is missing in the response json string:

{ "numberOfSelectedDocs": 1, "hasSelectedDocs": true }

I'am unsing a constructor to fill all fields with default values. I need the dafault values in the response and I don't want to write them every time again. Could someone tell me what's wrong with the constructor? Thanks!

  • By the way: How it even saved/compiled? You should recieve error AFAIK – user1974566 Jul 23 '18 at 14:32
2

Answer is:

You're not using default constructor because your default constructor name doesn't match class name.

Its DocInformations and DocumentInformations

Constructor name should also be named DocInformations

You're setting properties via getters and setters. That's why only two values are set.

public class DocInformations {
@AuraEnabled
public integer numberOfSelectedDocs { get; set; }
@AuraEnabled
public integer numberOfUploadedDocs { get; set; }
@AuraEnabled
public boolean hasSelectedDocs { get; set; }

public DocInformations() {
    this.numberOfSelectedDocs = 0;
    this.numberOfUploadedDocs = 0;
    this.hasSelectedDocs = false;
}
}

DocInformations docInfos = new DocInformations();
docInfos.numberOfSelectedDocs = 1;
docInfos.hasSelectedDocs = true;
system.debug(JSON.serialize(docInfos));

USER_DEBUG [4]|DEBUG|{"numberOfUploadedDocs":0,"numberOfSelectedDocs":1,"hasSelectedDocs":true}
  • Yeah, you're right.. I changed the code a lot to test why the constructor never was set and I forgot to change the constructor methodname. Thanks! – mg92 Jul 23 '18 at 14:42

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.