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!

1
  • By the way: How it even saved/compiled? You should recieve error AFAIK Jul 23, 2018 at 14:32

1 Answer 1

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}
3
  • 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, 2018 at 14:42
  • @user1974566 I know this is an old question, but what did you mean by: "You're setting properties via getters and setters. That's why only two values are set.". All 3 are set up in the same way - so why would only 2 be set?
    – cyberspy
    Nov 25, 2019 at 10:16
  • docInfos.numberOfSelectedDocs = 1; docInfos.hasSelectedDocs = true That's 2 :) Aura automatically get rid of property which is not set (is null by default) Nov 25, 2019 at 18:17

You must log in to answer this question.

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