1

Is it possible to add headers to the xml? We can add http headers, but we need to add information to the envelope header.

We are using wsdl2apex and following the documentation here:

https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

public S3.Status DeleteObject(String Bucket,String Key,String AWSAccessKeyId,DateTime Timestamp,String Signature,String Credential) {
      S3.DeleteObject_element request_x = new S3.DeleteObject_element();
      S3.DeleteObjectResponse_element response_x;
      request_x.Bucket = Bucket;
      request_x.Key = Key;
      request_x.AWSAccessKeyId = AWSAccessKeyId;
      request_x.Timestamp = Timestamp;
      request_x.Signature = Signature;
      request_x.Credential = Credential;
      request.inputHttpHeaders_x = new Map<String, String>();
      request_x.inputHttpHeaders_x.put('myHeader', 'myValue'); // this adds to the http headers, how can we put this in the <env: header />
      Map<String, S3.DeleteObjectResponse_element> response_map_x = new Map<String, S3.DeleteObjectResponse_element>();
      response_map_x.put('response_x', response_x);
      WebServiceCallout.invoke(
        this,
        request_x,
        response_map_x,
        new String[]{endpoint_x,
        '',
        'http://s3.amazonaws.com/doc/2006-03-01/',
        'DeleteObject',
        'http://s3.amazonaws.com/doc/2006-03-01/',
        'DeleteObjectResponse',
        'S3.DeleteObjectResponse_element'}
      );
      response_x = response_map_x.get('response_x');
      return response_x.DeleteObjectResponse;
  }

This is the generated SOAP xml:

enter image description here

Is it possible to set the <env: Header> with information?

3
  • 2
    If you can't figure out how do do this with this API (which is the API that the automatically generated WSDL2Apex code uses) you can build the full XML request yourself using e.g. the Apex DOM and the Http classes to send and receive the XML.
    – Keith C
    Aug 12, 2015 at 7:52
  • Thanks Keith, I believe this is the only option we have. Aug 12, 2015 at 16:39
  • A plus of that approach is although you will end up with more code it should be clearer and easier to change in the future.
    – Keith C
    Aug 12, 2015 at 17:10

1 Answer 1

1

You can generate SOAP headers for use with WebServiceCallout.invoke using a member and a corresponding string with the _hns suffix.

For reference, Wsdl2Apex processes this around Line 86 of the BindingClass.java file.

You will need to create an inner class to contain the header data.

Here is an example from the UPS XAV Service WSDL. The code was generated using the FuseIT SFDC Explorer Wsdl2Apex function (Disclosure, this is my current employer).

// Generated by FuseIT WSDL2Apex (http://www.fuseit.com/Solutions/SFDC-Explorer/Help-WSDL-Parser.aspx)
// Methods Included: ProcessXAV
// Primary Port Class Name: XAVPort 
public class wwwUpsComWsdlXoltwsXavV10 {
    public class XAVPort {
        public String endpoint_x = 'https://wwwcie.ups.com/webservices/XAV';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;

        // Setting this member will populate the <env:Header/> element 
        public wwwUpsComXmlschemaXoltwsUpssV10.UPSSecurity_element UPSSecurity;
        private String UPSSecurity_hns = 'UPSSecurity=http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0';

        private String[] ns_map_type_info = new String[]{'http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','wwwUpsComXmlschemaXoltwsUpssV10','http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0','wwwUpsComXmlschemaXoltwsCommonV10','http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1','wwwUpsComXmlschemaXoltwsErrorV11','http://www.ups.com/XMLSchema/XOLTWS/xav/v1.0','wwwUpsComXmlschemaXoltwsXavV10'};

        public wwwUpsComXmlschemaXoltwsXavV10.XAVResponse_element ProcessXAV(wwwUpsComXmlschemaXoltwsCommonV10.RequestType Request,String RegionalRequestIndicator,String MaximumCandidateListSize,wwwUpsComXmlschemaXoltwsXavV10.AddressKeyFormatType AddressKeyFormat) {
            // ...
        }
    }
}

public class wwwUpsComXmlschemaXoltwsUpssV10 {
    public class UPSSecurity_element {
        public wwwUpsComXmlschemaXoltwsUpssV10.UPSSecurity_UsernameToken_element UsernameToken;
        public wwwUpsComXmlschemaXoltwsUpssV10.UPSSecurity_ServiceAccessToken_element ServiceAccessToken;
        private String[] UsernameToken_type_info = new String[]{'UsernameToken','http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','','1','1','false'};
        private String[] ServiceAccessToken_type_info = new String[]{'ServiceAccessToken','http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','true','false'};
        private String[] field_order_type_info = new String[]{'UsernameToken','ServiceAccessToken'};
    }
    public class UPSSecurity_ServiceAccessToken_element {
        public String AccessLicenseNumber;
        private String[] AccessLicenseNumber_type_info = new String[]{'AccessLicenseNumber','http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','true','false'};
        private String[] field_order_type_info = new String[]{'AccessLicenseNumber'};
    }
    public class UPSSecurity_UsernameToken_element {
        public String Username;
        public String Password;
        private String[] Username_type_info = new String[]{'Username','http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','string','1','1','false'};
        private String[] Password_type_info = new String[]{'Password','http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','string','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0','true','false'};
        private String[] field_order_type_info = new String[]{'Username','Password'};
    }
}

If you do want to go down the HttpRequest path as Keith commented, the FuseIT SFDC Explorer tool can also generate code using the HttpRequest. I cover this in my Dreamforce 2014 presentation.

You must log in to answer this question.

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