Introducción :
En esta publicación, quiero mostrarle cómo usar generar ID de persona y contratar empleados en SF usando la operación $batch en CPI.
¿Cómo utilizar la API de generación de ID de persona?
Paso 1:
Agregue una conexión HTTP y coloque DIRECCIÓN : https://{servidorAPIdelcentrodedatos}/odata/v2/generateNextPersonID
Paso 2 :
La respuesta vendrá así, donde «10010» es el siguiente ID de persona:
<d:GenerateNextPersonIDResponse xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:type="SFOData.GenerateNextPersonIDResponse">
<d:personID>10010</d:personID>
</d:GenerateNextPersonIDResponse>
Paso 3 :
Estoy eliminando «d:» usando un script maravilloso para almacenar personID en una propiedad usando el modificador de contenido para su uso posterior.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.lang.*
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
body = body.replaceAll("d:", "");
message.setBody(body);
return message;
}
¿Por qué estoy usando $ lote para crear empleados?
Para contratar a un empleado en SF, hay algunas entidades obligatorias que debemos crear en secuencia. La secuencia de entidades es la siguiente:
Y, además, estamos creando otras entidades como PerNationalId, PerAddressDEFLT y PerPhone para crear un empleado en esta publicación.
Como no se admite la creación parcial de un empleado, estoy usando la operación $batch porque si alguna de estas entidades tiene un error, entonces el empleado no se creará. Solo si todas las entidades tienen el valor correcto y están en el formato correcto, solo se creará el empleado en SuccessFactor.
Paso 1:
Conversión de XML a JSON después de mapear todos los campos obligatorios de todas las entidades.
Paso 2:
Estoy tomando solo los nombres de los campos del cuerpo JSON de las entidades y almacenándolos en una propiedad.
Por ejemplo :
Cuerpo JSON original:
{"User":{"User":{"userId":"10010","firstName":"Peggy","lastName":"Carter","hireDate":"2023-02-27T00:00:00.000","gender":"F","nationality":"999999999999","status":"t"}}}
Cuerpo JSON después de usar un script maravilloso:
"userId":"10010","firstName":"Peggy","lastName":"Carter","hireDate":"/Date(1677456000000)/","gender":"F","nationality":"999999999999","status":"t"
Y almacenar este cuerpo en propiedad con la ayuda del modificador de contenido.
Nota: también necesita convertir la fecha y hora al formato EPOCH, puede hacerlo mediante el script dado:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.xml.StreamingMarkupBuilder;
import java.text.SimpleDateFormat;
import java.util.Date;
import groovy.xml.*; def Message processData(Message message) {
def properties = message.getProperties();
def hiredate = properties.get("hiredate");
if(hiredate!=''){
def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
def date = sdf.parse(hiredate)
def epoch = date.time
dotNetDate = "/Date(" + epoch + ")/"
}
else{
dotNetDate="";
}
message.setProperty("hiredate", dotNetDate);
return message;
}
Paso 3 :
Simple Concatenar todas las entidades usando Reunir y agregar Modificador de contenido para definir el cuerpo:
Agregue encabezado en el modificador de contenido y coloque
Tipo de contenido – Constante – multiparte/mixto; límite=lote_milote
Use el siguiente formato en el cuerpo del mensaje para crear un empleado:
--batch_mybatch
Content-Type: multipart/mixed; boundary=changeset_mychangeset1
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "User(userId='${property.personID}')"
},
${property.userbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "PerPerson(personIdExternal="${property.personID}")"
},
${property.personbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "EmpEmployment(personIdExternal="${property.personID}",userId='${property.personID}')"
},
${property.emplbody},"isContingentWorker":true
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "WorkOrder(effectiveStartDate=datetime'${property.startdate}',userSysId='${property.personID}')"
},
${property.workbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "EmpJob(seqNumber=1,startDate=datetime'${property.startdate}',userId='${property.personID}')"
},
${property.jobbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "PerPersonal(personIdExternal="${property.personID}",startDate=datetime'${property.startdate}')"
},
${property.personalbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "PerNationalId(cardType="MisparZehut",country='ISR',personIdExternal="${property.personID}")"
},
${property.nationalbody},"isPrimary":true
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "PerAddressDEFLT(addressType="home",personIdExternal="${property.personID}",startDate=datetime'${property.startdate}')"
},
${property.addressbody}
}
--changeset_mychangeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST upsert HTTP/1.1
Accept: application/json
Content-Type: application/json
{
"__metadata": {
"uri": "PerPhone(phoneType="${property.optionPT}",personIdExternal="${property.personID}")"
},
${property.phonebody},"isPrimary":true
}
--changeset_mychangeset1--
--batch_mybatch--
Etapa 4 :
Agregue una conexión HTTP para llamar a $batch:
donde será la dirección: https://{datacenterAPIserver}/odata/v2/$batch
La respuesta vendrá como, si fue un éxito y también se puede ver en SF:
--batch_a6a959f5-0d6a-48a6-9d2f-20f9ac746080
Content-Type: multipart/mixed; boundary=changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 150
{
"d" : [
{
"key" : "10010", "status" : "OK", "editStatus" : "INSERTED", "message" : null, "index" : 0, "httpCode" : 201, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 177
{
"d" : [
{
"key" : "PerPerson/personIdExternal=10010", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 208
{
"d" : [
{
"key" : "EmpEmployment/personIdExternal=10138,EmpEmployment/userId=10010", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 229
{
"d" : [
{
"key" : "WorkOrder/effectiveStartDate=2023-02-27T00:00:00.000+01:00,WorkOrder/userSysId=10010", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 559
{
"d" : [
{
"key" : "EmpJob/seqNumber=1,EmpJob/startDate=2023-02-27T00:00:00.000+01:00,EmpJob/userId=10010", "status" : "OK", "editStatus" : "UPSERTED","message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 231
{
"d" : [
{
"key" : "PerPersonal/personIdExternal=10010,PerPersonal/startDate=2023-02-27T00:00:00.000+01:00", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 242
{
"d" : [
{
"key" : "PerNationalId/cardType=MisparZehut,PerNationalId/country=ISR,PerNationalId/personIdExternal=10010", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 272
{
"d" : [
{
"key" : "PerAddressDEFLT/addressType=home,PerAddressDEFLT/personIdExternal=10010,PerAddressDEFLT/startDate=2023-02-27T00:00:00.000+01:00", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
DataServiceVersion: 1.0
successfactors-message: X-SF-Correlation-Id, successfactors-sourcetype are missing in request headers
Content-Length: 201
{
"d" : [
{
"key" : "PerPhone/personIdExternal=10010,PerPhone/phoneType=526", "status" : "OK", "editStatus" : "UPSERTED", "message" : null, "index" : 0, "httpCode" : 200, "inlineResults" : null
}
]
}
--changeset_2e0455d6-79e3-4161-871e-0cb754f8ff76--
--batch_a6a959f5-0d6a-48a6-9d2f-20f9ac746080--
Conclusión :
En esta publicación de blog, sabrá cómo contratar a un empleado utilizando generarNextPersonID y la operación $batch.
Calle Eloy Gonzalo, 27
Madrid, Madrid.
Código Postal 28010
Paseo de la Reforma 26
Colonia Juárez, Cuauhtémoc
Ciudad de México 06600
Real Cariari
Autopista General Cañas,
San José, SJ 40104
Av. Jorge Basadre 349
San Isidro
Lima, LIM 15073