Monday, October 1, 2012

To invoke Apex using the AJAX toolkit & to remove the HTTP 500 error when showHeader="false"



The AJAX toolkit includes built-in support for invoking Apex, so I have tried to call a webservice method through sforce.apex.execute . The execute method takes primitive data types, sObjects, and lists of primitives or sObjects.

To call a webService method with no parameters, use {} as the third parameter.

I have developed a sample code, kindly refer this:-

---------------------------VF page ------------------------------------

<apex:page sidebar="false" showHeader="true" >
<head> 
         <script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
         <script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>

<script type="text/javascript"> 
function sendUserName(){
                      //FirstExample is namespace
                     //Additionthrughsforce is ClassName
                    //addition is methodname
                   //third parameters are the argument sent to the method
var res = sforce.apex.execute("FirstExample.Additionthrughsforce","addition",{numOne:4,numTwo:5});
return false;
}
</script>
</head>
<body>
<apex:form>
<apex:commandButton onclick="sendUserName();" value="Call"/>
</apex:form>
</body>
</apex:page>

----------------------------Apex Class----------------------

global class Additionthrughsforce {
webservice static Integer addition(integer numOne, integer numTwo){
return numOne + numTwo;
}
}

****Please Note****


When showHeader="true " ,it was successful to call an public webservice method but when showHeader="false" , it fails with the status code as 500.

I have received an 500 (Internal Server Error) in the javascript console (Chrome) when invoking Apex from sforce.apex.execute.


Workaround when showHeader="false" would be to add
**var __sfdcSessionId = '{!GETSESSIONID()}';**

No comments:

Post a Comment