As a Salesforce Developer. There is no powerful debug tool in the developing process. It always confused the beginner. With the rise of the DX development. We need to install vs code and salesforce extension pack. Today. Let me introduce the apex replay debugger. And this tool has been included in the salesforce extension pack. So if you want to practice. Please make sure your machine had installed the vs code and salesforce extension. you can reference this article.
Cool. Let's get started. There are mainly three steps in the whole debug steps.
Get your debug log from Salesforce
When users do some operations in Salesforce. It might invoke the apex class or trigger. Normally. The platform doesn't generate the related execute log. So if you want to get a log from Salesforce. There are three ways.
- From setting. type the Debug Logs in the search box. And you can see a section named User Trace Flags. next. Click the new button. Select your want to trace user and debug level. After you save the trace flag. any operations will be traced. you can find these logs in Debug Logs section.
-
In the daily development. you must are familiar with the developer console. This is a powerful tool. I will introduce the developer console later in the next blog. now. you just know that when open the developer console. The operation log will be generated.
Developer Console Logs - As we learn the debugger from vs code. It's great that you can open the log trace function in vs code. Simply. just type Command + P. and enter >Turn On Apex Debug Log for Replay Debugger. another option. you can just input SFDX: Turn On.
Now. we have turned on the log trace in Salesforce platform. next step. What we need to do is putting the log in our project.
Place the log in the local project
Assumed that you have a project in your local machine. if don't have. This article may help you. Because the log had generated by Salesforce server. And you can get logs not only by download from Salesforce. But also you get the remote log from vs code by Salesforce extension. Open the vs code command palette. Type the SFDX:Get Apex Debug Logs. It may need to spend some time. The response will display many logs. you can pick the log by username and create time. Finally. The log your selected will download in your local automatically.
Another option. you can download the log from Salesforce both in the developer console and debug logs section.
Whether you get the log from vs code or salesforce. Finally. you should open the log in vs code.
Lanch debugger from vs code
Before we start the debugger. your vs code interface should like the below my screenshot.
Right-click in the log file. Select Launch Apex Replay Debugger with Current File option. It will awaken the debugger extension.
if you see a debug panel. Congratulation. next. Just enjoy the debug process.
Conclusion
Compared with the System.debug() method. Now you can debug code from vs code by apex replay debugger. It will improve development efficiency. And from another aspect. Currently, you can debug code just like Java or Python. The difference is that the whole debug process is based on the log. It's just a replay process. not real-time. So. if you update the code in the server. you need to regenerate and redownload the log. There are some inconveniences. Anyway, I hope you can enjoy it.