[SalesForce] Issue with passing header data on callouts using external wsdl

We have an external wsdl which we use to make a callout to an external application. The wsdl has a header schema which needs to be passed along with the request. I imported the wsdl and the wsdl2apex class stub has all the request and response classes except the header class. You will notice gwheaders-100 class is included in the stub but it is not added on the request. So the call out fails with header data not found exception. Any idea on how i can pass the header data to the call out please?
Thanks
Buyan

Best Answer

The soap header should be added as a member to the wsdl2apex generated stub class that corresponds to the port. This class will also have the public String endpoint_c = '...'; member.

There should also be a String member generated that ends with _hns. This has the namespace mapping for the header.

I assume you defined a value for the header member in the stub class?

Alternatively, there is also support for HTTP Headers via the inputHttpHeaders_x Map. See HTTP Header Support:

docSample.DocSamplePort stub = new docSample.DocSamplePort();
stub.inputHttpHeaders_x = new Map<String, String>();

//Setting a custom HTTP header
stub.inputHttpHeaders_x.put('myHeader', 'myValue');

When I generated the Apex classes from your WSDL the header was called ServiceName and had the type gwHeaders100.Service_element.

public class ingdirectComWsdlLoanCreateleadWsdl {
    public class loan_CreateLead_HTTPPort {
        public String endpoint_x = 'http://localhost:8080/wesp-dgw/';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        // ... snip ...
        public gwHeaders100.Service_element ServiceName; // <---- This is the input soap:header
        private String ServiceName_hns = 'Service=urn:gw-headers-100';
        private String[] ns_map_type_info = new String[]{'urn:gw-headers-100', 'gwHeaders100', 'urn:gw-message-100', 'gwMessage100', 'urn:gw-message-200', 'gwMessage200', 'http://ingdirect.com/wsdl/loan_CreateLead.wsdl', 'ingdirectComWsdlLoanCreateleadWsdl', 'urn:gw-ingdirect-services-lendingservices-contracts-common-100', 'gwIngdirectServicesLendingservicesCo', 'urn:gw-loanserviceenvelope-100', 'gwLoanserviceenvelope100'};
        // ... snip ...
    }
}