Business Opportunity Has No Year-End

The end of the year brings with it a sense of accomplishment for all that was achieved in 2018.  The accountants scramble to finalize the numbers and everyone looks forward to the start of a new year. After all, 2019 brings with it the promise of continuing growth, expanded market share, improved profit, and the next big business opportunity.

Read more

Why Invocable Methods Could Spell the End for Apex Triggers by Amnon Kruvi

Amnon Kruvi, Salesforce Solutions Architect
1218 Global – EMEA
Amnon.Kruvi@1218global.com

If you have written code on Salesforce before, chances are you have built an Apex trigger in your lifetime. However, on a platform that prides itself on its Clicks, Not Code mentality, the question must be begged: Are triggers the best solution to automate logic that cannot be handled by out-of-the-box declarative tools?

The Role of the System Administrator

Before we delve into this question, there is something that needs to be made clear: What is a system administrator?

Sure, a good system admin will configure a Salesforce org, manage its users and apps, and resolve issues on the system. But more than that, the administrator is often the person who decides what route a user needs to take in order to fulfill their tasks – what apps should be used, and how those apps should operate to achieve the best results for the business. And, if the need arises, the administrator can play the role of the app builder – modifying system behavior by creating workflow rules, processes, flows, and more.

Or in short: a fantastic, knowledgeable, capable resource.

Source: XKCD. Altered to remove swearing – we’re trying to be professional here.

And our role, as developers, is to allow administrators to achieve these requirements with as much flexibility as they require.

The Code Problem

Flexibility is where code fails administrators. When they install a trigger, they cannot edit its code. And when their requirements inevitably change – the code does not. When this happens, the only resort is to call upon the developer again (assuming they are still available), make that change, and go through an entire release cycle.

This includes small changes to the flow of the code – should this procedure run for every contact that goes into the system, or only for a certain record type? Imagine the frustration of having to call up the development team just because you installed a new app, which came with its own record types, and you need to make code changes just to decide which of them your procedure should process.

Invocable Methods are a Middle Ground

But what if a developer could write a procedure, and allow the system administrator to decide the conditions in which to execute it? That is exactly the issue Salesforce aims to resolve with their Invocable Methods mechanism.

These methods can be executed using two popular declarative tools: Process Builder, and Lightning Flow. They are written almost exactly like normal methods, can accept input parameters (with a few caveats), and can produce an output value. Here is an example:

@InvocableMethod(Label='Count Open Cases' Description='Returns a count of open cases for the provided contact')

public static List<Decimal> countOpenCases(List<Id> contactId) {

//Query the amount of open cases for all contacts

Map<Id, Decimal> mapResults = new Map<Id, Decimal>();

for (AggregateResult ar : [SELECT ContactId con, COUNT(Id) cnt

FROM Case

WHERE ContactId IN :contactId

AND IsClosed = false

GROUP BY ContactId]) {

//Store the amount of open cases for each contact in a map

mapResults.put((Id)ar.get('con'), (Decimal)ar.get('cnt'));

}

//Sort the amount of open cases into a list with the same order as the contactId list

List<Decimal> res = new List<Decimal>();

List<Contact> contactsToUpdate = new List<Contact>();

for (Id conId : contactId) {

Decimal val = mapResults.get(conId);

if (val == null) {

val = 0.0;

}

res.add(val);

contactsToUpdate.add(new Contact(Id = conId, Open_Cases__c = val));

}

//Store the amount of open cases on the contact records

update contactsToUpdate;

return res;

}

The function accepts an input (a list of contact IDs), produces an output (a list of open cases for each contact on the list), and even has a nifty label and description annotation which is bound to come in handy for app builders, as well as whoever ends up maintaining the code.

An administrator could use Process Builder to trigger this piece of code whenever a certain event happens to a record (for example, when a new case is created) and only when certain criteria are met (such as clients with a limited service contract) using the built-in criteria and decision steps built into process builder and flows. They can also store the return values back into flow variables and use them later on in decision steps!

This means Process Builder and Lightning Flow have become tools that can orchestrate the flow of logic and attach conditions to whether each method will be executed. This has long been the job of triggers, but now that power lies within the grasp of the declarative app builders. When they add a new record type to the organization, they can modify the process themselves and configure their preferred handling; they can create their own custom settings to toggle certain functionality on or off; they can trigger the code on record creation and record updates, and even limit it to only run when specific fields have been changed.

Getting Technical

So, what are the bits you need to know about creating Invocable Methods? Well, that’s simple!

To start with, you just need to add the @InvocableMethod annotation to any function. The annotation accepts two parameters: Label and Description. Use these to create a clear idea of what your method does – and they will show inside the process and flow builders.

Your Invocable Method can accept either no parameters, or one parameter. That parameter must be a list of values, and not a single instance. This is because flows and processes are bulkified, and the data from several instances may be sent to your method at once. This is a great thing when considering performance and governor limits!

If you need more than one parameter, you can create your own inner class with its own fields and accept a list of that class. The class can have many member variables, and those you mark with the annotation @InvocableVariable will be shown as possible input parameters in the flow or process.

public class CountParams {

@InvocableVariable(required=true)

public Id contactId;

@InvocableVariable

public Boolean commitToDatabase;

}

Another thing to keep in mind is that any one class can only have one Invocable Method. If you want more utilities, you need to create more classes.

Verdict: So, Are Triggers Dead?

The short answer is: No.

Apex triggers run in any of 8 contexts – a combination of before or after an event (insert, update, delete, undelete). Declarative tools in Salesforce only run in two contexts: after insert, and after update. This means that declarative logic cannot be executed when a record is deleted or undeleted and cannot make changes to a record before it is committed to the database. That remains the domain of triggers.

For after insert and update logic, however, it is becoming clear that declarative tools are slowly gaining an advantage with the help of code. After all, wouldn’t it be great if an admin, rather than submitting change requests to you, could handle these things by her or himself?

Isn’t it great to have a platform that is increasingly accessible to all?

About Amnon Kruvi

Salesforce Architect and Development leader with a background in startup companies and consultancy specializing in Force.com, CRM, and an assortment of web and mobile technologies.

Amnon has excelled as the technical lead to execute and manage projects from the very beginning stages to public release, and on to large international contracts. His technical expertise allows Amnon to analyze customer requirements, design a solution, and follow it to completion – either in a managing or development capacity.

About 1218 Global

A worldwide leader in consulting innovation, 1218 Global and its family of professional consulting companies (Convectus Solutions, and 1218 Global HR Solutions) were each founded with unique delivery capabilities. From systems implementations and upgrades to Managed Services (EMEA, APAC and USA), 1218 Global has developed a track record for measurably improving client productivity, effectiveness, and profitability.

“Old” and “Young” Workers – A Perspective

Jonathan Brostrom

Principal Consultant, 1218 Global – EMEA

jonathan.brostrom@1218global.com

Your Age. Such a simple enough entry on an application. Date of Birth. Age. A number that comes pre-loaded with conceptions, preconceptions, stereotypes, legal implications, attitudes, and story lines. It is the same with your photograph or in-person appearance. Age. What an interesting and unavoidable truth.

Read more

The Client/Consultant Relationship: An Interview with Mark Crist, Part 1

This is the first in a three-part series of interviews with Mark Crist, Chief Delivery Officer, 1218 Global. The series explores the critically important client/consultant relationship and how to properly balance resources, maintain open communication, and deliver successful implementation and upgrade results.

Read more

The Client/Consultant Relationship: An Interview with Mark Crist, Part 2

This is the second in a three-part series of interviews with Mark Crist, Chief Delivery Officer, 1218 Global. The series explores the critically important client/consultant relationship and how to properly balance resources, maintain open communication, and deliver successful implementation and upgrade results.

Read more

The Client/Consultant Relationship: An Interview with Mark Crist, Part 3

This is the third in a three-part series of interviews with Mark Crist, Chief Delivery Officer, 1218 Global. The series explores the critically important client/consultant relationship and how to properly balance resources, maintain open communication, and deliver successful implementation and upgrade results.

Read more

Talent is Everything

At the center of a successful consultancy is the core platform of talent that drives it: a knowledgeable and experience-based pool of resources proven in their capabilities and vetted in their skills, which can be deployed with confidence to meet the varied needs of clients.

Read more

Creative Evolution

No company survives without predicting, adapting to, or innovating change. Stasis is fatal.

The world culture itself is suffused with technological enhancements to how work is done. Dramatic changes made to machines, to software, to the manner of communication that dominates and who dominates it; to the materials that are purchased and resources required, to the software and systems that must be built, bought, licensed and deployed – all these changes must be managed, anticipated and absorbed.

Read more

How to Make a Killer LinkedIn Profile

When it comes to your LinkedIn profile, you need to consider your audience. The typical recruiter looks at your profile and knows whether or not you are the right candidate in the first 5-10 seconds. So, naturally, it makes sense that your profile should wow a recruiter right off the bat. But how do you do that? We went to our resident LinkedIn and recruiting expert, Director of Recruiting and Resource Management, Kelly Hooten, to get to the bottom of that exact question.

Read more