Monday, 9 November 2015

Triggering Email using UDF in SAP PI message Mapping based on some incoming payload

We can trigger an email using through our message mapping using Java's standard API and notify if certain conditions are met or not met based on the requirements. We can use the same functionality in JAVA mapping to trigger an email with the help of Operation mapping.
In this post I will try to cover the triggering of an email through UDF.


Prerequisites:
We need to use import the jar file as imported archive in ESR(This jar contains standard Java Mail API).
 You can download the zip from the below link. You need to  import it as Imported Archive in ESR.


https://sites.google.com/site/learnsappiblogspotcom/a-rapid-journey-to-sap-xi-pi/mail.zip?attredirects=0&d=1


We require the following parameters to trigger an email.
  1. Sender host
  2. Recipient List
  3. Subject
  4. Message
  5. Sender
After importing this zip file go through below steps.


Step1: After successful import of archive. Create a UDF sendEmail in message mapping.
(Suppose our UDF triggers email when Header Text is "Final".)  
Note: you can create your own logic based on the requirement.


Step2:  Assign your archive in tab Archive used.





Step3: import the package as below.





Step4: Now we need to find SMTP host and simply provide details.
For SMTP host we need to follow below steps:
Goto tcode SCOT in PI system and click on SMTP.
Then you will get below popup



Copy the mailhost from the above path.


Step4:  Write the udf( Below udf is written if incoming field is "Final". The mail will be send if input to udf is Final else mail will not be triggered.)





This will be helpful if you are sending email on the basis of some incoming payload in Message.


Note: Email will be triggered every time when we execute either message mapping or Operation mapping.







Friday, 23 October 2015

Creating Java Mapping for handling ampersand / & in SAP PI

As we all know SAP PI is rapidly moving to Single Stack (i.e. JAVA Stack) hence we should also be capable of handling Simple Java Codes to write UDFs and Java Mappings.


We can create a Java Mapping for our custom requirement either using SAX or DOM Parser ( I will elaborate in my next blog). We will write a simple program which will handle '&' coming in source payload. As PI can't handle special characters of XML language.
Below steps will help you to create your first java mapping.


Steps for writing JAVA mapping :


Prerequisites: We need below Jar file to be imported in our Java Project for writing a mapping.

TO Download the jar please follow the below link:


https://sites.google.com/site/learnsappiblogspotcom/a-rapid-journey-to-sap-xi-pi/com.sap.xpi.ib.mapping.lib.zip?attredirects=0&d=1


Below are the step need to be followed while creating the JAVA mapping.
Download and install the eclipse (its freeware).


Step1: Create a JAVA Project in Eclipse



Note: If you are using PI version 7.30 then select JRE as J2SE-1.5 from dropdown list.





Step2: Create a package com.test





Step3: Add External mapping jar downloaded from above link.
Right Click on Project --> Build Path --> Configure Build Path
Goto  Libraries tab and Click on Add External Jar





Then browse your jar location and add the Jar to library. Then you can see the jar in your library.



Step4: Now create a class file name as Handling_Ampersand in package com.test  as below






Step5 : Now write the below code in your class.



package com.test;







import java.io.InputStream;


import java.io.OutputStream;







import com.sap.aii.mapping.api.AbstractTransformation;


import com.sap.aii.mapping.api.StreamTransformationException;


import com.sap.aii.mapping.api.TransformationInput;


import com.sap.aii.mapping.api.TransformationOutput;


public class Handling_Ampersand extends AbstractTransformation





{

public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)

throws StreamTransformationException





{

try



{



InputStream inputstream = transformationInput.getInputPayload().getInputStream();



OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();




byte[] b = new byte[inputstream.available()];





inputstream.read(b);

String inputContent = new String(b);

inputContent = inputContent.replaceAll("&", "&;");





outputstream.write(inputContent);




} catch (Exception exception) {





getTrace().addDebugMessage(exception.getMessage());

throw new StreamTransformationException(exception.toString());





}



}








}




Step 6: Now create a jar for this through below steps.
Right click on your Project -> Export -> JAVA (Expand the tree) -> JAR file



Click on Next.


Step 7: Then browse the path to save the jar and select the below checkboxes and click Finish button.



Step 8: After this we can use this jar as external Archive in our SAP PI ESR in Operation mapping as JAVA class. This will handle the ampersand coming in our payload.