SaaSquatch Help Center

The SaaSquatch Managed Package for Salesforce includes a number of global classes and invocable methods that can be used to upsert users from APEX code. In this tutorial we will walk through how to create an APEX Trigger and use it to track events for users in SaaSquatch when a Contact is updated.

🔗 Before you start

This article is written for advanced users of Salesforce that understand Apex code and are comfortable with Apex triggers. If you are new to Salesforce there are a few Trailhead modules we recommend reading to get you started.

To get started you will need:

🔗 Create a new Trigger

To start open the Developer Console from inside of Salesforce.

DeveloperConsole

We are going to create a new Apex Trigger. Create one by going to File -> New -> Apex Trigger

NewTrigger

We will be triggering on Opportunity objects after they are marked as Won. You can name the trigger something that won't conflict with other trigger names in your environment.

trigger OppToContact on Opportunity (after insert, after update) {
    // Empty trigger ready for our logic
}

We want to only track events when an opportunity is marked as won, so we need to add some additional logic to check for when IsWon changes.

trigger OppToContact on Opportunity (after insert, after update) {

    for (Opportunity opp : Trigger.new) {
        if(opp.IsWon){  
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if(!oldOpp.IsWon){
               // Only trigger when opportunities become won

            }
        }
    }
}

🔗 Build records to insert

We're going to be building up SaaSquatchTrackEventByIdReq objects to send to the SaaSquatch API. Since triggers operate on batches, we're going to create a List of objects to build up.

trigger TutorialForSaaSquatch on Lead (after insert, after update) {
    List<SaaSquatch.SaaSquatchTrackEventByIdReq> userEvents = new List<SaaSquatch.SaaSquatchTrackEventByIdReq>();   
    // ... code from above ...
}

For each Opportunity that has been marked IsWon in this trigger we are going to copy that data to SaaSquatch. Since SaaSquatch tracks everything by user, we're also going to use the PrimaryReferralEmail__c field to be able to identify which user in the account was referred.

trigger OppToContact on Opportunity (after insert, after update) {

    List<SaaSquatch.SaaSquatchTrackEventByIdReq> userEvents = new List<SaaSquatch.SaaSquatchTrackEventByIdReq>();   
    for (Opportunity opp : Trigger.new) {
        SaaSquatch.SaaSquatchTrackEventByIdReq event = new SaaSquatch.SaaSquatchTrackEventByIdReq();
        if(opp.IsWon){  
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if(!oldOpp.IsWon){
                event.userId = opp.PrimaryReferralEmail__c;
                event.accountId = opp.PrimaryReferralEmail__c;
                event.key = 'oppwon';
                userEvents.add(event);
            }
        }
    }
}

The oppwon event key used here has no special meaning. SaaSquatch support dynamic event keys defined by your team. Ask your customer success team representative about what event keys are relevant to your program's data catalog.

🔗 Call the Invocable Method

Now that we have a list of events to track in SaaSquatch, we need to call the SaaSquatch invocable method to send the data to SaaSquatch.

We're sending the userEvents list to the SaaSquatchTrackEventById invocable method.

SaaSquatch.SaaSquatchTrackEventById.trackUserEvents(userEvents);

For this example we're using SaaSquatchTrackEventById but there are other InvocableMethods in the SaaSquatch package that may suite your use case, such as upserting users.

The final code should end up looking like this:

trigger OppToContact on Opportunity (after insert, after update) {

    List<SaaSquatch.SaaSquatchTrackEventByIdReq> userEvents = new List<SaaSquatch.SaaSquatchTrackEventByIdReq>();   
    for (Opportunity opp : Trigger.new) {
        SaaSquatch.SaaSquatchTrackEventByIdReq event = new SaaSquatch.SaaSquatchTrackEventByIdReq();
        if(opp.IsWon){  
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if(!oldOpp.IsWon){
                event.userId = opp.PrimaryReferralEmail__c;
                event.accountId = opp.PrimaryReferralEmail__c;
                event.key = 'oppwon';
                userEvents.add(event);
            }
        }
    }
    SaaSquatch.SaaSquatchTrackEventById.trackUserEvents(userEvents);
}

🔗 Test the Trigger

To test that this trigger is working:

  • Save your trigger
  • Make sure there are no alerts in the Problems tab of the Developer Console
  • Mark an Opportunity as Won in Salesforce
  • Open the SaaSquatch Admin Portal to confirm that the user related to the PrimaryReferralEmail__c has an oppwon event tracked.

🔗 Conclusion

In this tutorial we walked through creating a new trigger on Opportunity to track events in SaaSquatch whenever the opportunity is marked as won in Salesforce. Since the SaaSquatch API uses batches behind the scenes this create a low-overhead and near-realtime way of synchronizing data between Salesforce and SaaSquatch in order to trigger your referral and loyalty programs.