Speeding up your Salesforce APEX Development With C# and Visual Studio

ApexSharp is an opensource C# library that allows you to write code in C# and convert them to APEX; it also converts APEX code to C#. Other Transpiler projects inspired the idea for ApexSharp. For example:

While the above implementation is one-way transpilers, ApexSharp is a two-way transpiler which can convert APEX to C# and C# to APEX.

Productivity gains of using ApexSharp and develop your Salesforce APEX code in C#.

1. The speed of the development is not dependent on your Salesforce org speed. All your compiles and unit test run at local development environment as you are developing locally

Above screenshot shows how you can run real-time unit testing while you code and view the resulting data from Salesforce as the underlying C# library is always connected to Salesforce instance you are working on.

2. You can use a real debugger to debug your code.

Above screenshot shows how you can put a breakpoint in your C# code and view the resulting data from Salesforce, while you are running your code through the visual studio debugger we are connected to Salesforce instance you are working on.

3. Visual Studio is the best IDE in the market, the additional add-on such as Oz Code, ReSharper makes software development even more productive.

ApexSharp code is based on .NET Core 2.0, thus in addition to Visual Studio, you can use it under Visual Studio Code (Windows, Mac, Linux), Visual Studio For Mac.

4. Take advantage of Visual Studio Team Services (Git, Project management, Continues integration etc).

5. Implement Behavior-driven development (BDD) using SpecFlow and Gherkin  (Example)

6. Use 3rd party C# libraries during development by using the NoApex attribute (Example).

7. Support for Salesforce DX is included. If your code compiles on C# then its guaranteed to compile on Salesforce as Apex is a 1 to 1 translator.

As long as you have the Salesforce DX CLI installed, you can push code to your scratch org or dev org automatically without even seeing the generated APEX code.

High-level Architecture of ApexSharp

ApexSharp contains the following three distinct parts.

  • C# to APEX Code Converter

Take C# code and convert to APEX. We use MS Roslyn to generate the AST and then create APEX code from it.

  • APEX to C# Code Converter

Take APEX code and convert to C#. A custom parser was developed to parse the APEX source and generate the AST. Then we create C# code from this AST.

  • A C# API Salesforce API

A method signature for every Salesforce API. Keep in mind most of the API implementation has not been implemented and those you can mock it or write your own and contribute to the project.

Salesforce API

We are trying to map every Salesforce APEX API to a .NET API. You can view them at https://github.com/jayonsoftware/apexsharp/tree/master/Apex

For example the following C# code,

System.Debug("Hello World");

Is mapped directly to the API and it will provide the same function as what Salesforce provided.

When you write the C# code, instead of writing

Console.Writeln("Hello World");

You will write

System.Debug("Hello World");

For a more complex example, let’s look at SOQL in C#.

List<Contact> contacts = SOQL.Query("SELECT Id, Email, Name FROM Contact");

When the above line is executed in C#, this calls the .NET ApexSharp API, which intern uses the Metadata API to call Salesforce and get the data. This allows you to debug the results and work with real data. Once everything is working the above line will automatically get converted to its APEX equivalent

List<Contact> contacts = [SELECT Id, Email, Name FROM Contact];

Like it so far? Take a look at the following 10 min video to see all this in action

Or Jump into code by going to the apexSharp project GitHub