0

I am trying to post XML data to an external service using an endpoint WSDL. I have set up a Named Credential in Salesforce with authentication. When I run the code below I get the following error:

<faultstring>The endpoint reference (EPR) for the Operation not found is [MY ENDPOINT] and the WSA Action = null</faultstring>

I also have the below test which fails:

System.assert(header != null);

Here is the code I'm running using Execute Anonymous:

String xmlstring = '';
//GENERATE XML HERE
DOM.Document doc = new DOM.Document();
String soapNS = 'http://schemas.xmlsoap.org/soap/envelope/';
String xsi = 'http://www.w3.org/2001/XMLSchema-instance';

dom.XmlNode envelope = doc.createRootElement('Envelope', soapNS, 'soapenv');
envelope.setNamespace('xsi', xsi);
envelope.setAttributeNS('schemaLocation', soapNS, xsi, null);
dom.XmlNode header = envelope.getChildElement('Header', soapNS);

dom.XmlNode body = envelope.addChildElement('Body', soapNS, null);

dom.XmlNode products = body.addChildElement('products', null, null);
dom.XmlNode body1= products.addChildElement('product', null, null);

xmlstring = doc.toXmlString();

system.debug(xmlstring);

HttpRequest req = new HttpRequest();
//call out to Vendavo (this doesn't work)
req.setEndpoint('callout:Vendavo_Integration');
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setBody(xmlstring);
Http http = new Http();
HTTPResponse res = http.send(req);
System.assert(header != null);

if (res.getStatusCode() < 200 || res.getStatusCode() > 300) {
    String fullBody = '';
    for (Integer i = 0; i < res.getBody().length(); i=i+300) {
        Integer iEffectiveEnd = (i+300 > (res.getBody().length()-1) ? res.getBody().length()-1 : i+300);
        system.debug(res.getBody().substring(i,iEffectiveEnd));
    }

    System.debug('The status code returned was not expected: ' +
                 res.getStatusCode() + ' ' + res.getStatus());
} else {
    System.debug(res.getStatusCode());
    System.debug('body: ' + res.getBody());
}

I have tried running the full code from the example here using my endpoint but still encounter the same issues. I don't know how to determine if I am setting the header wrong or if there is an issue with the endpoint I have been given.

Thanks for any help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.