Skip to content

Salesforce Hack: Debugging in Apex

 

Salesforce Admin Tips
As a Developer, regardless of what language you are using or what environment you are working in, the ability to debug your code is crucial and something few developers can live without. When I started using Apex, Salesforce’s native language, it took me a while to learn how to best debug my code which led to a few frustrating days. In this blog, I will show you a helpful technique for debugging your Apex code that both Developers and Salesforce Admins can utilize. The syntax I will use in this blog can be used in Apex code located anywhere in Salesforce, I will use a Salesforce trigger as an example. All you need to follow along is a free Salesforce developer account. (If you don’t already have one, you can sign up for a free one using the following link: https://developer.salesforce.com/signup) All we are going to do is create a simple trigger that will fire when you update an account, as displayed below: blog1 “System.debug(‘account updated: ‘ + acc.name);” is all that will execute for each account that is updated. System.debug function will take a message in string format and write it to the execution debug log. To see this in action, we open up our Developer Console by clicking on the user drop down option (named after our Salesforce user name) located in the top right hand side of Salesforce and click on Developer Console and navigate to the log section as displayed in the screen shot below: blog2 Now if you simply update an account, you should see a number of execution log files appear in the log section of the Developer Console as displayed in the screen shot below: blog3 My first reaction on seeing this was to run as far as I can, how am I supposed to make sense of this? I promise that will go away after the first few time these log files come to your rescue. We may have to go through the log files to find the one that includes our message but the log time displayed in the Developer Console should help sort them in the order of execution and only look through the ones which were logged recently and within the same time. In the screen shot below, we have found the one that has our message and I have filtered messages in the file to debug only. blog4 As you can see, we have updated the account named “test1”. Of course, in the real world, there is no need for us to log this out but this is an example of how you can debug your Apex code instead of writing code in the dark and crossing your fingers when you hit run.  To read more about Apex’s System Class, click here.

Soroosh Avazkhani

Junior Developer at Ebsta. Obsessed with finding and sharing all the best Salesforce Admin hacks to help you achieve Salesforce greatness!

Related Posts

Give your Sales Reps access to the best data

Long gone are the days where you had to rely on your Reps to add emails or data into Salesforce. Ebsta does it automatically. Reps now have access to 100% data accuracy without having to do a thing.
Find out more