Salesforce Debugger from VS Code

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.

  1. 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.
  2. 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
  3. 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.

Log Location

Another option. you can download the log from Salesforce both in the developer console and debug logs section.
Download from Developer Console

Downlaod from 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.

VS Code Interface

Right-click in the log file. Select Launch Apex Replay Debugger with Current File option. It will awaken the debugger extension.
Launch Apex Replay Debugger

if you see a debug panel. Congratulation. next. Just enjoy the debug process.
Debug Panel

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.

你可能感兴趣的:(Salesforce Debugger from VS Code)