function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
stunaitestunaite 

(HowTo) Sending a SOAP message within Apex

Hi!

 

I want to use a webservice whose WSDL is unable to be imported by Salesforce. So I pick up its WSDL file as input for a SOAP Client "SOAP UI" which gave me the soap message to request de service:

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:ConversionRate>
         <web:FromCurrency>USD</web:FromCurrency>
         <web:ToCurrency>EUR</web:ToCurrency>
      </web:ConversionRate>
   </soapenv:Body>
</soapenv:Envelope>

 So what I need is to send this SOAP message through my apex code. I tried this skeleton:

 

 

//setting endpoint
string serviceUrl = 'http://www.webserviceX.NET/currencyconvertor.asmx';

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(serviceUrl); 
req.setMethod('POST');
req.setTimeout(10000);

HttpResponse res = h.send(req);
string bodyRes=res.getBody();

 What should I add to my code in order I can send the SOAP message above?

 

Thanks in Advance

MiddhaMiddha

This worked for me:

 

				Http h = new Http();
				HttpRequest req = new HttpRequest();
				req.setMethod('POST');
				req.setHeader('Accept-Encoding','gzip,deflate');
				req.setHeader('Content-Type','text/xml;charset=UTF-8');
				req.setHeader('SOAPAction', URL);
				req.setHeader('User-Agent','Jakarta Commons-HttpClient/3.1');
				
				
				Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
				String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
				req.setHeader('Authorization', authorizationHeader);
				
				req.setEndpoint(CustomerCheck__c.getAll().get('endpoint').value__c);
				
				String requestString = '<<SOAP MESSAGE>>';
						
				req.setHeader('Content-Length',String.valueOf(requestString.length()));
		
				req.setBody(requestString);
				h.send(req);

 

stunaitestunaite

Hi,

 

Can you put here what was the resulting SOAP message?

 

 

MiddhaMiddha
String requestString = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing">' + 
					'<soap:Header>' + 
						'<ei xmlns="http://dddddddd.com/schemas/envelope/v3_0">' + 			
								'<type>request</type>' + 			
										'<service>' +
												'<name>getProfile</name>' +
												'<version>3.6</version>' +
										'</service>' +
										'<originator>SFDC</originator>' +
										'<businessProcessName>LM_CSR_AccountAssessmentComm</businessProcessName>' +
										'<requestDateTime>' + System.Now().format('yyyy-MM-dd') + '</requestDateTime>' +
							'</ei>' + 	
					'</soap:Header>' + 
					'<soap:Body>' +
								'<request xmlns="http://dddddddd.com/schemas/wsdl/bill/getProfile">' +
									'<customerPhoneNumber>' +
											'<phone>' +
													'<phoneAreaCode xmlns="http://dddddddd.com/schemas/entities/v3_1">' + areaCode + '</phoneAreaCode>' +
													'<phoneNumber xmlns="http://dddddddd.com/schemas/entities/v3_1">' + phoneNumber + '</phoneNumber>' +
											'</phone>' +
									'</customerPhoneNumber>' +
								'</request>' +
							'</soap:Body>' +
						'</soap:Envelope>';