Filter Record Using UDF
Posted on Apr. 22, 2010 at 09.50 PM - Chennai, India
Scenario
Some receiver systems require similar data from the sender system. One typical example is SAP HR master data, which may need to be sent over to several systems. One receiver just requires new employees data, another receiver may requires both new and existing employees, while another may just requires retired or terminated employees.
Solution
To improve the performance, we just create one ABAP program to collect all the required data. once the message is received by SAP PI, it filter the records (of a message) and pass them to the respective receivers. So, how do we use SAP PI mapping to filter the records? It's quite simple actually.
Source XML
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Employee_Data xmlns:ns0="urn:sap.com">
<Emp_Detail>
<investmentComp>ELLI</investmentComp>
<empName>LIM</empName>
<emprBonusAmt>1</emprBonusAmt>
</Emp_Detail>
<Emp_Detail>
<investmentComp>ELLA</investmentComp>
<empName>JAY</empName>
<emprBonusAmt>2</emprBonusAmt>
</Emp_Detail>
<Emp_Detail>
<investmentComp>ELLI</investmentComp>
<empName>VINOD</empName>
<emprBonusAmt>3</emprBonusAmt>
</Emp_Detail>
</ns0:Employee_Data>
Target XML
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Employee_File xmlns:ns0="urn:sap.com">
<DetailRecord>
<empName>LIM</empName>
<emprBonusAmt>1</emprBonusAmt>
</DetailRecord>
<DetailRecord>
<empName>VINOD</empName>
<emprBonusAmt>3</emprBonusAmt>
</DetailRecord>
<TrailerRecord>
<RecordCount>2</RecordCount>
<emprBonusAmt>4.0</emprBonusAmt>
</TrailerRecord>
</ns0:Employee_File>
In this example, we just want to collect data where investment company equals to ELLI. How do we do that?
1. empName Mapping
for (int i=0; i<invComp.length; i++) {
if (invComp[i].equals("ELLI"))
result.addValue(var[i]);
}
2. recordCount Mapping
int count = (int)0;
for (int i=0; i<invComp.length; i++) {
if (invComp[i].equals("ELLI"))
count ++;
}
result.addValue(Integer.toString(count));
3. emprBonusAmt Mapping
float amt=(float)0.;
for (int i=0; i<invComp.length; i++) {
if (invComp[i].equals("ELLI"))
amt += Float.parseFloat(var[i]);
}
result.addValue(Float.toString(amt));
Life is beautiful! Let's make it meaningful and colorful!