Salesforce, clasificado dentro de la familia de los CRM (Customer Relationship Management), una herramienta que le facilitará la vida a quien quiera llevar un seguimiento de la actividad comercial de su empresa, gestionando clientes potenciales, clientes finales, contactos y ventas; entre otras acciones.
En este caso estamos hablando de una solución Cloud y como tal, su infraestructura será totalmente transparente, es decir, no nos tenemos que preocupar por dedicar esfuerzo a montar máquina, ni instalar software, para tener al menos un sistema funcionando.
Hablemos de la integración…
En este caso, para el desarrollo, hemos utilizado el lenguaje Java, así que vamos a ver qué opciones tenemos para comunicarnos con la plataforma.
De manera oficial, la empresa Salesforce ofrece un API vía REST o SOAP para poder interactuar con sus variados servicios. Ya es un primer paso, al menos sabemos que tendremos una colección de llamadas HTTP más o menos estándares, que podremos realizar mediante algunas herramientas conocidas como Jersey. Esto a las bravas puede ser un engorro, tener que picar cada una de las llamadas y parámetros a mano… Así que como todo buen progra vamos a investigar alguna solución más práctica y efectivamente, ya hay gente que se ha currado un envoltorio para utilizar dicho API, envolviendo las llamadas HTTP con simples objetos y servicios Java.
La librería en cuestión es force-rest-api , disponible tanto para clonar en github, como para descargar en java con Maven.
¿Cómo realizar la instalación?
Añadir a la sección <dependencies> del fichero pom.xml de nuestro proyecto
[java] <span style="font-weight: 400;"><dependency></span><span style="font-weight: 400;"> <groupId>com.frejo</groupId></span>
<span style="font-weight: 400;"> <artifactId>force-rest-api</artifactId></span>
<span style="font-weight: 400;"> <version>0.0.30</version></span>
<span style="font-weight: 400;"></dependency></span>
[/java]
Uso
La librería nos ofrece la clase ForceApi que será la que nos permita realizar toda comunicación con Salesforce.
Lo primero será instanciarla pasando la url, usuario y password en una Instancia de ApiConfig.
Código Java:
[java] <span style="font-weight: 400;">ForceApi api = new ForceApi(new ApiConfig()</span><span style="font-weight: 400;">.setUsername(sfUser)</span>
<span style="font-weight: 400;">.setPassword(sfPassword)</span>
<span style="font-weight: 400;">.setLoginEndpoint(sfUrl));</span>
[/java]
Desde aquí ya podremos realizar cualquier operación CRUD sobre los objetos de Salesforce, a destacar los métodos
query (String query, Class targetClass)
Devuelve una lista de Objetos de SF y transformados automáticamente a la clase que indiquemos. Se pasará por parámetro una consulta muy semejante al familiar SQL.
createSFObject (String type, Object object)
Se creará en salesforce el objeto enviado del tipo indicado por el parámetro type.
updateSFObject (String type, String id, Object object)
Se actualizará en Salesforce el objeto de tipo indicado por el parámetro type e identificado por el parámetro id.
deleteSObject (String type, String id)
Elimina un objeto en Salesforce de tipo type y referenciado por id.
Ya solo nos quedaría escribir las entidades que se han creado en Salesforce que serán las que interactúen con las de nuestro negocio. Me gustaría destacar que la librería utiliza los servicios REST con JSON, y para realizar el parseo utiliza por debajo Jackson, librería de transformación que estamos acostumbrados a utilizar en cualquier proyecto que publique un API. Así por ejemplo la entidad Lead (Candidato) quedaría de la siguiente manera.
Código Java:
[java] <span style="font-weight: 400;">public class Lead {</span><span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">@JsonProperty(value = "Id")</span>
<span style="font-weight: 400;">private String id;</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">@JsonProperty(value = "CreatedById")</span>
<span style="font-weight: 400;">private String createdById;</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">@JsonProperty(value = "LastModifiedById")</span>
<span style="font-weight: 400;">private String lastModifiedById;</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">@JsonProperty(value = "OwnerId")</span>
<span style="font-weight: 400;">private String ownerId;</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">public String getId() {</span>
<span style="font-weight: 400;">return id;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonProperty(value = "Id")</span>
<span style="font-weight: 400;">public final SFObject setId(final String id) {</span>
<span style="font-weight: 400;">this.id = id;</span>
<span style="font-weight: 400;">return this;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">public String getCreatedById() {</span>
<span style="font-weight: 400;">return createdById;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonProperty(value = "CreatedById")</span>
<span style="font-weight: 400;">public SFObject setCreatedById(final String createdById) {</span>
<span style="font-weight: 400;">this.createdById = createdById;</span>
<span style="font-weight: 400;">return this;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">public String getLastModifiedById() {</span>
<span style="font-weight: 400;">return lastModifiedById;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonProperty(value = "LastModifiedById")</span>
<span style="font-weight: 400;">public SFObject setLastModifiedById(final String lastModifiedById) {</span>
<span style="font-weight: 400;">this.lastModifiedById = lastModifiedById;</span>
<span style="font-weight: 400;">return this;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonIgnore</span>
<span style="font-weight: 400;">public String getOwnerId() {</span>
<span style="font-weight: 400;">return ownerId;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonProperty(value = "OwnerId")</span>
<span style="font-weight: 400;">public SFObject setOwnerId(final String ownerId) {</span>
<span style="font-weight: 400;">this.ownerId = ownerId;</span>
<span style="font-weight: 400;">return this;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">@JsonProperty(value = "FirstName")</span>
<span style="font-weight: 400;">private String firstName;</span>
<span style="font-weight: 400;">@JsonProperty(value = "LastName")</span>
<span style="font-weight: 400;">private String lastName;</span>
<span style="font-weight: 400;">@JsonProperty(value = "Company")</span>
<span style="font-weight: 400;">private String company;</span>
<span style="font-weight: 400;">@JsonProperty(value = "CIF__c")</span>
<span style="font-weight: 400;">private String cif;</span>
<span style="font-weight: 400;">@JsonProperty(value = "Email")</span>
<span style="font-weight: 400;">private String email;</span>
<span style="font-weight: 400;">@JsonProperty(value = "Street")</span>
<span style="font-weight: 400;">private String street;</span>
<span style="font-weight: 400;">@JsonProperty(value = "City")</span>
<span style="font-weight: 400;">private String city;</span>
<span style="font-weight: 400;">@JsonProperty(value = "State")</span>
<span style="font-weight: 400;">private String state;</span>
<span style="font-weight: 400;">@JsonProperty(value = "Country")</span>
<span style="font-weight: 400;">private String country;</span>
<span style="font-weight: 400;">@JsonProperty(value = "PostalCode")</span>
<span style="font-weight: 400;">private String postalCode;</span>
<span style="font-weight: 400;">@JsonProperty(value = "Status")</span>
<span style="font-weight: 400;">private String status;</span>
<span style="font-weight: 400;">@JsonProperty(value = "MobilePhone")</span>
<span style="font-weight: 400;">private String mobilePhone;</span>
<span style="font-weight: 400;">@JsonProperty(value = "NIF__c")</span>
<span style="font-weight: 400;">private String nif;</span>
[/java]
Notas:
*Los campos cuya JsonProperty termina en __c no son estándar en Salesforce, son personalizados.
**Los campos con un @JsonIgnore en el Getter son de sólo lectura en Salesforce.
Deja una respuesta