My First APEX Trigger

I went out on a mission last week to write my first APEX Trigger, the start of a long road of learning how to develop. Back when I used to be a Salesforce Administrator It was always very inconvenient when development needed to happen on my Org and I had to bring in an outside developer. So I thought it was better late that never to start learning! Continuing on my Salesforce journey, I also thought it would be a great addition to add to my skill set and a natural step after learning how to use declarative point and click Salesforce methods.

Whenever I did need to hire an outside developer to program APEX for me, it was always because workflows could not stretch across multiple objects and therefore a trigger needed to be used. I decided to focus on one of these triggers for my first mini project.

After doing a bit of research on how to go about creating my first Trigger I came across a Blog by David Liu. This is EXACTLY what I was looking for. You can place me in the camp that can confidentially point and click my way around Salesforce, but when it comes to programming, I wouldn’t know where to start. David has put together a curriculum like , step by step tutorial, starting with the real basics and getting into the really meaty stuff. If you are interested in following his tutorial then start reading here.


As well as his blog, David has teamed up with Salesforce and presents some of his starting material into three hour long youtube videos which really help get you started, the first is below.

After I armed myself with some basic Object Orientated insight by using David’s blog and his helpful youtube video series, I went off trying to create my first Apex Trigger. One thing I found tremendously helpful was simply copy and pasting David’s code for some basic triggers and then dissecting this until I fully understood what each component was doing.

The trigger I wanted to create was related to the Service cloud. A feature of the service cloud is called Entitlements. This allows service agents to relate a case to a customers Entitlement, allowing you to keep track of how many cases a customer is opening. I wanted to create a trigger that automatically populated the Entitlements field on a case with the most recent entitlement on the customers account. I unfortunately do not have a Service Cloud Dev instance so had to create a object called Support Agreements (aka Entitlements). The below is what I came up with.

This trigger is really simple and uses a mix of APEX and SOQL to go and grab the Entitlement from the account of the case we are working off.

trigger SupportAgreement on Case (before insert) {
for (Case c : {

String AccId = c.AccountId;
List<Support_Agreement__c> supportagreements = [SELECT Id FROM Support_Agreement__c
WHERE Account__c = :AccId

c.Support_Agreement__c = supportagreements[0].id;


That’s my update for my first week on the road to become a certified Salesforce Developer! If you want to do a similar thing then I strongly advise you to check out David’s blog, its got a huge amount of content and he is very good at explaining what he does best. Although what I have built is small and does a very simple job. I think the most important thing is to understand exactly what your code is doing and then build on from there. I will be updating my blog again once I have built something a bit more impressive! If anyone is interested, my test class for this is below.

public class TestSupportAgreements {
static testMethod void testSA() {

//Create an Account

Account a = new Account();
a.Name = ‘TestAccount’;
insert a;
String AccId = a.Id;

//Create a support agreement

Support_Agreement__c sa = new Support_Agreement__c();
sa.Name = ‘Test Support Agreement’;
sa.Account__c = AccId;
insert sa;

//Create a case

Case c = new Case();
c.Origin =  ‘Phone’;
c.AccountId = AccId;
insert c;





Subscribe To The Monthly Newsletter

No Spam. No Rubbish. Just great content from the Salesforce Industry.

You have Successfully Subscribed!

Add Comment