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

Help in making webservice calls using connection.js

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:'', }

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.setEndpoint(' userName='+UserInfo.getUserName()+'&cname='+cname+'&cemail='+email+'&cphone='+phone+'&product='+product);
HttpResponse res = h.send(req);
XmlStreamReader reader = res.getXmlStreamReader();

while(reader.hasNext()) {
if (reader.getEventType() == XmlTag.START_ELEMENT) {

return 'f$'+reader.getText();
return 's$'+reader.getText();
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 !${}.

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.
Not sure why you want to call your controller's method using Connection.js when you can call action methods directly from your page.
example 1: If you want to call external api on page load
<apex:page controller="myController" action="{!callExternalApi}">

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


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


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.

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.