html code

Salesforce Wrapper Class

Author: Neyaj Ansari, Tenetizer Technology

A wrapper class is a class nested within another class that encapsulates a set of variables within a single object. These variables can have similar or different data types.

Illustration:

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;
	}
}

Utilization of Wrapper Class in Apex

A wrapper class assists developers in efficiently organizing and encapsulating data values, incorporating variables of similar or different data types within a single wrapper.

Let’s explore how to pass return values within a wrapper class to a Lightning Web Component and also gain insights into using a wrapper class in integration scenarios.

1. Employing Wrapper Class in Lightning Web Component (LWC)

When employing a wrapper class to transmit values to a Lightning Web Component (LWC), it’s essential to annotate the wrapper class variables 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 Deserialization

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 Serialization

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

String jsonValueString = JSON.serialize(accWrap);

The ‘jsonString’ is employed to transmit data as the request body in a REST API