Automate Post-Workshop Tasks with Invocable Apex

Automate Post-Workshop Tasks with Invocable Apex

The final automation requires giving a Thanks badge to the Instructor. You can't automate this use case with just Process Builder or Flow. Never fear, invocable Apex is here!

What You'll Do

You're going to:

  1. Write a small piece of Apex code.
  2. Configure a process to call that Apex code.

You might be sensing a pattern here: even if Process Builder can't automate everything in a use case, you should start from Process Builder whenever possible.

Create the Apex Class

Create an Apex class that posts a Thanks badge to your awesome instructor's profile. Like with the flow you created, the process will pass information (Badge Name, Giver ID, Receiver ID, and Thanks Message) to the Apex class's methods. Why? So that if you later decide to update the Thanks message or the specific badge you're giving, you don't have to update the code. All you have to do is update the process. It's all about making your automations easy to maintain with clicks.

  1. Open the Developer Console.
  2. Click File | New | Apex Class.
  3. Give the class the name GiveWorkThanksAction.
  4. Copy the following code into the class.
global without sharing class GiveWorkThanksAction {

    @InvocableMethod(label='Give a Thanks Badge')
    global static void giveWorkBadgeActionsBatch(List<GiveWorkThanksRequest> requests) {
        for(GiveWorkThanksRequest request: requests){

    public static void giveWorkBadgeAction(GiveWorkThanksRequest request) {
        WorkThanks newWorkThanks = new WorkThanks();

                newWorkThanks.GiverId = request.giverId;
                newWorkThanks.Message = request.thanksMessage;
                newWorkThanks.OwnerId = request.giverId;

        insert newWorkThanks;

        WorkBadge newWorkBadge = new WorkBadge();

                // newWorkBadge.DefinitionId should be set to the ID for the Competitor Badge within this Org
                WorkBadgeDefinition workBadgeDef = [SELECT Id,Name FROM WorkBadgeDefinition WHERE Name = :request.badgeName Limit 1];

                newWorkBadge.DefinitionId = workBadgeDef.Id;
                newWorkBadge.RecipientId = request.receiverId;
                newWorkBadge.SourceId = newWorkThanks.Id ;
                //newWorkBadge.GiverId = request.giverId;

        insert newWorkBadge;

        WorkThanksShare newWorkThanksShare = new WorkThanksShare();

                newWorkThanksShare.ParentId = newWorkThanks.Id ;
                newWorkThanksShare.UserOrGroupId = request.receiverId;

                newWorkThanksShare.AccessLevel = 'Edit';
                insert newWorkThanksShare;

        FeedItem post = new FeedItem();

                post.ParentId = request.receiverId;
                post.CreatedById = request.giverId;
                post.Body = request.thanksMessage;
                post.RelatedRecordId = newWorkThanks.Id ;
                post.Type = 'RypplePost';

        insert post;


    global class GiveWorkThanksRequest {
        @InvocableVariable(label='Giver Id' required=true)
        global Id giverId;

        @InvocableVariable(label='Receiver Id' required=true)
        global Id receiverId;

        @InvocableVariable(label='Thanks Message' required=true)
        global String thanksMessage;

        @InvocableVariable(label='Badge Name' required=true)
        global String badgeName;

Notice the @InvocableVariable and @InvocableMethod annotations on this class. They let these methods be exposed to Process Builder and the Cloud Flow Designer, as well as to other authenticated clients via the REST API.

Build the Process

Now you can define the business process that will fire the Thanks automation and post a badge to the instructor. Here we get to see one of the really powerful features of Salesforce when you are writing code: the ability to bridge from clicks to code seamlessly!

Remember that the other process only fires when campaigns were created. Hopefully no classes will be Completed when they're created, so we'll create a process that fires when campaigns are edited, too.

  1. Fire up Process Builder.
  2. Click New and fill out the properties of the new process.

    • Process Name: Completed Class Sessions
    • API Name: Completed_Class_Sessions
    • Description: Instructor and Teaching Assistant Management for completed class sessions
    • The process starts when: A record changes
  3. Configure the process to start when a campaign is created or updated.

    1. Select the Campaign object.
    2. Specify to start the process when a record is created or edited.
  4. Configure the criteria. We want to thank our instructors only when a dev class campaign (not just any campaign) is complete.

    • Name: Dev Class Campaign Complete
    • Condition 1: [Campaign.Status] | Equals | Picklist | Completed
    • Condition 2: [Campaign.Type] | Equals | Picklist | Dev Class

    Process Criteria

  5. Now you can add an action that calls your freshly minted Apex class, ready to accept the four parameters that you annotated with the @InvocableVariable annotation. Note: You're referencing the badge by Name, which in this example happens to be 'Thanks'. If you reuse this code later, you can swap out a different badge name for Thanks.

    • Action Type: Apex
    • Action Name: Thank Instructor
    • Apex Variable: Badge Name | String | Thanks
    • Apex Variable: Giver ID | Field Reference | [Campaign.OwnerId]
    • Apex Variable: Receiver ID | Field Reference | [Campaign.Instructor__c]
    • Apex Variable: Thanks Message | String | You are awesome!

    Set the Action

Finally, activate the process.

Test the Process

All set! Now let's test it. As you would as the Chapter Leader, update some data.

  1. Open a campaign, where Type is set to Dev Class.
  2. Set its Status to Completed, and save your changes.
  3. Go to your Chatter feed. Is there a new post, thanking the instructor? Does it have the Thanks badge?

Thanks Badge


With the combination of Process Builder, flow, and invocable Apex methods, you've made Chapter Leaders more productive!

Give yourself a nice pat on the back. You deserve it!


For more hands-on learning about these tools, try the Process Automation module.

In addition, check out these resources: