Skip to end of metadata
Go to start of metadata

Per default, PI messages include just one single XML message (the main payload).
When integrating such messages through some communication channel, for example the Mail Receiver adapter, the XML message can be either sent as an attachment or in the Mail body.

While sending the NFe XML file as an attachment is the most desirable approach, several times it may also be desirable to include some custom predefined message in the Body of the e-mail that will be sent to the customers.

The code below is a proposal for an Adapter Module that would:

  1. create the XML message as attachment in the email;
  2. set the XML file name with a specific value (instead of "Untitled.xml");
  3. set the Body of the e-mail message with some pre-defined message for the customers.

Notice that the Adapter Module reads data from two inputs:

  • the Message Module parameter (to be included in the Module Processor tab of the communication channel)
  • one Adapter Specific Message Attribute with the File Name to be set to the attachment. Here, it was used an unexisting parameter ("http://www.sap.com/grc/nfe", "Name"), which was previously filled in the Message Mapping with a dynamic value for the File name (based on the NFe Access Key, for example).

As a debug feature, the Module will also add NFe specific entries to the communication channel Audit Log (which can be seen through the Communication Channel Monitoring or Message Monitoring tools of the Runtime Workbench of SAP PI).

SetAttachmentName.java
package com.sap.sdn.nfe;
import com.sap.aii.af.mp.module.*;
import com.sap.aii.af.ra.ms.api.*;
import com.sap.aii.af.service.auditlog.*;
import java.io.IOException;
import java.util.Iterator;
import javax.ejb.*;

public class SetAttachmentName
	implements SessionBean, Module
{

	public SetAttachmentName()
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate()
	{
	}

	public void ejbPassivate()
	{
	}

	public void setSessionContext(SessionContext context)
	{
		myContext = context;
	}

	public void ejbCreate()
		throws CreateException
	{
	}

	public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
		throws ModuleException
	{
		Message msg = (Message)inputModuleData.getPrincipalData();
		if(msg.getMessageDirection() == MessageDirection.INBOUND)
			amk = new AuditMessageKey(msg.getMessageId(), AuditDirection.INBOUND);
		else
			amk = new AuditMessageKey(msg.getMessageId(), AuditDirection.OUTBOUND);
		Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Entering on the module...");
		Iterator iterator = msg.getAttachmentIterator();
		Payload nfe = (Payload)iterator.next();
		Payload text = msg.createPayload();
		Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Retrieving E-mail details...");
		String FileName= msg.getMessageProperty("http://www.sap.com/grc/nfe", "Name");
		String message = moduleContext.getContextData("Message");
		try
		{
			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Setting Attachment Content Disposition...");
			if (FileName == null)
				nfe.setContentType("application/xml;charset = \"UTF-8\";filename=\"procNfe.xml\"");
			else
				nfe.setContentType("application/xml;charset = \"UTF-8\";filename=\"" + FileName + "\"");

			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: " + FileName);
			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Setting E-mail content...");
			text.setContent(message.getBytes("UTF-8"));
			text.setContentType("multipart/alternative");
			Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Updating E-mail content...");
			msg.addAttachment(text);
		}
		catch(InvalidParamException e)
		{
			Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, "NFe: Error setting E-mail details...");
			throw new ModuleException("NFe: Error setting E-mail details...", e);
		}
		catch(IOException e)
		{
			Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, "NFe: Error setting E-mail content...");
			throw new ModuleException("NFe: Error setting E-mail content...", e);
		}
		catch(PayloadFormatException e)
		{
			Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, "NFe: Error updating E-mail details...");
			throw new ModuleException("NFe: Error updating E-mail details...", e);
		}
		Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Updating the message...");
		inputModuleData.setPrincipalData(msg);
		Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "NFe: Exiting the module with success...");
		return inputModuleData;
	}

	private AuditMessageKey amk;
	private SessionContext myContext;
}
  • No labels

1 Comment

  1. Unknown User (uple9np)

    Thanks for the module code -  great help.

    I generated the ear file and uploaded using SDM. I checked rwb and found the issue I gave the wrong path  -- insead of localejbs/SetAttachmentName

    Now email is perfect. - thank youYou need to check the audit log in the message monitoring (runtime workbench).it will probably show the exception.The message parameter is a fixed text that would go in the mail body (e.g. "NF-e from Company XXX").If you want to make it dynamic, you shouldn't use the module parameter, but like another dynamic configuration. Best,Henrique.