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

Salesforce LWC: “Error: Failed to execute ‘removeChild’ on ‘Node’: The node to be removed is not a child of this node. at HTMLDivElement.removeChild”

I recently came across this error when implementing the 3rd party image carousel library called Glider into a Salesforce lightning web component. The message doesn’t give much away, does it? Given the hours I lost investigating the issue I thought I’d share the solution. Salesforce does not like 3rd party libraries manipulating the DOM. They do make it possible though e.g. if you know that the library is going to inject elements into a container element you can flag that container element like so: However, some libraries require that you partially set up an expected DOM structure and then the library’s initialisation sprinkles whatever else it needs within that structure. In my case, Glider injects a “track” (wrapper) <div> around the elements that you can flip through. Interestingly, SF LWC is fine with this unless you are conditionally re-rendering the part of the page that contains Glider. In that case, …

Read more

Using Native Mobile Device Features from Visualforce with Phonegap

The prospect of learning hybrid mobile development is daunting for most Salesforce developers. There are just so many new things to learn all at once. And so, whilst recently learning Phonegap, Ionic, Angular and responsive-design for a side project I realised that there is a very simple bridging approach that can teach you some of the basics and that might even result in some cool apps. This bridging is achieved by making the native features of a mobile device (GPS, local storage, camera etc.) directly accessible from Visualforce. It is dead easy. I swear. And will demystify the hybrid development paradigm for you. Set up Phonegap Install Phonegap and Set up an empty project For this post I’m assuming you know that Phonegap is a framework that allows you to build web applications that are then bundled into native containers (and iOS container for Apple devices, and Android Container for …

Read more

A Beginner’s Guide to Object-Oriented Programming with Apex: 3. Polymorphism

This is part 3 in the series “A Beginner’s Guide to Object-Oriented Programming with Apex” and will cover the aspect of Polymorphism. For other posts in this series see the links below. A Beginner’s Guide to Object-Oriented Programming with Apex What the hell is Polymorphism? Polymorphism has the funkiest name and is my favourite aspect of OOP. If done correctly you will feel like a proper genius. Originating from Greek words, polymorphism more or less means “many forms”. This gives away very little unless you know polymorphism quite well but essentially polymorphism describes the ability of a type e.g. a set of differing Apex classes, to be used in similar ways without knowing much about those classes. An immediate example on the platform is the Type.format() static method available on Integers, DateTime, etc**. Although each of the Types represents something different this method does a similar thing for each i.e. turns …

Read more

A Beginner’s Guide to Object-Oriented Programming with Apex: 2. Abstraction

This is part 3 in the series “A Beginner’s Guide to Object-Oriented Programming with Apex” and will cover the aspect of Abstraction. For other posts in this series see the links below. A Beginner’s Guide to Object-Oriented Programming with Apex What the hell is Abstraction? Abstraction is a very interesting and surprisingly simple concept. At its core it’s about context and if Abstraction were a company it’s motto would be “Provide the right tools and data at the right time.”. There are many analogies in the “real world” e.g. Where is this applicable to developers on the platform? Ever created a Visualforce component or Apex utility class? Well you my friend you are already using abstraction! This sounds a lot like Encapsulation! Abstraction and Encapsulation go hand in hand but they are different. Encapsulation is one of the ways you can achieve abstraction but does not encompass the entire concept. …

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

If This Then Salesforce

I’ve been enjoying IFTTT for a while now and if you haven’t experimented with it yet then I’m not sure we’ll ever be friends. Essentially it’s a very easy tool that lets you set triggers on a source API e.g. Foursquare and have some information from that API be posted to a target API e.g. Jawbone Up. IFTTT calls these recipes and I’d like to demonstrate some particularly delicious combinations that can be used with Chatter. Salesforce Org Alerts and Known Issue posted to Chatter Salesforce makes Instance Alerts e.g. “Perfomance degradation on EU0.” available through an RSS feed so all you need to do is create a recipe (or copy mine) that monitors the appropriate RSS url for changes and posts to a particular Chatter group. You can do a similar thing with Salesforce Known Issues. Tweets posted to Chatter Quite often there are interesting tweets that I want to …

Read more