Category: Salesforce

How To Reduce Visualforce’s View State

How to Reduce Visualforce View State

View State is a technique Salesforce uses to maintain the current state of the page and the data that was queried between the user requesting things from the server and sending any changes back to the server.If the view state gets too large problems can begin to occur.

Continue reading

Salesforce Developers Need to Learn JavaScript Today

Salesforce Developers Need to Learn JavaScript Today

Over the years, it’s been possible for Salesforcce Developers to avoid needing to learn JavaScript because things could usually be done through a combination of apex and visualforce. If you don’t know JavaScript and you are a Salesforce Developer, you need to really consider finishing this post and learning JavaScript. Continue reading

How to Automatically Convert Leads in Apex

How To Automatically Convert Leads In Apex

In the blog post “What’s the difference between a Lead and Opportunity?” I spent a bit of time covering how leads are converted into accounts, contacts, and opportunities.

Basically, a lead is converted when the sales person thinks that there’s actually an opportunity to sell them something. In a lot of cases, it might make sense of for leads to be automatically converted after a field is set to a certain value. For example, a lot of companies might want to automatically convert a Lead as soon as the Sales Rep has completed ANUM or BANT.

To convert a Lead into an Account, Contact and an Opportuninity we need to make use of the Database.convertLead method.

What’s the difference between a Lead and Opportunity?

Difference Between a Lead and Opportunity in Salesforce

A lead is basically a person that has contact information that you should be able to sell to, they have been created because they have contacted you in someway with some sort of need whether this be that they filled out a contact form on your website or they exchanged business cards with somebody at a tradeshow. Continue reading

Best Practices for Salesforce Lead Sources

Best Practices for Salesforce Lead Sources

“Lead Source” is a standard picklist field on the Lead which is used to determine where that particular Lead came from.

This field is highly valuable to a skilled marketing team because it will allow them to evaluate how the leads they are acquiring are doing for the sales team.

Continue reading

Salesforce: Enforcing Security in Apex

Salesforce allows data access to be specified at the object level, field level, and finally record level. To get listed on the AppExchange an application has to go through a really extensive security review process. As part of the review process Salesforce confirms that security permissions are being adhered to. Unfortunately, apex doesn’t automatically follow the rules.

Visualforce does if the fields are being referenced in the page.

Object Level Security

Object Level security, also known as CRUD, is used to limit access to an object. It’s pretty granular as you can control whether an object can be created, edited (updated), read, or deleted.

An example of how to enforce READ in apex on the opportunity can be seen below:

private List<Opportunity> getOpportunities() { 
    List<Opportunity> retVal = null; 
    if (Schema.sObjectType.Opportunity.isAccessible() ) {
        retVal = [select Id
                        ,Amount from Opportunity Where IsClosed = false Limit 100];
    return retVal;

Naturally, it’s rare that we would also just want to get a list and only ever do a READ only operation on them. Instead we would probably need to do something more like this:

private List<Opportunity> getOpportunities() {
    List<Opportunity> retVal = null;
    if (Schema.sObjectType.Opportunity.isAccessible()
        && Schema.sObjectType.Opportunity.isDeletable()
        && Schema.sObjectType.Opportunity.isUpdateable() ) {
        retVal = [select Id ,Name ,Amount from Opportunity Where IsClosed = false Limit 100];
    return retVal;

Please note that you should probably throw some sort of exception and display an error to the user. Also note that this solution is in no way complete, because it completely ignores Field Level Security.

Schema.sObjectType is used frequently in dynamic apex and has lots of other very useful methods.

Field Level Security

Field Level Security, also known as FLS, is a technique for limiting a user’s access to certain data. For example, it may not make sense for a Support Rep to see how much of a commission a Sales rep was paid or what the expected revenue is.

Field level security is usually set by the administrator and can be set in a couple of different ways: on the profile or through permission sets.

I previously wrote a blog post about Basic Security in Salesforce and how the permissions are set and where they are set.

Here’s a full example of how to get a list of opportunities that could be READ, Updated (Edited) or Deleted entirely.

If later in the code, your user will need to update or delete values you need to make sure you call isDeletable() and isUpdateable() for each value. I always check if isAccessible() when querying.

private boolean isObjectVisibleToUser() {
return Schema.sObjectType.Opportunity.isAccessible()
    && Schema.sObjectType.Opportunity.isDeletable()
    && Schema.sObjectType.Opportunity.isUpdateable(
private boolean isFieldsVisibleToUser() {
    return Schema.sObjectType.Opportunity.Fields.Id.isAccessible()
        && Schema.sObjectType.Opportunit.Fields.Id.isDeletable()
        && Schema.sObjectType.Opportunity.Fields.Id.isUpdateable()
        && Schema.sObjectType.Opportunity.Fields.Name.isAccessible()
        && Schema.sObjectType.Opportunit.Fields.Name.isDeletable()
        && Schema.sObjectType.Opportunity.Fields.Name.isUpdateable()
        && Schema.sObjectType.Opportunity.Fields.Amount.isAccessible()
        && Schema.sObjectType.Opportunit.Fields.Amount.isDeletable()
        && Schema.sObjectType.Opportunity.Fields.Amount.isUpdateable()
private List<;Opportunity>; getOpportunities() {
    List<;Opportunity>; retVal = null;
    if (isObjectVisibleToUser() && isFieldsVisibleToUser() ) {
        retVal = [select Id
            ,Amount from Opportunity Where IsClosed = false Limit 100];
    return retVal;

As you can see this is really quite messy and results in a lot of duplicate code. Every time we need to add or remove a field we would need to change multiple places. We could make this a lot more generic and make it reusable in other situations.

Asynchronous Programming in Salesforce

What is Asynchronous Programming?

In synchronous programming, each step is performed one after the previous one is finished executing. This means that each step blocks the next step.

In a lot of cases, we probably don’t necessarily need to do everything in order. In asynchronous programming, steps can all execute in parallel and/or in really any order. Continue reading

How to Use Salesforce Custom Settings

How to Use Salesforce Custom Settings

When developing on Salesforce’s platform it’s essential to avoid hardcoding application settings because a deployment will be required every time something needs to be changed. Application Settings could be anything really, but often it’s things like an email address, ip address, or even a passkey. For those not already aware, a Salesforce deployment can take hours to finally happen which may cause significant downtime in some businesses. Continue reading

Apex: Converting Lists to Sets, and Sets To Lists

As many Salesforce Apex programmers know, in Salesforce it’s pretty much an unchallenged best practice that all code should be able to handle bulk inserts, updates, etc. When adhering to this best practice, it’s really common to use sets, or lists as parameters in functions. Sometimes, there’s a need to convert between lists, or sets, or maybe even maps. Continue reading

Everything you need to know about creating Tasks in Salesforce Apex

Everything You need to Know About Creating Tasks in Salesforce Apex

Salesforce task records are basically action items or items on a to-do list. Tasks are commonly logged against relevant records like a Lead, Opportunity, Contact, or Account. Sometimes, depending, on your organization’s data model it may also make sense to log tasks against custom objects.

In this post, we cover how to automatically create a task in Apex.

Continue reading

5 Incredible Resources For Learning to Program Salesforce

5 Incredible Resources for Learning to Program Salesforce

Salesforce is an incredibly large and customizable platform with hundreds of different features. Learning to use Salesforce can be difficult, but learning to program and manage Salesforce is even more daunting. Below I’ve provided you with the best resources I could find on programming Salesforce.  Before jumping into learning to program apex, I recommend you register for a demo org and watch a few of the different product demos.

When I started learning how to use Salesforce Apex in 2013, I was amazed at the hundreds of different online resources available. Literally, there are thousands of different sites. Over the last two years, I’m sure there’s been even more resources. Salesforce has developed its own “learning portal”, and there’s been a few new books that have come out.


One site, in particular really stood out to me which was SALESFORCE CODING LESSONS FOR THE 99%. David has written very good and detailed tutorials that anyone should be able to follow and implement. I understand that David lead some great sessions at Dreamforce 2014; I wasn’t able to attend any of them though.


At Dreamforce 2014, Salesforce introduced Trailhead. A while after Dreamforce, I wrote a post about my thoughts on Trailhead. Trailhead is an incredible resource that I wish was around when I had first started using Salesforce. Over the last year, it’s really exploded and now offering a ton of different paths to learning about Salesforce.

Bob Buzzard Blog

No list would be complete without the Bob Buzzard blog. Keir has written dozens of blog articles that I have bookmarked about different Salesforce topics. His posts are primarily about visualforce and apex, although he has started writing quite a bit about Salesforce Lightning and Trailhead. He’s also written a book about visualforce and led some great sessions at Dreamforce. I believe right now Keir has almost all of the Salesforce Certifications.


Harshit Pandey previously worked at Salesforce as a Technical Architect. He has some incredibly basic blog articles, and some incredibly technical articles as well. One of my favourite series so far is his section on Salesforce Technical Architect Best Practices.

Jeff Douglas Blog

Prior to becoming a Salesforce Employee Jeff wrote some incredible posts about apex and visualforce. One of my favourite posts is his post about having “Fun With Salesforce Collections“.

Additional Salesforce Training Resources

  • Force Platform Fundamentals is Salesforce’s introduction to the Platform. There’s quite a bit of discussion about building metadata (creating objects, fields, custom workflows, approval process), security, and reporting. Overall, it’s pretty good but also very daunting. The pdf has about 400 pages.
  • Visualforce Developers Guide is pretty much a must for any developer that will develop pages in Salesforce.
  • Apex Workbook covers a lot about the syntax, fundamentals, and limits the Salesforce have placed in the language and on the platform.
  • Salesforce University provides some in person and online training. I haven’t taken any of the training, but it seems like it could be pretty good based on some of the Hands on Training I’ve done at Dreamforce. Prices also seem to be pretty high, and in US Dollars
  • On Youtube, there’s a lot of different Dreamforce Presentations many of which are very good and still pretty relevant.
  • Also review my blog article about about Using jQuery with Visualforce.