Salesforce belongs to the CRM (Customer Relationship Management) family. It’s a tool to help anyone who wants to track the commercial activity of his/her company, managing potential customers, final customers, contacts and sales, among other actions.
Here we’re talking about a Cloud solution. Therefore, the infrastructure will be completely transparent, so that we don’t need to focus our efforts on assembling machines or installing software to have at least one system working.
Let’s talk about the integration
In this case, for the development we’ve used Java. Let’s see which options we have to communicate through the platform.
Officially, the Salesforce company offers REST or SOAP API to interact with some of their services. This is the first step. At least we know we’ll have a bunch of HTTP calls more or less standard, that we’ll be able to do by using some usual tools as Jersey. This can be quite annoying if we have to write the calls and enter the parameters one by one. So let’s try a more useful solution. There are people already working in wrappers to use this API, wrapping the HTTP calls with simple objects and Java services.
The library is called force-rest-api, and it’s available either to clone on github or to download in Java with Maven.
How to do the installation?
Add the section <dependencies> of the file pom.xml from our project
[java] <dependency><groupId>com.frejo</groupId>
<artifactId>force-rest-api</artifactId>
<version>0.0.30</version>
</dependency>
[/java]
Use
The library offers us ForceApi, allowing us to communicate with Salesforce.
Firstly, we’ll need to issue it by placing the url, user and password in an ApiConfig Instance.
Java Code:
[java] ForceApi api = new ForceApi(new ApiConfig().setUsername(sfUser)
.setPassword(sfPassword)
.setLoginEndpoint(sfUrl));
[/java]
From here, we can do any CRUD operation about the Salesforce objects. There are several methods to be highlighted:
query (String query, Class targetClass)
It returns a string of SF Objects automatically transformed to the class we specify. It will be passed as a param in a way quite similar to SQL.
createSFObject (String type, Object object)
The sent object of the type specified by the parameter type will be created in Salesforce.
updateSFObject (String type, String id, Object object)
The object of the type specified by the parameter type and identified by the id parameter will be updated in Salesforce.
deleteSObject (String type, String id)
It eliminates in Salesforce a type object referenced by id.
The only thing left is to describe the entities created in Salesforce that will interact with those of our business. I’d like to highlight the library uses the REST services with JSON. Also, to do the parsing it uses Jackson in the background. Jackson is a transformation library we’re used to use in any project using an API. For instance, the Lead entity would become like this:
Java code
[java] public class Lead {@JsonIgnore
@JsonProperty(value = “Id”)
private String id;
@JsonIgnore
@JsonProperty(value = “CreatedById”)
private String createdById;
@JsonIgnore
@JsonProperty(value = “LastModifiedById”)
private String lastModifiedById;
@JsonIgnore
@JsonProperty(value = “OwnerId”)
private String ownerId;
@JsonIgnore
public String getId() {
return id;
}
@JsonProperty(value = “Id”)
public final SFObject setId(final String id) {
this.id = id;
return this;
}
@JsonIgnore
public String getCreatedById() {
return createdById;
}
@JsonProperty(value = “CreatedById”)
public SFObject setCreatedById(final String createdById) {
this.createdById = createdById;
return this;
}
@JsonIgnore
public String getLastModifiedById() {
return lastModifiedById;
}
@JsonProperty(value = “LastModifiedById”)
public SFObject setLastModifiedById(final String lastModifiedById) {
this.lastModifiedById = lastModifiedById;
return this;
}
@JsonIgnore
public String getOwnerId() {
return ownerId;
}
@JsonProperty(value = “OwnerId”)
public SFObject setOwnerId(final String ownerId) {
this.ownerId = ownerId;
return this;
}
@JsonProperty(value = “FirstName”)
private String firstName;
@JsonProperty(value = “LastName”)
private String lastName;
@JsonProperty(value = “Company”)
private String company;
@JsonProperty(value = “CIF__c”)
private String cif;
@JsonProperty(value = “Email”)
private String email;
@JsonProperty(value = “Street”)
private String street;
@JsonProperty(value = “City”)
private String city;
@JsonProperty(value = “State”)
private String state;
@JsonProperty(value = “Country”)
private String country;
@JsonProperty(value = “PostalCode”)
private String postalCode;
@JsonProperty(value = “Status”)
private String status;
@JsonProperty(value = “MobilePhone”)
private String mobilePhone;
@JsonProperty(value = “NIF__c”)
private String nif;
[/java]
Notes:
*Fields with a JsonProperty ending with __c are not standard in Salesforce, but personalized.
**Fields with a @JsonIgnore in the Getter are read-only in Salesforce.
Leave a Reply