In this blog we will try to understand what exactly is a "Wrapper Class" and how does it work? Everything you need to know will be on this blog.
As we Know, both users and developers can utilise Salesforce as a large and robust platform. It offers a tonne of pre-built tools and out-of-the-box functionality, but there are frequently difficulties when something special is required, such as when custom development and complex data type manipulation are necessary.To solve this problem, use a Wrapper Class.
What Is a Wrapper Class?
Wrapper Class is an Apex object that is similar to a custom object but it is created in code.Salesforce Developers frequently use Wrapper Classes to create new objects within the scope of Apex code. It assists users in condensing a collection of different fields (whether they belong to different objects or not) that are frequently required during runtime.This data exists only in code during your transaction and is not stored in the database.
or
In simple terms, Wrapper class is simply a collection of different Salesforce data types that allows us to combine and use multiple data types (Primitive data types, Collections, and sObjects) for various purposes.
Use Cases of a Wrapper Class:
Use Case 1:
Suppose while calling an External Service you are getting response in this format:
{"accountName":"SFDCops Inc", "Rating":"Hot"}
Now Based on this response we have to create an Account Record in Salesforce.
So to handle this firstly we have to create an Apex Class:
public String accountName;
public String accountRating;
}
Public static void handleData(string dataFromService){
ResponseOutputFromCall rs = (ResponseOutputFromCall)JSON.deserialize(dataFromService, ResponseOutputFromCall.class);
String acctName=rs.accountName;
String acctRating = rs.accountRating;
system.debug('Rating is:- '+acctRating);
Account acc=new Account();
acc.Name=acctName;
acc.Rating=acctRating;
insert acc;
}
}
Use Case 2:
Imagine you have to send some information to another external system. And that information is stored in more than one object inside Salesforce. Suppose you have a Quote record created inside salesforce, now you have to send that Quote to external system for an approval. Now the data you have to send is complex. lets take an example of data we are trying to send:
Now if you see the structure of the data that we have to send taking information from Quote, QuoteLineItems, Account, and Contact Object.
For this we have to use wrapper Class that will assist us in replicating the same data structure in Apex. To accomplish this, we must create a Class with the same variables and structure as in JSON.
public String accountId; // from Account Object
public String unit; // from Contact Object
public String contactName; // from Contact Object
public String contactEmail; // from Contact Object
public ShippingAddress shippingAddress; // from Quote Object
public List<QuoteLineItems> quotelineItems; // from QuoteLineItem Object
}
public String usageType;
public String streetName;
public String city;
public String state;
public String postalCode;
}
public class QuoteLineItems {
public String quantity;
public String productCode;
}
So to see the magic we will create a flow. After that we will create a quick action which will call this flow in Quote Object. This flow will pass the recordId and accountId of this quote Record to the Apex. Based on this recordId and accountId we will find all the information required to create the structure to send. Just for testing purpose either our callout is Successful or not we will return the data that we are sending to an external system in the flow to display it in a screen.
public class DataToSend{
public String accountId;
public String unit;
public String contactName;
public String contactEmail;
public ShippingAddress shippingAddress;
public List<QuoteLineItems> quotelineItems;
}
public class ShippingAddress {
public String usageType;
public String streetName;
public String city;
public String state;
public String postalCode;
}
public class QuoteLineItems {
public String unitPrice;
public String quantity;
public String productCode;
}
public class ApexOutputToFlow {
@InvocableVariable()
public String messagefromApex;
}
public class FlowInput{
@InvocableVariable(required=true)
public string quoteId;
@InvocableVariable(required=true)
public string acctId;
}
@InvocableMethod(callout=true label='Quote for Approval')
public static List<ApexOutputToFlow> passQuoteData(FlowInput[] params)
{
List<ApexOutputToFlow> outputFlow=new List<ApexOutputToFlow>();
string bodyforcallout='';
List<Quote> quote=[select id, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode,Account.Name,AccountId from quote where id=:params[0].quoteId Limit 1];
List<QuoteLineItem> quoteLine=[select QuoteId,UnitPrice, Quantity, Product2.ProductCode from QuoteLineItem where QuoteId =:params[0].quoteId];
List<Contact> conRecord=[select Id,Accountid,Name,Email,Unit__c from Contact where Accountid=:params[0].acctId AND isprimary__c=True LIMIT 1];
for(Quote quo:quote)
{
DataToSend fd=new DataToSend();
fd.accountId=quo.AccountId;
for(Contact con: conRecord)
{
fd.unit=con. Unit__c;
fd.contactName=con.Name;
fd.contactEmail=con.Email;
}
fd.ShippingAddress =new ShippingAddress();
fd.ShippingAddress.usageType='shipping';
fd.ShippingAddress.streetName=quo.ShippingStreet;
fd.ShippingAddress.city=quo.ShippingCity;
fd.ShippingAddress.state=quo.ShippingState;
fd.ShippingAddress.postalCode=quo.ShippingPostalCode;
fd.quotelineItems=new List<quotelineItems>();
for(QuoteLineItem quoteLineItems:quoteLine)
{
QuoteLineItems li=new QuoteLineItems();
li.productCode=quoteLineItems.Product2.ProductCode;
li.unitPrice=String.valueOf(quoteLineItems.UnitPrice);
li.quantity=String.valueOf(quoteLineItems.Quantity);
fd.quotelineItems.add(li);
}
bodyforcallout=JSON.serialize(fd);
}
Http http=new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('SOME URL');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody(bodyforcallout);
HttpResponse res = http.send(request);
if(res.getStatusCode()==201)
{
ApexOutputToFlow otpt=new ApexOutputToFlow();
otpt.messagefromApex=bodyforcallout;
outputFlow.add(otpt);
}
else
{
ApexOutputToFlow otpt=new ApexOutputToFlow();
otpt.messagefromApex=bodyforcallout;
outputFlow.add(otpt);
}
return outputFlow;
}
}
Output that we are getting after Clicking the "Send Quote For Approval Button":
We hope that you find this blog helpful, if you still have queries, don’t hesitate to contact us at omsfdc91@gmail.com.
No comments:
Post a Comment