[SalesForce] salesforce passing value in soap header

I having an issue passing a URL value into the header.

I have added this code:

CustomerFieldscheduling.CFSPartnerSlotsServiceImplPort cfsPort = new CustomerFieldscheduling.CFSPartnerSlotsServiceImplPort();
        
        Blob headerValue = Blob.valueOf('LOGIN' + ':' + 'LOGINPWD');
        String authorizationHeader = 'Basic ' +EncodingUtil.base64Encode(headerValue);
            String sURL = 'https:/testURL';
            System.debug('***authorizationHeader***'+authorizationHeader);
            cfsPort.inputHttpHeaders_x = new Map<String,String>();
            cfsPort.inputHttpHeaders_x.put('URL',sURL);        
            cfsPort.inputHttpHeaders_x.put('Authorization',authorizationHeader);

When i tried to invoked the webservice, able to connect to the operation that i need to call but getting the error of the URL not being part of the request that i sent

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header/>
</env:Header><env:Body>....</env:Body>

the web service is expecting a request in this format

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<URL>https:/testURL</URL>
</env:Header><env:Body>

Can you please share idea on how i could attain the said format.

Best Answer

inputHttpHeaders_x is used to control the HTTP Headers. These are typically things like the Authorization header or a Cookie. These exist outside the SOAP envelope. Docs for HTTP Header Support.

In contrast, your example shows the URL defined within the env:Header SOAP envelope header. That is something different from the HTTP headers. These types of headers are typically defined by the WSDL and then replicated into the generated Apex code.

E.g. Take the LoginScopeHeader from the Partner and Enterprise APIs.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">
  <soapenv:Header>
    <urn:LoginScopeHeader>
      <urn:organizationId>[mention the salesforce Org Id]</urn:organizationId>
    </urn:LoginScopeHeader>
  </soapenv:Header>
  <soapenv:Body>
    <!-- ... -->
  </soapenv:Body>
</soapenv:Envelope>

This appears in the generated Apex class as:

public class LoginScopeHeader_element {
        public String organizationId;
        public String portalId;
        private String[] organizationId_type_info = new String[]{'organizationId','urn:partner.soap.sforce.com','ID','1','1','false'};
        private String[] portalId_type_info = new String[]{'portalId','urn:partner.soap.sforce.com','ID','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'urn:partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'organizationId','portalId'};
}

It then appears as a member in the proxy class (the same class that has the inputHttpHeaders_x member).

public partnerSoapSforceCom.LoginScopeHeader_element LoginScopeHeader;
private String LoginScopeHeader_hns = 'LoginScopeHeader=urn:partner.soap.sforce.com';

So check your WSDL and corresponding Apex classes to see if the required property is already there.

Related Topic