Wrapper Class in Salesforce Tutorial

Wrapper Class in Salesforce Tutorial

Last updated on 08th Oct 2020, Blog, Tutorials

About author

Babu ((Associate Director - Salesforce ) )

He is a Proficient Technical Expert for Respective Industry & Serving 7+ Years. Also, Dedicated to Imparts the Informative Knowledge to Freshers. He Share's this Blogs for us.

(5.0) | 12547 Ratings 1966

What is Wrapper Class in Salesforce?

You learned about Salesforce in-depth in the previous sections of this Salesforce tutorial. Let’s now learn ‘What is a wrapper class in Salesforce?’

A wrapper class is nothing but a collection of different Salesforce data types. In Salesforce, you can combine multiple data types and utilize them for various purposes. For example, there is a wrapper class that can access the account records and display an in-page block table. You have an option of a checkbox, and you can associate multiple serial numbers to display the number of records. So, there can be multiple records and respective serial numbers.

When elucidating the architecture of Salesforce, you can see that it is somewhat similar to a database wherein the records are defined as rows, the fields are determined as columns, and the objects are defined as tables. Having different collections of classes assigned to facilitate a successful development process, Salesforce possesses the MVC architecture.

Subscribe For Free Demo

Error: Contact form not found.

MVC Architecture

Salesforce UI has a Model-View-Controller (MVC) model, and here metadata is stored to view it as XML later. A build-up of Visualforce pages and lightning components that can be used as needed comprises the view layer. A controller is basically an Apex Class extended to the controller; data is relinquished to accessible models first to view the elements. Developers are limited to a data structure of a particular object type while the relationship between these data structures is necessary. In order to handle various objects collectively, you will need only a single object for an individual field. This condition might be confusing in the beginning, but some practice will help you get through this.

Following image shows the MVC architecture in Salesforce:

the -MVC- architecture -in- Salesforce

There are two types of controllers: standard controllers and custom controllers. Controllers are responsible for modeling and viewing objects, components, and pages.

the -MVC- architecture -in- Salesforce

As can be seen from the image above, a single contact can have many orders wherein each order can have various order lines. This kind of nested structure is exemplified through a JSON or NoSQL database.

Wrapper Class in Salesforce as a Solution

What is a wrapper class in Salesforce? A wrapper class is used mainly to wrap data collected from the present objects to a new object. An sObject is defined as a distinct type that doesn’t include a database or any relevant field; it doesn’t have any API page but is definitely a valid object.

In other orders, a wrapper class is a container class; it is a data structure and an abstract data type that contains various objects and their members together. A wrapper class is also called a custom object which is determined by a developer with a particular set of properties, e.g., a custom class in Salesforce contains fields and each field has a definite data type. Similarly, a wrapper class is also a custom class that has various properties and data types, which can be determined according to the requirement. If you want to wrap various data objects, there is no better option than wrapper classes in Salesforce.

Advantages of a Wrapper Class in Salesforce

  • When considering the JSON structure, the wrapper class structure is comparable to the data visualization technique on a particular web page.
  • There isn’t any requirement for passing the map structure for browsing elements or for managing the relationship between objects.
  • If you want to pass an sObject, there isn’t any penalty for the same, and it can also be extendable to the class constructors.
  • Data can be organized perfectly when it is nested.

A wrapper class and a container class can be kept together, but it is better if they are stored separately. When they are kept independently, it is easy for them to be maintained and reused whenever required, thus preventing code duplication.

Therefore, it is clear enough that a wrapper class is used to construct a new object within the Apex code which can consolidate a set of fields that are most importantly required at the runtime or various fields from different objects. This provides extra control and flexibility over the data objects to view and control the elements.

Wrapper Class as an Everyday Tool in Salesforce

Let’s now talk about why wrapper class is described as an everyday Salesforce tool. By saying that it means, all Salesforce Developers use wrapper classes, along with other Salesforce tools, on a daily basis. This is normal in Visualforce wherein some of the queries are performed on the data before presenting it to the user.

A few activities included are as follows:

  • Connecting values concurrently for various fields of an object using a mapping table
  • Presenting various threads of data mutually in a single list
  • Enhancing a data object by extracting the data from another object

The most suitable solution for all these concerns is wrapper classes. Every wrapper class can have different specifications as designed, which is based on its user terms, but the comprehensive structure is more manageable for all of the wrapper classes.

A wrapper class doesn’t have an inner class determined by default, yet the practice of having an inner class can be more efficient and simple in Salesforce. A wrapper class is used similarly as unions, structures, or algebraic types for any other class; the method is limited with a bounded introspection and with possible dynamism in Apex.

In order to construct any powerful wrapper class, you should use properly designed properties in your wrapper class rather than opting for a highly complex rendering. Also, have in mind that there is a huge requirement for enforcing support manually so that the data restructuring can come with some particular obligation to enforce all the permissions properly.

Sorting Wrapper Class Collection in Apex

A predefined functionality in Apex is used to classify fundamental data types in Salesforce. A lot of developers have faced this situation wherein they had to classify the custom data types according to force.com. Still, how would force.com know about the sorting of a class? And, how would it know the data usage within a class?

The Verdict

A wrapper class is a proven enhancement of growth for Salesforce Developers with its wider range of solutions, along with its functionalities. It is used everywhere, and Salesforce Developers have realized its benefits too.

Whenever you want to compare custom objects or you need to add more functionalities, you can try using wrapper classes in your own way. You can also add many fields within an object whenever required and maximize the usability of an application. With this, we come to the end of this section on ‘What is wrapper class in Salesforce?’

Course Curriculum

Learn On-Demand Salesforce Training with Advanced Concepts By Industry Experts

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

Salesforce Governor Limits

Governor limits in Salesforce are used to ensure the effective use of resources on the Force.com multi-tenant platform. In order to execute the code efficiently, Salesforce.com sets some limits.

Before continuing, let’s take a look at what you will learn in this tutorial on Governor Limits in Salesforce:

1.Salesforce Governor Limits

2.What are Governor Limits

3.Types of Governor Limits

  • Per-transaction Certified Managed Package Limits
  • Static Apex Limits
  • Per-transaction Apex Limits
  • Lightning Platform Apex Limits
  • Size-specific Apex Limits

4.Advantages of Governor Limits in Salesforce

5.How can we avoid Salesforce Governor Limits?

What are Governor Limits?

To ensure that no one acquires resources from others, Force.com sets many restrictions that limit normal code execution. Salesforce has to do this because of its multi-tenant architecture, where all organizations and customers share a single resource. If one of the governor limits is not met, an error will be raised and the program will stop or halt. Therefore, it is important to ensure that our code is scalable and does not violate governor limits. These limits are called the single transaction basis, a single trigger execution.

Types of Governor Limits in Salesforce

  • Per-transaction Certified Managed Package Limits
  • Static Apex Limits
  • Per-transaction Apex Limits
  • Lightning Platform Apex Limits
  • Size-specific Apex Limits
types-of-governor-limits

Per-transaction Certified Managed Package Limits

Certified managed packages are managed packages that have passed AppExchange security checks for most transaction limits. Certified managed packages are developed by Salesforce ISV partners and installed in our org from AppExchange under a unique namespace.

With this governor limit, there is no limit to the number of certified namespaces that can be accessed through a single transaction. However, the prerequisite is that the number of operations that can be performed in a single namespace does not exceed the per-transaction limits. In addition, the total number of operations that can be performed in any transaction across the namespace is limited. The total limit for each namespace is 11 times. Shared limits on all namespaces are not affected by collective limits, such as a limit on maximum CPU time.

DescriptionCumulative Cross-namespace Limits
The total number of SOQL queries issued1,100
The total number of records retrieved by Database.getQueryLocator110,000
The total number of SOSL queries issued220
The total number of DML statements issued1,650
The total number of callouts (either HTTP requests or Web services calls) in a transaction1,100
The total number of sendEmail methods allowed110

All transaction limits count individually for certified managed packages expect:

  • Maximum CPU time
  • Total heap size
  • Maximum number of unique namespaces
  • Maximum transaction execution time

Each of these limits applies to all transactions, even as the number of certified managed packages running in the same transaction. If we install an AppExchange package created by an uncertified Salesforce ISV partner, the code for that particular package will not have its own governor limits. All resources used by the package are counted toward the total governor limit of the org. All collection resource messages and warning messages are also generated based on the corresponding package namespace.

Static Apex Limits

So far, we know that there are different governor limits for each description we give in Apex. There are more governor limits for Static Apex, i.e., for different kinds of callouts, queries, loops, records, and batch sizes, along with different transactions. Let’s look at these limits individually:

DescriptionLimits
The default timeout of callouts (either HTTP requests or Web services calls) in a transaction10 seconds
The maximum size of the callout request or response (either HTTP request or Web services call)6 MB for synchronous Apex or 12 MB for asynchronous Apex
The maximum SOQL query runtime before Salesforce cancels the transaction120 seconds
The maximum number of class and trigger code units in the deployment of Apex5,000
Apex trigger batch size200
For loop list batch size200
The maximum number of records that are returned for a Batch Apex query in Database.QueryLocator50 million
Course Curriculum

Enroll in Salesforce Certification Course to UPGRADE Your Skills

Weekday / Weekend BatchesSee Batch Details

Per-transaction Apex Limits

Per-transaction Apex Limits are used for counting each Apex transaction. When we talk about Batch Apex, these specific limits in the execution method for executing each batch of records will be reset. The following table summarizes the limits for the synchronous and asynchronous Apex:

DescriptionSynchronous LimitsAsynchronous Limits
The total number of SOQL queries issued100200
The total number of records retrieved by SOQL queries50,000
The total number of records retrieved by Database.getQueryLocator10,000
The total number of SOSL queries issued20
The total number of records retrieved by a single SOSL query20,000
The total number of DML statements issued150
The total number of records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin10,000
The total stack depth for any Apex invocation that recursively fires triggers with insert, update, or delete statements16
The total number of callouts (either HTTP requests or Web services calls) in a transaction100
The maximum cumulative timeout for all callouts (either HTTP requests or Web services calls) in a transaction120 seconds
The maximum number of methods with the future annotation allowed per Apex invocation500 in batch and future contexts and 1 in the queueable context
The maximum number of Apex jobs added to the queue with System.enqueueJob501
The total number of sendEmail methods allowed10
The total heap size6MB12MB
The maximum CPU time on Salesforce servers10,000 milliseconds60,000 milliseconds
The maximum execution time for each Apex transaction10  minutes
The maximum number of push notification method calls allowed per Apex transaction10
The maximum number of push notifications that can be sent through each push notification method call2,000

Lightning Platform Apex Limits

The following limits do not apply to Apex transactions and are therefore managed by the Lightning platform.

DescriptionLimit
The maximum number of asynchronous Apex method executions (Batch Apex, future methods, Queueable Apex, and Scheduled Apex) per 24-hour periodEither 250,000 or the number of user licenses in the org multiplied by 200, whichever is greater
The number of synchronous concurrent transactions for long-running transactions, which last longer than 5 seconds for each org.10
The maximum number of Apex classes scheduled concurrently100. In Developer Edition org, the limit is 5
The maximum number of Batch Apex jobs in the Apex flex queue that is in the Holding status100
The maximum number of Batch Apex jobs queued or active concurrently5
The maximum number of Batch Apex job start method concurrent executions1
The maximum number of batch jobs that can be submitted in a running test5
The maximum number of test classes that can be queued in a 24-hour period (in Production org other than Developer Edition)Either 500 or 10 multiplied by the number of test classes in the org, whichever is greater
The maximum number of test classes that can be queued in a 24-hour period (Sandbox and Developer Edition org)Either 500 or 20 multiplied by the number of test classes in the org, whichever is greater
The maximum number of query cursors open concurrently per user50
The maximum number of query cursors open concurrently per user for the Batch Apex start method15
The maximum number of query cursors open concurrently per user for Batch Apex execute and finish methods5

Size-specific Apex Limits

Size-dependent Apex Limits are specifically designed to ensure that there are no oversized items in classes, triggers, org, etc.

The following table gives the limits for each of these:

DescriptionLimit
The maximum number of characters in a class1 million
The maximum number of characters in a trigger1 million
The maximum amount of code used by all Apex codes in org16 MB
The method size limit 265,535 bytecode instructions in a compiled form

Advantages of Governor Limits in Salesforce

  • Governor limits in Salesforce prevent other orgs from using and hence executing lengthy code, which can take up a lot of space in the memory and even the entire cloud CPU.
  • Apex has completely different or unique coding limits.
  • These governor limits help us stay in the right space of coding with Apex.
Sales Force Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

How can you avoid Salesforce Governor Limits?

As a developer, we need to ensure that our code is scalable and does not exceed the governor limits. But how? Follow this process to meet the governor limits in Salesforce:

  • Do not have DML statements or SOQL queries in our FOR loop
  • Try not to use SOQL or DML operations in the loop
  • Try to bulkify the code and helper methods
  • Query large data sets
  • Use Batch Apex if we want to process 50,000 records
  • Streamline various triggers on the same object
  • Use streamlining queries and collections for loops

Are you looking training with Right Jobs?

Contact Us

Popular Courses