Wrapper Class In Salesforce

Author: Sivaprakash Sakthivel

A wrapper class is a class inside another class that contains a group of variables within a single object. These variables can be of similar data types or different data types.

Example: 

1. Wrapper Class

public class wrapperClassExample {
	
	public Account accInstance = new Account();
	public String status;
	public Boolean isSelected;
}

2. Wrapper Class with Constructor

public class wrapperClassExample {
	
	public Account accInstance = new Account();
	public String status;

	public wrapperClassExample (Account acc, String statusValue) {
		this.accInstance = acc;
		this.status = statusValue;
	}
}

Use of Wrapper Class in Apex

A wrapper class helps the developer to construct data values effectively and provides similar or different data type variables as a wrapper.

Let’s see how to pass the return values in a wrapper class to a Lightning Web Component and also learn how to use a wrapper class in integration.

1. Using Wrapper Class in LWC

If a wrapper class is used to pass values to an LWC, then the wrapper class variables should be annotated with @AuraEnabled.

Class : AccountDetails

public class AccountDetails {

	@AuraEnabled(cahceable=true)
	Public static List<accWrapper> getAccDetails() {

		List<accWrapper> accWrapperList = new List<accWrapper>();
		List<Account> accList = [SELECT Id, name, status
						FROM Account
						LIMIT 5];
	
	for (Account acc : accList) {
		accWrapperList.add(new accWrapper(acc, acc.status));
		}
	}

	public class accWrapper {
		@AuraEnabled
		public Account accInstance = new Account();
@AuraEnabled
		public String status;
		public accWrapper (Account acc, String statusValue) {
			this.accInstance = acc;
			this.status = statusValue;
		}
	}
}

LWC : displayComp.html

<template> 
<lightning-card title=”LWC”>
<template if:true={accWrapperList.data}> 
<template for:each={accWrapperList.data} for:item="wrap"> 
        Account Name : {wrap.accInstance.Name}
         Status : {wrap.status}
</template>
 </template> 
</lightning-card> 
</template>

LWC : displayComp.js

LWC component used to get data from Apex class.

import { LightningElement,wire } from 'lwc'; 
import getAccountDetails from '@salesforce/apex/AccountDetails.getAccDetails;
 
export default class displayComp extends LightningElement { 
@wire(getAccDetails) accWrapperList; 
}

2. Wrapper Class in Salesforce Integration

In Salesforce Integration, we receive the data in JSON format. So, we use a wrapper class to convert the JSON into Apex variables.


{
  "accountList": [
    {
      "totCost": 2.5,
      "ProdList": [
        {
          "QualityRate": 2,
          "Name": "Product-1"
        },
        {
          "QualityRate": 3,
          "Name": "Product-1.1"
        }
      ],
      "accountNumber": 1
    },
    {
      "totCost": 1.5,
      "ProdList": [
        {
          "QualityRate": 1,
          "Name": "Product-2"
        }
      ],
      "accountNumber": 2
    }
  ]
}

A wrapper class is used to convert the above JSON into Apex variables.

public class AccountWrapper{
	public cls_accountList[] accountList;
	class cls_accountList {
		public Double totCost;	//2.5
		public cls_ProdList[] ProdList;
		public Integer accountNumber;	//1
	}
	class cls_ProdList {
		public Integer QualityRate;	//3
		public String Name;	//Product-1.1
	}
}

2.1 JSON Deserialize


To convert JSON into an accountWrapper instance, we can use JSON.deserialize().

Http responseVariable = http.send(request);
String responseBody = responseVariable .getBody();

AccountWrapper accWrap = (AccountWrapper) JSON.deserialize(responseBody , AccountWrapper.class); 

2.2 JSON Serialize

To convert accountWrapper instances into JSON, we can use JSON.serialize().

String jsonValueString = JSON.serialize(accWrap);

jsonString is used to pass data in REST API as a body.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.