+ Start a Discussion
theitdeptrockstheitdeptrocks 

Salesforce HTTP callout to Salesforce Site Web service

Hi all,

 

I'm working on a simple proof of concept project to help me understand web services better.

 

On the Salesforce Site side, I have the following web service:

 

global class helloWorld  
{  
    WebService public static String GetMessage()  
    {  
        return 'Hello world';  
    }  
}

The corresponding site is http://mydomain.sandboxname.cs1.force.com/webservice.

The site's Active Site Home Page is set to a nearly blank page called test and I have enabled the above class within the site's Public Access Settings

 

On the VisualForce page side that I'm trying to make the call from, I have the following:

 

public string getContent(){
	Http h = new http();
	HttpRequest req = new HttpRequest();
//req.setEndpoint('http://mydomain.sandboxname.cs1.force.com/services/Soap/class/helloWorld');
	req.setEndpoint('http://mydomain.sandboxname.cs1.force.com/webservice');
	req.setMethod('POST');
	req.setHeader('Content-Type', 'text/xml;charset=utf-8'); 
	req.setHeader('Content-Length', '18'); 
	req.setHeader('soapAction', 'https://cs1-api.salesforce.com/services/Soap/class/helloWorld');
	HttpResponse res = h.send(req);
		system.debug('Body was:  '+ res);
		system.debug('String was:' + res.getBody());
		return res.getBody();
	}

This is the portion I'm not sure of.  Really just winging it.  As it stands, I'm currently receiving this error:

System.CalloutException:  Read timed out

 

I'm not sure what to put as my endpoint, nor am I sure about the header information.  Previously, without the Content-Length, it complained.  

 

Within the WSDL for the class, I see "http://soap.sforce.com/schemas/class/helloWorld" and "https://cs1-api.salesforce.com/services/Soap/class/helloWorld" but not sure if those come into play.

 

I've successfuly called out to an externally hosted web service and had a much more complex request body, but was able to put it together using another example on the web.  

 

Can any of you school me on what I'm doing wrong or point me in the direction of  good example?

 

Thanks!

 

 

 

theitdeptrockstheitdeptrocks

Here's what I got to work:

 

	public string getContent(){
		string elementValue = 'Not Found';
		Http h = new http();
		HttpRequest req = new HttpRequest();
		req.setEndpoint('http://mydomain.mysandbox.cs1.force.com/webservice/services/Soap/class/helloWorld');
		req.setMethod('POST');
		req.setHeader('Content-Type', 'text/xml;charset=utf-8'); 
		req.setHeader('SOAPAction', 'http://soap.sforce.com/schemas/class/helloWorld');
		string b ='<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://soap.sforce.com/schemas/class/helloWorld"><soapenv:Header><hel:DebuggingInfo><hel:debugLog>?</hel:debugLog></hel:DebuggingInfo></soapenv:Header><soapenv:Body><hel:GetMessage></hel:GetMessage></soapenv:Body></soapenv:Envelope>';
        req.setbody(b);
		HttpResponse res = h.send(req);
		system.debug('Body was:  '+ res);
		system.debug('String was:' + res.getBody());
		
		
		XmlStreamReader reader = res.getXmlStreamReader();
		reader.setNamespaceAware(true);
		while(reader.hasNext()) {
        	if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'result') {
            	reader.next();
	            elementValue = reader.getText();
	            system.debug('The element value was:  ' + elementValue);
        	} 
        reader.next();
     	}
		return elementValue;
	}

 

 

theitdeptrockstheitdeptrocks

And for good measure, here is another test I did to create a first and last name to the webservice, create a contact, and return the new contact record's ID.

 

VF Page:

 

<apex:outputPanel id="SFService_CreateRecord">
    First Name:  <apex:inputText value="{!FirstName}" title="First Name"/><br />
    Last Name:   <apex:inputText value="{!LastName}" title="Last Name" />
    <apex:commandButton value="Create Contact" action="{!CreateContact}" reRender="result"/>
</apex:outputPanel>
<apex:outputPanel id="result"> 
    Result:  {!TheReturnedName}
</apex:outputPanel>   

 Class:

 

public String LastName{get;set;}
public String FirstName{get;set;}
public String TheReturnedName{get;set;}
	public void CreateContact(){
		string elementValue = 'Not Found';
		Http h = new http();
		HttpRequest req = new HttpRequest();
		req.setEndpoint('http://mydomain.mysandbox.cs1.force.com/webservice/services/Soap/class/helloWorld');
		req.setMethod('POST');
		req.setHeader('Content-Type', 'text/xml;charset=utf-8'); 
		req.setHeader('SOAPAction', 'http://soap.sforce.com/schemas/class/helloWorld');
		string b ='<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://soap.sforce.com/schemas/class/helloWorld"><soapenv:Header><hel:DebuggingInfo><hel:debugLog>?</hel:debugLog></hel:DebuggingInfo></soapenv:Header><soapenv:Body><hel:CreateTheContact><hel:firstname>'+firstname+'</hel:firstname><hel:lastname>'+lastname+'</hel:lastname></hel:CreateTheContact></soapenv:Body></soapenv:Envelope>';
        req.setbody(b);
		HttpResponse res = h.send(req);
		system.debug('Body was:  '+ res);
		system.debug('String was:' + res.getBody());
		
		
		XmlStreamReader reader = res.getXmlStreamReader();
		reader.setNamespaceAware(true);
		while(reader.hasNext()) {
        	if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'result') {
            	reader.next();
	            TheReturnedName = reader.getText();
	            system.debug('The element value was:  ' + TheReturnedName);
        	} 
        reader.next();
     	}
		
			
	}

 Web Service:

 

    WebService public static String CreateTheContact(string firstname, string lastname)
    {
    	contact c = new contact();
    	c.firstname = firstname;
    	c.lastname = lastname;
    	insert c;
    	return c.id;
    }   

 

 

 

 

Suresh RaghuramSuresh Raghuram

hi i am new to the integration and i  went through your question and found intresting and hope for the help from you i am writing this could you please explain the process and a scenario in detail that i could understand how the integration is being carried out. I will be very glad if you could help.

 

thank you

suree

 

 

 

Jia HuJia Hu
Hi theitdeptrocks,

I really want to know if you can call the Apex WS from the HTTP callout.

Thanks ahead.