Skip to Content
Technical Articles
Author's profile photo Shruthi Sundaram

Pulling one entity data based on a field of another entity

Introduction

Sometimes it becomes very difficult to combine unrelated entities (e.g: PerPerson and Background_Education) pulled from successfactors in CPI, especially when they have no field name in common. And it also becomes difficult when you want data from a particular entity only for those employees retrieved in the previous entity

This blog post explains about pulling data from one entity based on a field of another entity (e.g: personIDexternal) using the Successfactors OData V2 adapter in CPI. Where sometimes the same data is stored under different names in different entities.

For e.g: In Background_Education it is stored as userID whereas in PerPerson it is stored as personIDexternal

Hope you enjoy reading!!

Scenario

Here we are pulling data from Background_Education based on the employees pulled from PerPerson entity

Step 1:

Pull the required data from PerPerson entity based on the filters according to the requirement

Step 2:

Store the payload content in the content modifier.

Step 3:

Remove the namespaces of the input XML payload through groovy script.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.regex.*;
import java.util.HashMap;
import java.util.*;

def Message processData(Message message) {
	//Body
	   def body = message.getBody(java.lang.String) ;
	  //Properties
	def bodyNice = new XmlSlurper().parseText(body) ;
	   map = message.getProperties();
def messageLog = messageLogFactory.getMessageLog(message);

def input_xml = body.replaceAll("><",">\n<");
def wordStartsWith = ~/<multimap.*>/
def wordEndsWith = ~/<\/multimap.*>/
def wordStartsWithxml = ~/<\?xml.*\?>/

def wordStartswithNode1 = ~/<ns0:Message1.*>/
def wordEndswithNode1 = ~/<\/ns0:Message1.*>/
def wordStartswithNode2 = ~/<ns0:Messages.*>/
def wordEndswithNode2 = ~/<\/ns0:Messages.*>/

input_xml = input_xml.replaceAll(wordStartsWith,"");
input_xml = input_xml.replaceAll(wordEndsWith,"");
input_xml =  input_xml.replaceAll(wordStartsWithxml,"");
input_xml = input_xml.replaceAll("(?m)^[ \t]*\r?\n", "");
input_xml = input_xml.replaceAll(">\n</","></");
input_xml = input_xml.replaceAll(wordStartswithNode1,"");
input_xml = input_xml.replaceAll(wordEndswithNode1,"");
input_xml = input_xml.replaceAll(wordStartswithNode2,"");
input_xml = input_xml.replaceAll(wordEndswithNode2,"");

message.setBody(input_xml);
return message;
}

Step 4:

Use a General Splitter to split the incoming payload into groups of 100. Because otherwise the IFlow fails in case huge amounts of input data

Step 5:

Use a message mapping to perform one-to-one mapping of all fields and write a UDF to save the personIDexternal into a list.

 

import com.sap.it.api.mapping.*;

def void SetEmpList(String[] input,Output output,MappingContext context) {
    
    def list = []

        for(int i=0;i<input.length;i++)
        {
          list.add("'"+input[i]+"'")
          output.addValue(input[i])
        }
        
    context.setProperty("EmpList",list)
    
}


      

Step 6:

Now build the query to be sent to the OData V2 adapter through BuildQuery (groovy script function)

It takes the employeeIDs from EmpList and builds a whereClause property

package com.test.groovy
import com.sap.gateway.ip.core.customdev.util.Message;


def Message processData(Message message)
{

	
		def pMap = message.getProperties();
		def EmpList = pMap.get("EmpList");
	    def whereString = new StringBuffer()
	   
	
	   for(int i=0;i<EmpList.size();i++)
	   {
	       if(i<EmpList.size()-1)
	       {
	         whereString.append("userId eq ")
		     whereString.append(EmpList.get(i))
		     whereString.append(" or ")
	       }
	       
	       if(i>=EmpList.size()-1)
	       {
	         whereString.append("userId eq ")
		     whereString.append(EmpList.get(i))
	       }
		
	   }
      message.setProperty("whereClause",whereString.toString()); 
		
	return message;
}

Step 7:

Use the whereClause property to retrieve data from Background_Education entity

$filter=${property.whereClause}.

Step 8:

Gather all the groups of employees into one payload

Step 9:

Combine both the XMLs after saving the Background_Education payload in one property in Content Modifier and removing the namespace for the same

Conclusion:

Now you can use this combined payload as per your requirement.

This can also be used in case you want to pull data from any two sets of unrelated entities. You would just need to figure out the field which you want to consider and save them in the list.

Hope this helps!!

 

Thanks and Regards

Shruthi Sundaram

 

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.