1

Im fairly new to LWC so any advice is appreciated here. How do I create a new instance of the OnboardingPeopleResult apex class below using javascript of my LWC?

public class OnboardingPeopleResult {

    @AuraEnabled
    public String accountid {get;set;} 
    @AuraEnabled
    public String firstname {get;set;} 
    @AuraEnabled
    public String lastname {get;set;} 
    @AuraEnabled
    public String middlename {get;set;} 
    @AuraEnabled
    public OnboardingAddressResult employerAddress {get;set;}

    public OnboardingPeopleResult() { 
        this.accountid = '';
        this.firstname = '';
        this.lastname = '';
        this.middlename = '';
        this.employerAddress = new OnboardingAddressResult();
    }

}
1

I will go for a wrapper class in this case:

public class OnboardingPeopleResult {

 public OnboardingPeopleResult() {}

 public class OnboardingPeopleResultWrapper {
  @AuraEnabled public String accountid {get;set;} 
  @AuraEnabled public String firstname {get;set;} 
  @AuraEnabled public String lastname {get;set;} 
  @AuraEnabled public String middlename {get;set;} 
  @AuraEnabled public OnboardingPeopleResultWrapper employerAddress {get;set;}

   }

}

On javaScript you can call this wrapper like this.

import getOnboardingPeopleResultWrapper from '@salesforce/apex/OnboardingPeopleResult.OnboardingPeopleResultWrapper';

To fill in your wrapper you can just use dot notation:

getOnboardingPeopleResultWrapper.accountid;
getOnboardingPeopleResultWrapper.firstname;
// and so on ....

If you want to pass the wrapper back to the you apex controller you need a method to handle it, so add this to your apex controller:

@AuraEnabled public static recivingOnboardingPeopleResultWrapper(OnboardingPeopleResultWrapper onboardingPeopleResultWrapper) {
// Work your magic here.

 }
  • 1
    Why did you rename it with "wrapper" on the end? There's no need. Also, if you look at it this is a data object, not a wrapper. To be a wrapper it must be wrapping something - you would typically have a "wrapper" that "wraps" an SObject to add extra functionality, for example. That's not what we are doing here. – Phil W Nov 13 at 22:48
  • Thank you for your response. I was able to get this to work using my original class above. I added an AuraEnabled property to my main class that initialized the OnboardingPeopleResult class. Im running into an issue now with the employerAddress inner class, the LWC is unable to access the inner class' properties for some reason. [Cannot read property 'street1' of undefined] – user891859 Nov 14 at 19:08
  • Here's my inner class with the one property... ``` public class OnboardingAddressResult { @AuraEnabled public string street1{get;set;} public OnboardingAddressResult() { this.street1 = ''; } ``` – user891859 Nov 14 at 19:21

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.