Skip to content

Instantly share code, notes, and snippets.

@js1972
Created August 25, 2014 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save js1972/5cfa2dff3f0b81fdea95 to your computer and use it in GitHub Desktop.
Save js1972/5cfa2dff3f0b81fdea95 to your computer and use it in GitHub Desktop.
Example PI UDF showing how to add an attachment to a message. In this case its sent to the MAIL adapter so is a text file attachment to the email. UDF function imports: public void setAttachment(String[] OrgFormatName, String[] StreetName, String[] HouseId, String[] Email, String[] RegionName, String[] CityName, String[] CountryName, String[] St…
/* This UDF reads all contents and creates an output along with an attachment to SAP PI message, which has data in CSV foramt as expected by DB Schenker.
Please note that DB Schenker expect some of the fields as empty and hence only commas are added in the payload. For future enhancements if further fields are required to be added, pass them as input to UDF and update the
content field to use that input
*/
AbstractTrace trace = container.getTrace();
String firstRow = "Organization Name,,Contact Name,Email Address,Phone Number,Handling Office,Trucker,US Principal Party of Interest / ISF Importer,Factory,Consignee/Destination/ISF Ship To,Freight Forwarder,Third Party,Equipment Stuffing Location,Secondary Name/3rd Party Description,Address (Line 1),Address (Line 2),Address (Line 3),Email Address,State,City,Country (ISO Country Code),Postal Code,Phone,Fax,Internal Customer Account #,Importer of Record Number,Type (EIN, SSN, Foreign),Consignee #,Type (EIN, SSN, Foreign),Vendor Name,Vendor Code,Contact Name";
String content = firstRow + "\n";
String mimeType= "text/comma-separated-values;charset="+ "\"" + "uft-8" + "\"";
String filename = "Attachment";
// Add values as comma-separated format
for(int i =0; i< OrgFormatName.length; i = i +2)
{
//Check if Vendor details passed are intended for DB Schenker --> RoleCode = "OA"
if(PartnerRoleCode[i].equals("OA"))
{
content = content + OrgFormatName[i] + ",,,,,,,,,,,,,," + StreetName[i] +"," +HouseId[i] + ",,"+ Email[i] + "," + RegionName[i] + "," + CityName[i] + "," + CountryName[i] + "," + StreetPostalCode[i] + "," + PhoneNum[i] + "," + FaxNum[i] + ",,,,,,,," + "\n";
}
}
// This row has been added for unit testing to ensure fields are populated correctly in CSV format
trace.addInfo(content);
//Create an attachment and move the content as attachment with CSV data
try
{
GlobalContainer globalContainer = container.getGlobalContainer();
OutputAttachments outputAttachments = globalContainer.getOutputAttachments();
Attachment attachments = outputAttachments.create(filename, mimeType, content.getBytes());
outputAttachments.setAttachment(attachments);
result.addValue(filename);
} catch (Exception e)
{
result.addValue(e.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment