2

I have a WSDL file which can't be imported in Salesforce, so I am currently using the Dom.Document to write my own soap envelope to connect to a webservice.

In my soap envelope I have to set credentials in the soap header:

<soapenv:Header>
  <soap:HeaderLogin>
     <soap:username>?</soap:username>
     <soap:password>?</soap:password>
  </soap:HeaderLogin>
</soapenv:Header>

Preferably I would like to use a NamedCredential to setup authentication for this webservice. But how can I pass this information from the NamedCredential to the soap header?

1 Answer 1

3

In your Named Credential configuration, make sure you specify "Allow Merge Fields in HTTP Body", which will allow you to use the merge fields from your Named Credential. Then, use the $Credential global variables to specify that you want to use the username and password from the Named Credential:

<soapenv:Header>
  <soap:HeaderLogin>
     <soap:username>{!$Credential.UserName}</soap:username>
     <soap:password>{!$Credential.Password}</soap:password>
  </soap:HeaderLogin>
</soapenv:Header>

You may also need to specify HTMLENCODE for special characters:

<soapenv:Header>
  <soap:HeaderLogin>
     <soap:username>{!HTMLENCODE($Credential.UserName)}</soap:username>
     <soap:password>{!HTMLENCODE($Credential.Password)}</soap:password>
  </soap:HeaderLogin>
</soapenv:Header>

You can read more about this feature in Merge Fields for Apex Callouts That Use Named Credentials.

1
  • 1
    Additional info: This also works in apex when settings the callout as endpoint in you http request and settings '{!$Credential.Password}' somewhere in the dom: node.addChildElement('username', ns, null).addTextNode('{!$Credential.UserName}');
    – Stijn
    Dec 5, 2017 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .