Blog

Apex Code is the Force.com programming language used by developers to build custom business logic on the platform by writing Apex Triggers, custom VisualForce controllers and extensions, Anonymous Apex codes, etc. It runs in a multi-tenant environment where a single resource can be shared by all different Salesforce organisations or instances. In this case, it is crucial to ensure that no organisation would monopolise the resources. Salesforce.com enforced a set of restrictions or limits to ensure the efficient use of resources in the force.com platform. These governor execution limits are applied per transaction basis, and when crossed, throws an error and halts the execution of the program.

As a developer, it is significant to ensure that our codes are scalable and can prevent the enforced limits. This article illustrates some of the key best practices for writing and designing Apex Code solutions on the Force.com platform. There are a lot of enforced limits by the platform, and here are some tips on how to avoid the most common limits exceeded by developers:

1. Avoiding SOQL queries and DML Calls in For Loops

The limits allow only 100 SOQL queries and 150 DML operations maximum per transaction otherwise it will throw an error. In the example below, a query was initiated inside the for loop [1.1]. In this case, if the trigger is initiated with more than 100 records, it will exceed the governor limits. The UPDATE DML call was also initiated inside the for loops [1.2]. In this case, if the records gathered are more than 150, it will breach the governor limits.

1.1 to 1.2 - Not optimised code

To avoid these governor limits to throw an error, we should ensure that queries are initiated outside the for loops [1.3], optimise the code by bulkifying it [1.4] to efficiently query the contacts and only perform a single update DML operation [1.5].

1.3 to 1.5 - Optimised code

2. Adding filters to SOQL queries

The limits allow only 50,000 or less than 50,000 records retrieved by SOQL queries per transaction otherwise it will throw an error. If the records retrieved are more than 50,000, it may breach the limit [2.1]. In this case, we can utilise logical conditions to filter our records [2.2].

2.1 - For loop without filters

2.2 - For loop with filters

3. Looping through large sets of data

Returning large set of queries may cause the exceeding of the heap limit [3.1]. In this case, a SOQL query in a for loop should be used instead [3.2]. In this case, it can process multiple batches of records through the use of internal calls to query and queryMore.

3.1 - Querying large sets of data

3.2 - Looping through large sets of data

To view the full set of execution governors and limits, you may access it here - Execution Governors and Limits.

              LEARN MORE

What Certification are you studying for now?

Focus on Force currently provides practice exams and study guides for sixteen certifications

Apex Best Practices – Avoid Exceeding Governor Limits

Apex Code is the Force.com programming language used by developers to build custom business logic on the platform by writing Apex Triggers, custom VisualForce controllers and extensions, Anonymous Apex codes, etc. It runs in a multi-tenant environment where a single resource can be shared by all different Salesforce organisations or instances. In this case, it is crucial to ensure that no organisation would monopolise the resources. Salesforce.com enforced a set… Read More

Apex Best Practices – Avoid Exceeding Governor Limits

Salesforce Lead Process

Salesforce is a CRM or Customer Relationship Management tool and as such, its primary use is to manage the relationship between a business and their customers. Prior to consumers or businesses becoming customers, however they start out as potential leads. In this post are the basics of how to utilize Salesforce to track leads, convert them to deals and best practice around when to do so.All businesses are unique so… Read More

Salesforce Lead Process

Focus on People: Nick Liechty

Nick Liechty is a next-generation innovative Salesforce leader with a unique combination of technical expertise and enterprise experience across a variety of industries. Having architected and led dozens of clients through their journey into the Salesforce ecosystem, he is definately one of the leaders of the pack. Below he shares some of his experiences and tips with you: How and Why did you get started with Salesforce? I was doing… Read More

Focus on People: Nick Liechty

Extending Social Sign-On in Communities

By: Warren Mangahas With an ever-expanding number of social media platforms, the need for companies to accommodate third-party logins is increasingly paramount. Even if you have a fantastic Salesforce Community set up, full of valuable resources and a vibrant user-base, the idea of having to sign up on another site and memorize another password may just be enough to turn someone off from signing up. This is where Salesforce Social Sign-On… Read More

Extending Social Sign-On in Communities

Customizing a Flow User Interface

Flows are a very useful way for developers to create standardize business processes and perform actions in Salesforce, without writing any code. Some examples of where Flows can be useful include helping your Customer Service team provided consistent service across your organization by standardizing questions and scripts, or Flows can be used internally to help maintain data integrity. When you embed your Flow into your user’s UI, you will usually… Read More

Customizing a Flow User Interface

Focus on People: Nancy Johnson

Nancy Johnson is a high level Project Manager hailing from Sydney, Australia. Having focused on being a Project Manager and Business Analyst during her career, getting Salesforce certified was her next logical step. Nancy shares her Salesforce journey with us below. How and Why did you get started with Salesforce? I am a very experienced Project Manager and a friend asked me to be a Project Manager at her organization… Read More

Focus on People: Nancy Johnson

Top 5 Salesforce Business Analyst Skills

Salesforce Administrators are in the unique position of wearing numerous hats to maintain their instance. Depending on the number of licenses and integrations, people can find themselves in more of a business analyst or project manager position instead of working in just Salesforce. Quite a few admins are the sole system experts for their company so this can be very complicated when enhancements pop-up. The main problem is that just… Read More

Top 5 Salesforce Business Analyst Skills

Focus on People: Neal Lightfeldt

Neal Lightfeldt works as a Sales Excellence manager and in this post, shares his Salesforce journey with us. How and Why did you get started with Salesforce? Our parent company began a new initiative several years ago focused on applying OpEx principles to the front end of the business. Part of that initiative included implementing Salesforce. I already had experience implementing and administering our previous CRM system (SalesLogix), so I… Read More

Focus on People: Neal Lightfeldt

Salesforce Naming Conventions are not just for Devs

by Amanda Bailey In a previous post, we did a deep-dive into cleaning up Salesforce fields using a free resource called Field Trip. We discussed the importance of removing fields that are not used or updating them to be required. In today’s post, we will be taking cleanup a step further by discussing the importance of naming conventions. It isn’t the most glamorous of topics but an administrator who can… Read More

Salesforce Naming Conventions are not just for Devs

Focus on People: Jasmin Ashley

I have been a Salesforce.com consultant for two years in Wellington, New Zealand. A career in IT was not a focus while I was studying both a Marketing and Psychology degree. I even completed a couple of IT papers while at university, wondering why they were compulsory. Having worked a series of different jobs in different industries in the build up to this role, I observed different businesses and processes… Read More

Focus on People: Jasmin Ashley

Clean up your Salesforce fields with this free tool

by Amanda Bailey At some point in a Salesforce Administrators career, they will inherit an existing and already customized Salesforce instance. It can be very exciting to start digging around in the custom objects and code, putting on a thinking cap and working through how the customizations all work together and how the business uses the tool. The downside to this excitement is that often times they will also uncover… Read More

Clean up your Salesforce fields with this free tool

Clone of Opportunities and Line Items using Visual Workflow

In this post, we are going to explore how Visual Workflow can be used to clone opportunities and line items. Yes, Salesforce does has a ‘Clone with Products’ and ‘Clone without Products’ option on a single record, but what if your requirements are more complex than that? For example, you may want to control which fields are cloned, or use a naming convention on the cloned opportunity, or set particular… Read More

Clone of Opportunities and Line Items using Visual Workflow

5 Free Salesforce Career Game Changers

By Amanda Bailey Every day people read job descriptions for their dream positions and feel frustrated when they get to the “required skills” section because it has 20 skills that they don’t have. In most industries, it would require extensive classes, possibly college courses and more to be able to gain those skills. Fortunately, with Salesforce they want you to grow and the community of people who work with the… Read More

5 Free Salesforce Career Game Changers