Skip to Content
Author's profile photo Denny Schreber

Dynamic E-mail Determination Using New Features from SAP GRC NFE 10.0 and PI 7.1 Onwards

Overview

In my other article about Migrating GRC NFE B2B PI Mail Interfaces from 1.0 to 10.0 I suggested an approach to migrate your existing B2B mail interfaces. However, with SAP Process Integration 7.1 onwards it is much simpler. It is still relying on the approach described by described by Henrique Pinto in his article Using  SAP PI Lookup API and Dynamic Configuration in SAP GRC NFE Outbound B2B Interface for Dynamic E-mail Determination but you don’t have to do any lookups in PI because the email address can be pre-populated on the GRC NFE system. I don’t go into details for all preparation steps since they are already described in the aforementioned article.

SAP GRC NFE

First, you have to implement the BAdI /XNFE/EMAIL_B2B_OUT on the SAP GRC NFE ABAP system. The screenshot below shows a dummy implementation. This BAdI just writes an email address into an attachment.

/wp-content/uploads/2012/09/abap_0_135131.png

NetWeaver Developer Studio

In the NWDS you have to create a Java Mapping that extracts the email from the additional attachment that you created in the BAdI implementation. The example below is named AttachmentReader.java and it is designed to run as a stand-alone java mapping after the official java mapping from the PI standard content.

package sdn.sap.com.xi.nfe;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Iterator;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.Attachment;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.InputAttachments;
import com.sap.aii.mapping.api.OutputAttachments;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
/**
 * This class is an PI Java Mapping which extracts the email
 * from an additional attachment for Dynamic Configuration
 *
 * @author Denny Schreber
 */
public class AttachmentReader extends AbstractTransformation {
    InputAttachments inputAttachments = null;
    OutputAttachments outputAttachments = null;
    DynamicConfiguration conf = null;
    /**
     * This method is for testing purposes only.
     */
    public static void main(String[] args) throws Exception {
        InputStream in = new FileInputStream("C:/Testdaten/b2b_nfe_new.xml");
        OutputStream out = new FileOutputStream("C:/Testdaten/b2b_mail_out.xml");
        new AttachmentReader().execute(in, out);
        in.close();
        out.flush();
        out.close();
    }
    /**
     * @param tf_in             The incoming message
     * @param tf_out               The transformed message
     * @return                     void
     * @throws StreamTransformationException
     */
    public void transform(TransformationInput tf_in, TransformationOutput tf_out)
            throws StreamTransformationException {
        inputAttachments = tf_in.getInputAttachments();
        outputAttachments = tf_out.getOutputAttachments();
        conf = tf_in.getDynamicConfiguration();
        this.execute(tf_in.getInputPayload().getInputStream(), tf_out
                .getOutputPayload().getOutputStream());
    }
    /**
     * @param in                 The incoming message
     * @param out               The transformed message
     * @return                     void
     * @throws StreamTransformationException
     */
    public void execute(InputStream in, OutputStream out)
            throws StreamTransformationException {
        // getTrace().addInfo("JAVA Mapping Called");
        Attachment attachment;
        String email = "";
        String aid;
        if (inputAttachments.areAttachmentsAvailable()) {
            Collection<String> attachments = inputAttachments.getAllContentIds(true);
            Iterator<String> it = attachments.iterator();
            // assuming only one attachment
            while (it.hasNext()) {
                aid = it.next();
                attachment = inputAttachments.getAttachment(aid);
                email = new String(attachment.getContent());
                outputAttachments.removeAttachment(aid);
            }
            // Fill the dynamic configuration for the “to” field in the mail
            // adapter
            DynamicConfigurationKey key = DynamicConfigurationKey.create(
                    "http://sap.com/xi/XI/System/Mail", "THeaderTO");
            conf.put(key, email);
        }
        // return the stream without doing anything
        int c;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            while ((c = in.read()) != -1)
                baos.write(c);
            out.write(baos.toByteArray());
        } catch (Exception e) {
            throw new StreamTransformationException(e.getMessage());
        }
    }
}

If you have an older Support Package in your PI system take care that SAP Note 1641548 is deployed. Otherwise, the attachment with the email address will not be removed and send with your mail.

However, another (not recommended) option would be to extend the Java Mapping from the standard content. When you use the graphical mapping you can run into namespace issues which will render the NF-e xml invalid.

Enterprise Services Repository

In the Repository, you have to import the Java Mapping under Imported Archives in your own SWCV. Then, copy the Operation Mapping NTB2B_procNFe_TO_procNFe / namespace http://sap.com/xi/NFE/006 from SWCV SAPBO SLL-NFE 10.0  into your SWCV and add the new Java Mapping as the second step after the ProxyNamespaceCleaner. (I’m assuming you have a dependency created between the SWCV). Here, you have to select the “Read Attachments” option in order to allow the Java mapping to access the attachment.

/wp-content/uploads/2012/09/esr_om_1_135132.png

Finally, save and activate all your changes.

Integration Directory

In the Integration Directory you have to change the generated Interface Determination and select your new mapping from your SWCV. Save and activate.

/wp-content/uploads/2012/09/dir_id_2_135133.png

Please take care that your email communication channel has the option “Keep Attachments” under General and “Use Adapter-Specific Message Attributes” under the Advanced tab selected. Furthermore, you have to use the MessageTransformBean to rename the attachment.

/wp-content/uploads/2012/09/dir_cc_3_135137.png

Testing

In the PI monitor, you can see how the additional attachment with the mail address is removed after the mapping.

/wp-content/uploads/2012/09/moni_4_135138.png

In the end, the mail recipient should get an email with the NF-e as attachment called NFe.xml.

Assigned Tags

      17 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Fernando Da Ros
      Fernando Da Ros

      Muito bom Denny.

      Obrigado

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      Thanks Fernando

      Author's profile photo Ricardo Viana
      Ricardo Viana

      Denny,

      I´m getting error, do you have any idea what is that ?

      http://scn.sap.com/thread/3317977B2B Dinâmico - Erro - PI 7.1

      Kind regards,

      Author's profile photo Rafael Vieira
      Rafael Vieira

      Denny, thanks for this great content.

      I was wondering if this solution should work for GRC 10.0 running with PI 7.0.

      We have different options in Interface Mapping (such as the Read Attachment checkbox).

      Tks again!

      Rafael Vieira.

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      Hi Rafael,

      To my knowledge no: PI 7.0 does not have the "new" mapping api which allows to access attachments from Java mappings. That the Interface Mapping has the "Read Attachments" checkbox is also new to me, which SP of PI 7.0 are you using?

      Cheers,

      Denny Schreber

      Author's profile photo Rafael Vieira
      Rafael Vieira

      PI 7.01, SP06.

      Some people say we cannot use the previous solution proposed by NFe team (by using the RFC lookup) but this Javamapping, as you said, wouldn't work on PI 7.01. Not sure, now, how to proceed.

      Tks,

      Rafael Vieira.

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      I think the approach I described in http://scn.sap.com/docs/DOC-31215 works well with an RFC lookup. You can follow that one on your PI release. The new approach just makes it easier because you don't need the lookup anymore.

      Cheers,

      Denny Schreber

      Author's profile photo Former Member
      Former Member

      Hello Rafael, I am currently working on upgrading Nfe from 1.0 to 10.0. We are using PI 7.02 EHP 11.  We enabled the BADI to get the email as a additional attachment, but we are not able to access it in UDF. Is there any other way to  read the additional attachment, the check box you are talking about, where can I find that. 

      Please help

      Regards,

      Bhasker Appala

      Author's profile photo Rafael Vieira
      Rafael Vieira

      Hello Bhasker,

      I've seen guys configuring the dynamic email determination by using the BADI, just like you did.

      I'm using the RFC lookup option and it's working fine.

      Let me know if you need any further details regarding the solution I'm using.

      Best regards,

      Rafael Vieira.

      Author's profile photo Former Member
      Former Member

      Denny,

      Thank you for the wiki, was really excellent.

      After doing this implementation, the e-mail always arrives as an attachment to email. That is, an e-mail arrives, you have another email attached and inside the annex is XML.

      I do not know what's going on, this could be the problem of the e-mail exchange?

      Helped others posting the wiki, but if not asking too much, please help me again.

      Thank you.

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      Hi, Sorry, did not see this happening yet. Can you tweak your mail server?

      Cheers,

      Denny Schreber

      Author's profile photo Krish Gopalan
      Krish Gopalan

      Denny,

      How would you handle the issue if the email address has not been maintained (for example, business simply forgot to maintain the email address). Do you raise the error message in PI or BADI?

      Also, is there a way to identify the "event" (this BADI is called during NFE and also CC-e events).. Is it possible to identify whether it is the original NFE or CC-e event?

      Krish

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      Hi Krish,

      That depends what approach is better suited for your environment but how about using a default mail that someone would receive in the error case and can take appropriate action on it.

      Cheers,

      Denny Schreber

      Author's profile photo Former Member
      Former Member

      Hi Denny,

      I have a few questions:

      Is it possible to use this solution for the CTB2B scenario?

      I meant, is the BADI  /XNFE/EMAIL_B2B_OUT also accessed when we have a Cancel NF-e Out? Is it necessary to make any changes in the java mapping source code?

      Thanks in advance,

      Daniela

      Author's profile photo Denny Schreber
      Denny Schreber
      Blog Post Author

      Hi Daniela,

      Yes, it works for CTB2B and ETB2B as well. Remember that the Cancellation web service will be decommissioned soon and replaced with the Cancellation Event.

      Cheers,

      Denny Schreber

      Author's profile photo Former Member
      Former Member

      Hi Denny.

      I have follow all steps above but I am facing with an error.

      I created a thread asking for help:

      https://scn.sap.com/thread/3874249

      Thanks,

      Renan.

      Author's profile photo Former Member
      Former Member

      Hello Denny.

      I´m facing with an error when trying to send an email to BP.

      I have created a thread with more information about my issue.

      B2B OutBound Error - Nfe 3.10

      Can you help me please?

      Thanks,

      Renan.