We’re here for you during the global COVID-19 pandemic. Learn more
Start tracking your progress
Trailhead Home
Trailhead Home

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 Builder. 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.

Create the Apex Class

Create an Apex class that posts a Thanks badge to your awesome instructor's profile. Similar to 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 text of the Thanks message or the specific badge you're giving, all you have to do is update the process, not the code. 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 this Apex 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 Flow Builder, as well as other authenticated clients via the REST API.

Build the Process

Now you can create the process that will fire the Thanks automation and post a badge to the instructor’s profile. Here we get to see one of Salesforce’s most powerful features: the ability to move seamlessly from clicks to code!

Remember that the other process we built only runs when dev classes (campaigns) are created. But the badge needs to be posted when a dev class is done, so our new process will also run when a dev class campaign is edited and marked as complete. 

  1. Fire up Process Builder.
  2. Click New and define the properties.
    • 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
    • Click Save.
  3. Configure the process to start when a campaign is created or updated.
    • Select the Campaign object.
    • Specify to start the process when a record is created or edited.
    • Click Save.
  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
    • Click Save.
      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 Class: Give a Thanks Badge
    • Apex Variable: Receiver ID | Field Reference | [Campaign].Instructor__c
    • Apex Variable: Giver ID | Field Reference | [Campaign].OwnerId
    • Apex Variable: Thanks Message | String | You are awesome!
    • Apex Variable: Badge Name | String | Thanks
    • Click Save.
      Set the Action
  6. Finally, activate the process.

Test the Process

All set! Now let's test this process. 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?


With the combination of Process Builder, Flow Builder, and invocable Apex methods, you've made your League of Extraordinary Coders 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: