Sunday, 9 January 2022

ApexTypeImplementor in Spring 22



Note: This feature may be in Beta come Spring 22 - the release notes in Salesforce Help say that it is in beta, the PDF version doesn't mention it. If I get confirmation either way I'll update this post! Remember that Spring 22 is still in preview, so this feature might never see the light of day anyway!

Introduction

The Spring 22 release of Salesforce introduces the ApexTypeImplementor object, which contains information about the classes that implement an interface, either directly or indirectly. You've been able to access Apex class bodies for years, so picking up the direct implementation of an interface has been possible for a while. Figuring out that that the interface is implemented in the inheritance hierarchy gets a bit more tricky. In either case, having the platform take care of it for you takes away a chunk of code, which is always a good thing.

Use Cases

When I first came across this new feature it had a touch of the "solution looking for a problem" about it, but once I had a chance to dig into it in a little more detail, I have a number of scenarios where I'll be able to apply it as soon as it is available/maybe out of beta.

Rule Engine

The most common use case from my perspective is the configurable plug and play engine. I have several rules engines where a class is considered a rule because it implements a specific interface. I'll have a number of classes that implement rule variants, and these rules are enabled and applied in a specific order based on configuration. 

The ApexTypeImplementor doesn't help in terms of which rule classes should be applied, as I still want it to be driven through configuration rather than the code just applying everything it can find in whatever order they come back. Where it does help is to assist the admin who is configuring the rules. Rather than them having to remember the names of classes or look them up and copy/paste from other setup pages, I can create a custom configuration page that offers them only the classes that implement the interface, thus ensuring that the configured classes are available.

Setup Verification 

A less obvious use case is confirmation that a system is setup and configured correctly - i.e. that there is no missing or obviously broken configuration that will cause errors at runtime. Each feature that can be enabled has an associated class that checks the feature is configured correctly (or at least plausibly as it's quite hard to confirm correctness without running a bunch of tests). As long as each of these classes implements the same interface, I can execute code from an admin clicking a button or quick action that finds all of the classes that implement the verification interface. Each of these is instantiated and the method that checks the configuration is executed. This works really well if the additional features are delivered via org dependent or unlocked packages, as the code that implements the feature and the code that checks it are developed in tandem.

Training Confirmation

The third use case that I have is around training, and ensuring that the training has been retained. I have a collection of packages that I can drop into an org that ask the user to carry out some configuration or regular business work, and then check that they carried it out correctly. 

At the moment the packages create configuration entries when they are installed, and those configuration entries are used to generate cards on an application page so that users can access and check the challenges. Because I want all of the challenges that are installed to be available to the user, with the ApexTypeImplementor I can do away with the configuration entries and just show the users details of all the classes that implement the interface. 

Sample App

I've created a sample application in my Spring 22 repository to show ApexTypeImplementor used in anger. I have an interface that a class implements to describe itself to interested parties :

public interface ClassInfoIF 
{
    String getDescription();
    String getAuthor();
}

I'm using a Lightning Web Component to display the details of the classes, which extracts them from a dedicated controller (that also implements the interface!) : 

@AuraEnabled(cacheable=true)
public static List<ClassDetail> GetClassDetails()
{
    List<ClassDetail> classDetails=new List<ClassDetail>();
    List<ApexTypeImplementor> classInfoTypes = 
           [SELECT ApexClassId, ClassName, ClassNamespacePrefix
            FROM ApexTypeImplementor
            WHERE InterfaceName = 'ClassInfoIF' and IsConcrete=true];

    for (ApexTypeImplementor classInfoType : classInfoTypes)
    {
        ClassDetail classDetail=new ClassDetail();
        classDetail.classId=classInfoType.ApexClassId;
        classDetail.fullName='';
        if (null!=classInfoType.ClassNamespacePrefix)
        {
            classDetail.fullName=classInfoType.ClassNamespacePrefix + '.';
        }

        classDetail.fullName+=classInfoType.ClassName;
        ClassInfoIF classInfo = 
                    (ClassInfoIF) Type.forName(classInfoType.ClassNamespacePrefix,
                                            classInfoType.ClassName).newInstance();

        classDetail.description=classInfo.getDescription();
        classDetail.author=classInfo.getAuthor();
        classDetails.add(classDetail);
    }

    return classDetails;
}

The bolded sections above show the query that extracts the classes that implement the interface, and the code that constructs the class as an instance of ClassInfoIF and executes the methods that describe the class. If a new class is added to the org that implements this interface, it is automatically included the next time I access the page.

I've put in a few fake classes too, including one that implements the interface by overriding methods from its superclass to check the indirect side of things works too. Accessing the application page shows the information I'm interested in for each class:


Related

Tuesday, 4 January 2022

2021 Year in Review - Part 4


Testing. Testing. 1..2..3 (and more) Lateral Flows

The theme of the last few months of the year was testing. Not the fun kind involving mocks and setup/teardown, but the unpleasant kind that required shoving swabs up your nose. The picture everyone wanted to post on social media had changed from a selfie to a negative result!

It was all worth it though.

October

The London Salesforce Developers were back in person! This felt simultaneously wonderful and very weird - seeing three dimensional versions of faces I'd only seen on screens for 18 months took a little bit of getting used to. I also got the opportunity to meet in person a bunch of graduates that I'd been training up for the last few months!

The theme of the meetup was a quick run through some of the interesting features from the Winter 22 release that had gone live earlier in the month. We kept it pretty light though, as most people just wanted to network. 

Bret Taylor was apparently telling everyone he was going to be promoted to Salesforce CEO soon. He wasn't wrong, but as it turned out wasn't right either.

November

Back to back in-person events for the London Salesforce Developers, as Rob Cowell presented a cracker of a session on Integrating AWS with Salesforce. There was also a dog, which made it pretty much perfect for me.


And the biggest news of the month, for me at least, BrightGen was majority acquired by Credera.

December

Just ahead of the Omicron variant, the Xmas Megameet of the London Salesforce Developers, Admins and Women in Tech took place on 6th December.  

After months of rumours it finally happened, although not exactly as some were expecting, Bret Taylor was promoted to co-CEO of Salesforce. He was also took over as Chairman of the Board of Twitter. Not a bad week's work by any measure.

Dreamforce made it outside of San Francisco, although sadly for those of us in Europe it was only to the other side of the US in New York. The safety aspects of this were taken extremely seriously, and held up as an example for everyone else. And launched as a product - Dreampass. What with the reimagined work.com in 2020, the acquisition of Slack and now Dreampass, Salesforce have certainly maximised opportunities during these pandemic times.

What Does 2022 Hold?

More Covid cases for sure - the numbers continue to rise rapidly in a number of locations including the UK. Work from home is again the advice, so I'd expect the first couple of London Salesforce Developer meetings to be virtual again. Hopefully three months of in-person softens that particular blow!

I can't see Salesforce resting on their laurels with regard to remote work, so I'd expect more in the way of acquisitions to establish themselves as a player in this space. Videoconferencing still seems like an obvious addition to the product set, and it will be interesting to see if (and how) Slack is further integrated  into the Salesforce UI, given that it will supposedly replace Chatter.  Half of employees say they may quit if remote work goes away, so there's plenty more opportunity in this space I feel.

Two things are for sure, there will be three further releases of Salesforce in 2022, and if there is a major outage it will be in May!

Whatever happens, I'm sure I'll be banging on about it on here or Substack. Thanks for reading!

Related Posts


Monday, 3 January 2022

2021 Year in Review - Part 3

July

Another month, another new (and virtual) event. The first Consultancy Dreamin' took place on the Hopin platform. In a change of pace for me, I was looking after a cohort of speakers rather than running any sessions myself. I was also (and somewhat unexpectedly, as I'd missed the training session) facilitating a couple of panel sessions, which was great fun. Usually at these events I just have to worry about getting myself somewhere at the right time and not messing up my demo - it's a very different experience organising others, especially once the EMEA and US aspects of the event started to overlap!

July also saw my personal favourite event for the London Salesforce Developers - Discover a lightning fast way to debug in Salesforce with RFLIB. I've long been convinced that platform events are a great way to decouple automation from database transactions, and this is an excellent use of them.

The Salesforce acquisition of Slack finally went through, although it felt like it had already happened some time before. Almost simultaneously, a bunch of Trailhead badges for Slack went live and the hunt to get them all started up again. It wasn't all unicorns and rainbows though, as the Times New Express reminded us of the Salesforce execs that had departed to this point in 2021.

August

Salesforce launched Salesforce+ - the first (and only?) enterprise software company streaming service. While I've done some gentle mocking of this (here and here), I do think it's a great idea. It was also good to see Salesforce trying something different on the event front, albeit still trapped in front of a screen!

I reviewed Ahsan Zafar's book on Salesforce Data Architecture and Management - a good book tackling a tricky subject. Remote working was also something I was spending a lot of time thinking about, with particular reference to returning to something like normality.

An old friend, and former leader, of the London Salesforce Developers - Anup Jadhav - returned to tell us all about OmniStudio. This was an extremely well attended session, second only to Erika McEvilly's trigger session that kicked the year off. 

September

Dreamforce was back. Sort of. The events around the world had disappeared and San Francisco was now 1,000 attendees over 2 days, but it did happen and people were there. It was streamed on Salesforce+ rather than trying to recreate the physical event on a virtual platform, which made a bit of a change, but still kept us in front of our screens. The London Salesforce Developers had another virtual virtual watch party, but we were seeing the numbers drop as people were becoming zoomed out.  This chart of our attendance figures shows the decline - it was slow, but definitely heading down aside from the occasional event.

for this reason we'd decided to go back in-person in October. Exciting times!

Related Posts 

Sunday, 2 January 2022

2021 Year in Review - Part 2


The lockdown locks before and after!

April

Lockdown finally started to ease in the UK, with non-essential retail starting to open up and overnight stays away from home allowed again. Outdoor mixing was allowed, but with a maximum of six people from two households, so we didn't feel it made a lot of sense to move the London Salesforce Developers back in person just yet.  My fellow co-organiser, Amnon Kruvi, gave a well attended virtual talk on the Salesforce Security Review. Judging by the questions, there are clearly a lot of future ISVs among our members.

The rumour mill suggested that Salesforce were committed to an in person Dreamforce in 2021, which turned out to be accurate, although calling what happened Dreamforce seems a bit of a stretch when only a small number of people from the US were allowed to attend. I guess it was important to be seen to be holding it, but it's hard to agree with "Dreamforce is an annual event that brings together the global Salesforce community" when the vast majority of said community have to watch on a streaming platform.

Summer 21 Pre-release signups opened up, reminding us that we are never more than a few months away from a Salesforce release.

I also reviewed Tameem Bahri's book: Becoming a Salesforce Architect - I liked it then and I still like it now!

May

While you might never be more than a few months away from a Salesforce release, when it's May you also appear to be at the most risk of an outage, and 2021 continued the trend with a DNS issue. While the issue took the trust site down, it was good to see Salesforce continue to send people there to maximise their frustration. Why limit yourself to unhappy customers when you can turn them incandescent with rage?

Paul Battisson joined the London Salesforce Developers to tell us how to improve the performance of our Apex code by turning it up to 11.

Continuing with the performance theme, this month also saw the publication of one of my most popular blogs of the year - The Impact of System.Debug, which garnered close to 3,500 views, several comments and a new Apex PMD rule!

June

After 15 months without letting any scissors near me, the lockdown locks finally went, freeing me up from several minutes of grooming every week and raising over £500 for charity. They'd provided a lot of pleasure for a lot of people, mostly in the form of pointing and laughing, but their day was done.

The Summer 21 release hit production, and Salesforce published the root cause analysis of this year's instalment of the May outage. There was a new Event Bus on the way, with talk of a new pub/sub API. Six months later there's a few references to a pilot and a guide, but the marketing seems to have been dialled right down. Shades of Evergreen/Functions maybe?

Although countries were starting to open up, the virtual events kept coming, with the trailheadx developer conference. Scheduled with an eye on San Francisco local time, the near to 5pm start wasn't ideal for those of us in the UK, so I mostly caught up on the content over the next couple of months. Mostly, because the London Salesforce Developers ran a viewing party for the keynote, where we all joined a video call to watch a keynote on video. Very meta. To ensure everyone paid attention we had a word hunt, where spotting common terms from Salesforce keynotes (awesome, 1-1-1, you know the kind of thing) earned swag. An unexpected side effect of this was the entertainment of people joining an hour into the session and hoping they were the first to hear 'awesome'. Entertaining for me, as I wasn't running the competition!

Dreamforce was announced as a series of in-person events around the world, although everything outside of San Francisco quietly disappeared over the next few months. 

Related Posts

2021 Year in Review - Part 1

Wednesday, 29 December 2021

2021 Year in Review - Part 1

London's Calling Caricatures - 1 year and zero haircuts apart

London's Calling Caricatures - 1 year and zero haircuts apart

2021 began in the UK as we spent quite a bit of 2020, in lockdown (Lockdown 3, just when you thought it was safe to go inside someone else's house again) and doing everything over videoconference, which was starting to get a bit samey. 

January

Those of us that had submitted London's Calling presentations heard back, and I was relieved to see that I'd made the cut. 

The London Salesforce Developers held two events in January - the first for the graduation of another round of Speaker Academy candidates, and the second for Apex Trigger Essentials courtesy of my fellow co-organiser Erika McEvilly. The combination of Erika and starting from the ground up with triggers clearly resonated, as we had a record 163 signups for this event. 

I made a bold, and incorrect, prediction that there wouldn't be an in person Dreamforce in 2021. I got it right that Salesforce wouldn't bring 100k+ people to San Francisco from all over the world, but got it wrong that they'd be happy with a 5k event.

February

The Spring 21 release of Salesforce went live, including an update that corrected the CPU tracking for flows - this is enforced in the Summer 22 release, so if you haven't tested with the new behaviour the clock is ticking. As as the tradition, here at BrightGen we ran our release webinar and gave everyone a chance to spend more time in front of a screen, but at least not on camera. Yay!

London's Calling ticked ever closer, and session recording got underway. In keeping with the scale of the thing I was accompanied by a member of the content team and an AV specialist to keep things on track. I have to say I still prefer the adrenaline rush of doing everything live - there's nothing like wondering if the wifi will hold up to get the heart racing. That said, from the point of view of the organisers I can see that trying to co-ordinate a ton of speakers to present live from around the would be a total nightmare.

The London Salesforce Developers were treated to a session on respecting data protection laws in Salesforce - a dry topic, but also an important one that isn't going away.

I also launched a Substack in February, which is proving invaluable for reminding me what was going on back then!

March

The wait was finally over and London's Calling was here - another chance to spend a whole day in front of a screen! As always it felt like the hardest thing to recreate online was the expo, but at least we at BrightGen had the caricatures which do translate pretty well, as you can see at the start of this post. My session was on Org Dependent Packages, which I still think are pretty awesome, especially for large enterprises with mature orgs. You can find recordings of all of the sessions on the youtube channel, and there is some great stuff there, so it's well worth a few hours digging around. 

When it's London's Calling month, we at the London Salesforce Developers try to keep our event lightweight as we feel like there's plenty of learning around already. To this end we decided to crowd-source our member's favourite Spring 21 features, which didn't get a huge take-up. To punish them, I did most of the presenting on my favourite features, which should motivate people to either get more involved or not show up in the future!

In an unexpected turn of events, the data recovery service came back from the dead, having been retired in July 2020. A fine example of listening to your customers. There were also rumours that Marc Benioff was considering stepping down and handing over to Bret Taylor. Not entirely wrong as it turned out, but not exactly correct either.

March also marked a whole year fully remote - little did I realise that there was plenty more of this to come.


Sunday, 5 December 2021

JavaScript for Apex Programmers Part 2 - Methods and Functions


(for the full Gertrude Stein quote on art having no function and not being necessary, see : https://www.scottedelman.com/2012/04/26/art-has-no-function-it-is-not-necessary/)

Introduction

In the first part of this occasional series on JavaScript for Apex Programmers, we looked at my chequered history with JavaScript, then the difference between Apex and JavaScript types. In this instalment we'll look at methods and functions.

Definitions

A method is associated with an object. It's either part of an object instance (non-static) or part of the class that objects are created from (static method). A function is an independent collection of code that can be called by name from anywhere. These are often used interchangeably (by me especially!) but the difference is important when considering JavaScript and Apex.

Apex

Apex only has methods. All Apex code called by its name is either a method being invoked on an object instance (that you or the platform created) or a static method in a class.

...

Almost all Apex code.

...

Apart from triggers. 

...

Here's an account trigger with a named collection of code that I can call later in the trigger:

trigger AccountMethod on Account (before insert) 
{
    void logMessage(String message) 
    {
        System.debug('Message : ' + message);
    }
    
    logMessage('In account trigger');
}

And if I execute anonymous and insert a trigger, I see the message appear as expected:



While this might look like a function, it's actually a static method on the trigger, as I found out by changing the use to this.logMessage('In account trigger');



It's not a useful static method though, as it can't be called from outside of this trigger. I suppose it could be used to organise code in the trigger to be more readable, but code shouldn't live in triggers so you'd do far better to put it in a utility or service class.

That interesting digression over with, as far as we are concerned Apex has methods in objects or classes, and I'll proceed on that basis.

If you want to pass some Apex code to be executed by another method, you have to pass the object to the method. An example of this that we come across pretty often is scheduled Apex:

MySchedulable mySch = new MySchedulable();
String cronStr = '21 00 9 9 1 ?';
String jobID = System.schedule('My Job', cronStr, mySch);
The platform will call the execute() method on the mySch instance of the MySchedulable class that I pass to it.

 

JavaScript

JavaScript, as I find so often the case, is a lot more powerful and a lot more confusing. JavaScript has both methods and functions, but under the hood methods are functions stored as a property of an object. 

Functions are also actually object instances - every function you create is actually an instance of the Function object.  Which means they can have properties and methods like other objects. And those methods are actually functions stored as properties. And so on.

The good news is that you don't need to care about most of this when using JavaScript in Salesforce. In my experience, what you really need know is:

Functions are First Class Citizens

In JavaScript, functions can be assigned to variables:

let log=function(message) { 
                console.log(message);
        }
   
log('Hello');

Hello

passed as parameters to other functions:

let consoleLogger=function(message) {
               console.log(message);
               }
               
let log=function(logger, message) {
             logger(message);
}

log(consoleLogger, 'Message to console');

Message to console

and returned as the result of a function;

function getConsoleLogger() {
    return function(message) {
        console.log(message);
    }
}

let consoleLogger=getConsoleLogger();

consoleLogger('Message for console');

Message for console

Functions can be Anonymous

When you create callback functions in JavaScript for very simple, often one-off use, they quickly start to proliferate and become difficult to distinguish from each other. Anonymous functions are defined where they are needed/used and don't become a reusable part of the application. Using a very simplistic example, for some reason I want to process an array of numbers and multiply each entry by itself each entry. I'm going to use the map() method from the Array object, which creates a new array by executing a function I supply on every element in the source array. If I do this with named functions:

function multiply(value) {
    return value*value;
}
let numbers=[1, 2, 3, 4];
let squared=numbers.map(multiply);
console.log(squared);
[1, 4, 9, 16]

If I don't need the multiple function anywhere else, it's being exposed for no good reason, so I can replace it with an anonymous function that I define when invoking the map method:

let numbers=[2, 4, 6, 8];
let squared=numbers.map(function(value){return value * value});
console.log(squared);
[4, 16, 36, 64]

My anonymous function has no name and cannot be used anywhere else. It's also really hard to debug if you have a bunch of anonymous functions in your stack, so exercise a little caution when using them.

Arrow Functions improve on Anonymous

Especially for simple functions. Arrow functions (sometimes called fat arrow functions) give you a more succinct way to create anonymous functions.

numbers.map(function(value){return value * value});
I can lose a lot of the boilerplate text and just write:
numbers.map(value=>value*value);

Breaking this down:

  • I don't need the function keyword - I replace it with =>
  • I don't need parenthesis around my parameter, I just put it to the left of =>
    Note that if I have no parameters, or more than one, I do need parenthesis
  • I don't need the braces, as long as the code fits onto a single line
  • I don't need the return statement, again as long as the code fits onto a single line. The result of my expression on the right hand side of => is implicitly returned
Thus arrow functions can look pretty different to regular functions:
let multiply=function(value) {
    return value * value;
}

let arrowMultiply=value=>value*value;

or quite similar

let addAndLog=function(first, second) {
    let result=first + second;
    console.log('Result = ' + result);
    return result;
}

let arrowAddAndLog=(first, second)=>{
    let result=first + second;
    console.log('Result = ' + result);
    return result;
}

Arrow functions have a few gotchas too - the major one is 'this' always refers to the Window object, regardless of how you might try to change it. 

Functions have a Context

There's quite a bit to this (pun intended!) and as I mentioned in the first instalment, this isn't intended to be a JavaScript tutorial, so I can't see any point in replicating the Mozilla Developer Network content. Instead I'll just point you at it. The key thing to remember is 'this' depends on how the function is called, not where it is declared, so if you pass an object method as a callback function, when it is invoked 'this' won't refer to the original object, but whatever object is now invoking it. I'd recommend spending some time getting to grips with the context, otherwise you'll likely spend a lot more time trying to figure out why things don't work.

Related Posts

JavaScript for Apex Programmers Part 1 - Typing

Sunday, 28 November 2021

Salesforce++ Holiday Highlights

With the holiday season fast approaching, it's time to take a look at the feast of programming coming in the next few weeks, starting with one from my side of the pond.

The Great British Break Point


Amateur developers compete for the crown of Britain's Top Debugger. This week focuses on the user experience, where the breakers are tasked with crafting the perfect break point to identify why a user cannot successfully create an opportunity and its related products in a single transaction. Judges Paul Cricklewood and Prue L33t are on hand to deliver the verdict. 

On your marks ... get set ... break!

Bob and Mate: Plus 8


Introduction to simple formulas featuring me, Bob Buzzard, and an acquaintance from the Salesforce ecosystem. December's episode shows how to add 8 to various numeric fields, either directly or by calculating the value 8 using advanced mathematical operations like addition and multiplication. 

Licensed at First Sight


Five prospect companies who have never seen Salesforce before are matched up to license packs by a team of experts. Cameras follow the users as they get their first sight of the system on go live day. Look out for the follow up program in 8 weeks time, when the prospects decide if they want to stay licensed or break up their contract. 

Unlike other matchmaking shows, there is no cash prize for prospects who stay with their licenses - quite the reverse as they are then liable for the full cost of the license pack, even the ones they don't want!

Batched


Follow two Apex specialists as they remedy extreme asynchronous processing gone wrong. Whether it's a maximum scope of 1 record, or exceeding the 50 million records per day processing limit, there's always hope.

Film of the Month  - Hidden Triggers (2019)



Documentary featuring the unsung trigger heroes that keep enterprises moving. Whether it's overcoming limitations with roll up summaries, or simply copying an updated field from one sObject type to another, if these triggers fail then western civilisation would quickly grid to a halt. Filmed over five years with unparalleled access to version control, see for the first time how updates to these triggers are deployed and tested. 

Contains scenes of mild jeopardy and swearing at failed deployments.

See Also


If you enjoyed this post, you might like Salesforce++ Top Picks. And you might also like to question some of your life choices.