My First APEX Trigger

Share this article...

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.

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.

SUBSCRIBE TO OUR MONTHLY NEWSLETTER

Connect with us

Address

Salesforce Ben
199 Bishopsgate
London
EC2N 4AG

Interested in guest posting?

Have something to share? Want to tell your story? We are always on the hunt for writers that interesting to say about the Salesforce platform and ecosystem. Join our group of 120+ trusted guest posters – Click here to start the conversation.