Salesforce data in Java?

How to convert APEX object into Java class

Vergil
3 min readMar 5, 2023

Excel is not database. Neither is Salesforce. But it some cases, you need to get some data stored in Salesforce into your web service.
No blame, no questions asked. Let’s see how to do it.

You need:

  • instance url and access token
  • java classes representing salesforce objects
  • REST API client

Classes

Salesforce uses APEX language which is derived from Java.
Object classes however doesn’t use java native types (string, int, boolean, etc) but it’s own (Text, Email, Address, Checkbox, etc) and so we have to do our best to guess to what java type to cast them into.
We can find object names and fields in our sf instance > Setup > Object Manager > [sobject name]

Account object as an example

Types are described by Salesforce in FieldDefinition. Mapping is required but it’s not hard to guess that. Checkbox is boolean, Text is string, etc.
You find object fields in Fields & Relationships section.
Some objects can be nillable (not a typo, that’s how SF is calling it). You can check each field by clicking on it and if there’s required field unchecked, it’s nillable, if it’s chacked (or missing at all, like it was at the writing this article), then it’s required. Java is not null-safe, but in other languages you may want to pay attention to this.
Let’s see an example of our Account SObject.

Account sObject in Java

Rest API client

To query the data from Account table you need to create REST API client.
You can use any dependency to make your life easier.

url parameter in FeignClient is your salesforce instance URL. You see it when you are logged-in in Salesforce.

In this example we’ll use Spring Feign Client.

salesforce query client in Java using FeignClient

As you can see, we will need

  • accessToken (it’s Bearer auth) and
  • SOQL query.

SOQL query which consists of attributes and sObject name of desired sObject, and it is url encoded. So it’s something like:

SELECT%20Name%20FROM%20Account

Since we are already using Spring framework (that’s where Feign client comes from), we wrap this logic in SalesforceQueryComponent and run query on start in @PostConstruct method.

salesforce query component

When we run our demo application, it will output names of all accounts from our salesforce instance. Well… not all, we didn’t implement pagination, so only from first page. This is just a demo.

Demo on my GitHub

I’m putting this working demo to my GitHub public repo, so you can check it out and try it. You just change application.properties

There’s an easier way. 👇

Salesforce Codegen

As you can see, I’ve spent some time working on the best solution.
Here I presented only the essentials to get those data but in reality you need more functionality and it takes a lot of time to develop it all by yourself.

I created a platform where you just select sObjects you need to implement (wheter standard or custom) and it will generate code for you in a second.
With classes, abstracts, query client, all.
It’s framework agnostic, so you are not bound to use specific framework. Copy&Paste and you are good to go.

Check free demo at http://salesforce.codegen.link/

--

--

Vergil

I just love modern programming languages, don’t you?