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
saravanasaravana 

Help in making webservice calls using connection.js

Hi,
I am having a problem in making calls to webservice methods using the sforce.apex.execute method.
The exception i get is

Error {faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', line:'1019', sourceURL:'https://na5.salesforce.com/soap/ajax/13.0/connection.js', }

I wrote a javascript to call webservice method passing arguments to save it in my server.
/////////////////////////////////////////////////////////////////////////////////////
function call()
{
var mess= sforce.apex.execute('MyController','getSend',{cname:document.getElementById('cname').value,email:document.getElementById('cmail').value,phone:document.getElementById('cphone').value,product:products});
}
/////////////////////////////////////////////////////////////////////////////////////

The controller contains the webservice method defined.
/////////////////////////////////////////////////////////////////////////////////////
WebService static String getSend(String cname,String email,String phone,String product) {
System.debug('inside send '+cname );
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
//req.setHeader('Cookie','sid='+UserInfo.getSessionId());
//+'&product='+product
req.setEndpoint('http://www.iclub.in/MRUserTrail/insert.html? userName='+UserInfo.getUserName()+'&cname='+cname+'&cemail='+email+'&cphone='+phone+'&product='+product);
HttpResponse res = h.send(req);
System.debug(res.getBody());
XmlStreamReader reader = res.getXmlStreamReader();

while(reader.hasNext()) {
if (reader.getEventType() == XmlTag.START_ELEMENT) {
if(reader.getLocalName()=='status')
{
reader.next();
if(reader.getText()=='FAILURE')
{
reader.next();reader.next();reader.next();

return 'f$'+reader.getText();
}
if(reader.getText()=='SUCCESS')
{
reader.next();reader.next();reader.next();
return 's$'+reader.getText();
}
}
}
reader.next();
}
return 'f$Please try again after some time';
}
/////////////////////////////////////////////////////////////////////////////////////
I used this sforce.apex.execute because there was not way to call an controller method by passing
an argument's to it.I tried various ways of callling the controller but i could not do so. If it is
just an normal method without parameters then we can call it using !${Accounts.name}.

Q.Please tell me is there a way to call controller methods by passing parameters to it?

Q2.Please help me on this exception i am getting when calling the javascript function.

Thaking You everyone.
ESES
Not sure why you want to call your controller's method using Connection.js when you can call action methods directly from your page.
Code:
example 1: If you want to call external api on page load
<apex:page controller="myController" action="{!callExternalApi}">
..
</apex:page>


example 2: If you want to call external api on button press
<apex:page controller="myController">
<apex:form>
<apex:commandButton value="Make a call" action="{!callExternalApi}"/>
</apex:form>
</apex:page>



Controller:

public class myController{
public void callExternalApi(){
//make API calls to external api and do whatever you want to do with response
}
}

 

saravanasaravana
Re:Not sure why you want to call your controller's method using Connection.js when you can call action methods directly from your page.

I want to pass parameters (String arguments) into the controller and i could not find any other way to do so. Thats why i used Connection.js.
If there's any want please tell me.

Thanks Emad Salman for your quick reply.

ESES
Try using the "param" component with links/button components. There are a few posts on the forum regarding it already so you can look at those too in addition to the description in component reference + dev guide.

Alternatively, if the arguments are based on user input, you can capture the input using components like inputText etc. and have the value available to you in apex properties/variables without the need of passing parameters.