Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

In my current project, client requirement is to integrate SFDC (salesforce.com online CRM) with ECC using PI 7.1. SFDC provides an enterprise WSDL that can be used to query/perform DML operations on SFDC objects using web services. When using this Enterprise WSDL, we found that SFDC expects few attributes to be set explicitly in the SOAP:Header of the SOAP message coming out of the PI SOAP Receiver CC. The WSDL would expect a sessionID and server URL in the SOAP header. In order to achieve this, we built the SOAP envelope explicitly using Java Mapping and checked "Do Not use SOAP Envelope" option in SOAP Receiver communication channel. This SOAP envelope can also be built using XSLT mapping, but I am explaining only the Java Mapping approach below.
What the below code does:
1) In the execute () method of the Java Mapping, we call the login web service
to get the sessionId
2) Now, add the SOAP envelope to the incoming copypayload(inputstream)

{color:#111111} {color} import com.sap.aii.mapping.api.*;
import java.io.*;
import java.util.Map;
import com.sap.aii.mapping.lookup.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

public class SoapEnvelopeForSFDC implements StreamTransformation
{
String sessionId ="";
String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:enterprise.soap.sforce.com\">"+
"<soapenv:Header> "+
"<urn:SessionHeader>"+
"<urn:sessionId> ";
String suffix = "</urn:sessionId> </urn:SessionHeader> </soapenv:Header> <soapenv:Body>";
String envelope = "</soapenv:Body> </soapenv:Envelope>";

private Map map;
/* method setParamters is required, but we do not anything with it*/
public void setParameter(Map param)
{
map = param;
}
public void execute(InputStream in, OutputStream out)
{
try
{
getSessionIdFromSFDC("username", "password"); //Supply the SFDC developer account username and password.
copyPayload(in,out);
}
catch (Exception t)
{
t.printStackTrace();
}
}
/* method is to build the Soap envelope for the input message payload */
private void copyPayload(InputStream in, OutputStream out) throws streamTransformationException
{
String c = "";
String Newpayload = "";
try
{
InputStreamReader inr = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inr);
/* The following is to remove the <?xml version=\"1.0\" encoding=\"UTF-8\"?> from the input message payload */
String temp = "";
while((temp = reader.readLine()) != null)
{
c = c + temp;
}
int len = c.indexOf(">");
Newpayload = c.substring(len+1);
/* End of remove xml version tag */
out.write(prefix.getBytes());
out.write(sessionId.getBytes());
out.write(suffix.getBytes());
out.write(Newpayload.getBytes());
out.write(envelope.getBytes());
}
catch (IOException e)
{
throw new StreamTransformationException(e.getMessage());
}
}

/* method is to login to SFDC with the username and password to get the sessionId from the response*/
private void getSessionIdFromSFDC(String username, String password)
{
try
{
Channel channel = LookupService.getChannel("BC_SFDC","CC_Login");
SystemAccessor accessor = null;
accessor = LookupService.getSystemAccessor(channel);
String loginxml = "<login xmlns=\"urn:enterprise.soap.sforce.com\"> <username>"
+ username
+ "</username> <password>"
+ password
+ "</password> </login>";
InputStream inputStream = new ByteArrayInputStream(loginxml.getBytes());
Payload payload = LookupService.getXmlPayload(inputStream);
Payload SOAPOutPayload = null;
SOAPOutPayload = accessor.call(payload);
InputStream inp = SOAPOutPayload.getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inp);
NodeList sessionId1 = document.getElementsByTagName("sessionId");
Node node = sessionId1.item(0);
if (node != null)
{
node = node.getFirstChild();
if (node != null)
{
sessionId = node.getNodeValue();
}
}
}
catch(Exception e)
{e.printStackTrace();}
}
public static void main (String[] args) throws Exception
{
try
{
SoapEnvelopeForSFDC obj = new SoapEnvelopeForSFDC();
FileInputStream in = new FileInputStream("/D:/input.xml");
FileOutputStream out = new FileOutputStream ("D:/Output.xml");
obj.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}