This blog explains how we can upsert attachment to SAP SuccessFactors custom MDF using CPI.
I had a requirement where I must fetch the Background verification report from the third-party API and Upserting it to custom MDF created for background verification in SuccessFactors.
We cannot UPSERT attachments directly to Custom MDF attachment field, it will be a two-way process:
- First, we will have to fill the attachment table using Attachment entity. In response we will receive attachmentId which will be later used while Upserting attachment to main entity.
- Second, we will use main custom entity to upsert attachment into custom MDF.
In this blog I will use custom MDF named “cust_BackgroundVerificationBGV”. Custom field is configured “cust_BGVReportNav” with data type Attachment and we can navigate to Attachment API from this field.
- Encoding Incoming Attachment from 3rd party API/SFTP to Base64
SuccessFactors only accepts Base64- encoded data, so first we will convert incoming message to Base-64 if it is in some other format.
My Incoming message:
Add base64 encoder step to convert the above data to base64 encoded data:
Your data will then converted into below format:
- Filling Attachment table with attachment fields:
Add Content-Modifier in next step with below body created for upserting attachment to SuccessFactors system:
Code:
<Attachment>
<Attachment>
<fileContent>${in.body}</fileContent>
<fileName>BgvReport.pdf</fileName>
<module>GENERIC_OBJECT</module>
<userId>EC_ADMIN</userId>
<viewable>true</viewable>
<deletable>false</deletable>
</Attachment>
</Attachment>
Attachment – Entity using which we’re inserting attachment to SuccessFactors system.
fileContent – This is the encoded base64 attachment that we have received from previous step. ${in.body} is used to insert the body(attachment) to this field.
filename – File name which will appear in MDF after attachment is success fully inserted. Keep it as per your requirement.
module – Set it as GENERIC_OBJECT.
userId – This is the userId of the API user upserting the attachment to the system.
Note- Do not use userId of the user in which you want to upsert the attachment, userId of the user which is stored in your SuccessFactors Credentials shall be used. Example – API_ADMIN, API_USER etc.
viewable – Set this field as true.
Deletable – Set this field as false.
- Add SuccessFactors adapter to UPSERT attachment into system:
You will receive the below response from SuccessFactors after Upsert call(add logger to store response):
Store attachment Id received from response; this will be used later while upserting attachment to custom MDF.
- Add content-modifier step to prepare body for Upserting Attachment into main entity:
First add Message Header to pass Content-Type as application/json:
Then add message body in same content-modifier:
Code:
{
"__metadata": {
"uri": "cust_BackgroundVerificationBGV"
},
"effectiveStartDate": "/Date(1682088699000)/",
"externalCode": “123",
"cust_BGVReportNav": {
"__metadata": {
"uri": "Attachment(attachmentId=2012)"
}
}
}
Here,
1. {
"__metadata": {
"uri": "cust_BackgroundVerificationBGV"
},
Here first we have used the main entity.
2. "effectiveStartDate": "/Date(1682088699000)/", - This is primary key for custom MDF. Its value must be provided in ticks’ format.
Find below Groovy for converting EPHOC format from SuccessFactors (yyyy-MM-ddThh:mm:ss.00Z) to above milliseconds format (“/ Date(ticks)/”)
Import com.sap.gateway.ip.core.customdev.util.Message
Import java.util.HashMap
Import java.util.*;
Import java.text.DateFormat;
Import java.text.SimpleDateFormat;
Import java.util.Date;
Import static java.util.Calender.*;
def Message processData(Message message) {
def property = message.getProperties();
def String SDate = property.get(“effectiveStartDate”);
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd);
Date date = df.parse(SDate);
Long epoch = date.getTime();
String convertedTime = Long.toString(epoch);
convertedTime = “/Date(“ + convertefTime +”)/”;
message.setProperty(“convertedTime”, convertedTime) ;
return message
}
NOTE : Here incoming date was stored previously as property in content-modifier(effectiveStartDate) which is then called in Groovy script, and new property is created(convertedTime) which can then be used in payload above to pass expected date to the payload.
3."externalCode": “123”, - UserId of the employee in which you want to upsert the attachment.
4."cust_BGVReportNav": {
"__metadata": {
"uri": "Attachment(attachmentId=2012)"
- Attachment Id received from response which we have stored in previous step.
- Last step is to Upsert Attachment to Custom MDF:
We will use HTTP adapter to upsert attachment to custom MDF. Here we will not SuccessFactors adapter.
In address, put the (API URL of your SuccessFactors/upsert)
Note that credentials must be separately stored in Keystore for making upsert call using HTTP adapter in (username = User@CompanyId) format.
- Deploy your Integration you will receive below Response from SuccessFactors
Now attachment is finally uploaded to the custom MDF:
Hope this blog helps!
Best Regards,
Megha