Salesforce: Yet Another JSON to Apex Converter

A screenshot of the JSON to Apex Converter tool

I’ve been doing a lot of Salesforce integration as part of my most recent role. And where there’s integration, there’s JSON. The vast bulk of this work is integration using Apex, and so I began brushing up on what’s changed since I last got in the weeds many years ago. How much has changed? Surprisingly, not much! Depending on your situation, there are broadly 3 options when it comes to handling JSON in Apex: The options are in order of difficulty. Additionally, options 1 and 3 make for code that is much easier to work with because the Apex Types you define are strongly typed. But option 1 isn’t always possible e.g. if the JSON you’re working with uses reserved keywords, or if you want to process part of the JSON before sticking it in an Apex structure. However, this blog post is not about the various approaches and their …

Read more

Salesforce: Sending emails from communities doesn’t work

I’ve been doing a lot of work with Communities AKA Experience Sites recently and encountered an issue where emails sent using the Apex method Messaging.sendEmail() were not be received by the recipient. The strange thing was that the debug logs showed that the email had been successfully sent, and the associated Activity record had been created. And thus began many hours of debugging to understand the root cause and its fix. I tried the usual suspects: None of the above worked. When I checked the Email Logs there was still no entry for the email despite the Apex code saying it had successfully gone out. Then I came across an article in the Winter ’24 release notes saying the email addresses must be verified to send emails through Salesforce. Aha! Or so I thought. For standard Salesforce users, you’ll see “[Verified]” next to their email address. Or, if they’re not …

Read more

A Beginner’s Guide to Object-Oriented Programming with Apex: 1. Encapsulation

This is part 2 in the series “A Beginner’s Guide to Object-Oriented Programming with Apex” and will cover the aspect of Encapsulation.

A Beginner’s Guide to Object-Oriented Programming with Apex

  1. Introduction
  2. Encapsulation (this post)
  3. Abstraction
  4. Polymorphism

What the hell is Encapsulation?

Encapsulation isn’t a tricky concept in itself but some confusion does arise from it’s close relationship with Abstraction (to be covered in my next post). Broadly it is defined as one of, or both of the following:

  1. An information hiding mechanism.
  2. A bundling of data and methods that operate on that data.

These are pretty abstract statements but in reality are very simple. I will demystify them with examples below but for now let’s use an analogy:

Imagine you have a watch (or if you have one just consider it, don’t worry about the imagining part) that tells you the date and time. You can adjust the date and time using buttons/knobs/switches but you have no idea what happens inside the watch, just that it results in the date and time changing. You have some data (the date and time), you have some methods to operate on that data (buttons/knobs/switches) and there is a whole lot of stuff going on in the background that you don’t know about (and it’s better that way, imagine you had to adjust all those bits and pieces yourself).

That’s encapsulation baby!

Read more

A Beginner’s Guide to Object-Oriented Programming with Apex: 0. An Introduction

Apex has come a long way in the past few years, and as Salesforce has grown so has the number of super smart people working on the platform. There are lots of guides on how to do fancy things using JavaScript, Apex and Visualforce as well as many more whitepapers on the topics of governances and standards. However I think there is a gap, and I’d like to plug it. Over the next few weeks and months I will be releasing articles that describe and show the basics of object-oriented programming (OOP) with Apex. My approach will be to make these articles as modular and simple as possible. The target audience? Developers with a-teeny-bit-of-experience through to those who have tons of experience but never had any formal training in OOP. A Beginner’s Guide to Object-Oriented Programming with Apex What the hell is OOP? Good question, and although many agree on …

Read more

Salesforce: Universal Batch Scheduling Class

I’d like to propose a new way of working with scheduled batch classes. I’ve worked on several hundred Salesforce projects in the past few years and often see batch scheduling classes being created per scheduling requirement and it grinds my OCD – not in a good way. In most cases you should only need one “Batch Scheduler” per Org, let me demonstrate how and why. The Universal Batch Scheduler™ Requirements Let’s assuming you have a batch class that you need to run on a repeated schedule*, such a class signature is given below. That class will have to obey some conventions such as implementing the Batchable interface as shown in the standard documentation. [code language=”Java”] global class MyBatch implements Database.Batchable<SObject> { // … } [/code] * For one off, schedule execution of batch classes you can use the System.ScheduleBatch() method. The Scheduler Now you might be tempted to created a scheduled …

Read more

Salesforce JavaScript Remoting: Using Apex and JavaScript objects to pass data from client- to server-side and vice versa

I’ve spoken about how to do this at a high level during Cloudstock London and there are hints at how it can be done but no formal documentation that I’ve found, so here we are 🙂 Quite simply JavaScript Remoting will transform Apex objects and classes (or collections of these types) into JavaScript objects for you. The opposite is true too but there are some rules you need to observe. Apex Types to JavaScript Equivalents This is the easier of type conversions in that you don’t have to really do anything to make it happen. The code below uses a custom class that I’ve defined but you can do the same with any sObject too. Let’s have a look at the code. The Controller The Visualforce The Output JavaScript Types to Apex Equivalents This is a little tricker, especially when it comes to sObjects. Note that the approach below works for …

Read more

Salesforce: JavaScript Remoting – a different way of thinking

  JavaScript Remoting for Apex operates in a very different paradigm from what you might be used to i.e. Visualforce pages have controllers and the two interact through action methods – where this might be a full form submission or some neat AJAX functionality. Remoting also calls controller methods but there is a gaping maw in terms of how the two work under the hood. I’ve seen a few great articles on the syntax and example usage of JavaScript Remoting for Apex but when I started using it I came across a number domain differences that weren’t documented anywhere. Hopefully my list here will help you in the learning process. The best way to describe the new way of thinking is to examine the features set in contrast to “normal” Apex and Visualforce. How JavaScript Remoting Differs Pass parameters naturally i.e. the call matches the method signature syntactically instead of …

Read more

Salesforce: Dynamically determining the field type of a dynamically determined sObject

Call me crazy but I need to do this from time to time, and every time I do I can’t remember how I did it before! So I then trudge through the API and the Apex docs until I find the answer and that’s no mean feat in this specific case. Well, no more my friends because I’m putting it right here on this very blog! In short the code below will return (as a String) the type of field that we’re working with. Neither the name of the object or the name of the field need to be known in advance.

Salesforce: Different percentage unit test code coverage in different environments

Many people are finding that their tests are reporting different degrees of test-coverage in different environments. There are a few things to check if you are getting inconsistent results but there’s a new bug in the wild. Before you assume you have the bug make sure that you’ve: ‘Run All Tests’ in each environment. This will tell you a few things viz. Perhaps there are tests failing that are bringing coverage in that environment down. There are some tests that only fail when run in the browser e.g. MIXED_DML_EXCEPTION will not rear it’s head through the IDE. Click the ‘Compile all classes’ link above the Setup > Develop > Apex Classes view. I’m not sure when this lil’ bugger first appeared but it’s darn useful. Essentially it makes sure that all the dependencies in your code are fulfilled e.g. if page A uses controller B that in turn refers to utility class …

Read more

Salesforce: Stop email being sent on user creation or password reset or …

I’ve had to do this a few times but infrequently enough for me to forget how to do it each time. Forgetting things isn’t usually an issue because of our Google Overlords and their mighty The Google but it’s quite a journey down the rabbit hole to find this specific information. The reasons it’s tricky to find is because the setting that controls whether an email is sent to the user on creation is not directly associated with users but with DML. Long story short you need to set a particular Database.DMLOption e.g. [code language=”java”] User u = new User(); // Add some details here … // Set the DML options Database.DMLOptions dlo = new Database.DMLOptions(); dlo.EmailHeader.triggerUserEmail = false; Database.insert(u,dlo); [/code] Hopefully this information will now be easier to find next time I forget 🙂