+ Start a Discussion
Alex85Alex85 

sforce.apex.execute Response : status - 500

Hi.

I try to call my web service from javascript over sforce.apex.execute method, but I get as response the status: 500.

what I did wrong?

 

Following error I get in AJAX Toolkit Shell:

 

Request: server- /services/Soap/package/WSHelper
<textarea cols=80 rows=5 wrap=hard>&lt;se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;se:Header xmlns:sfns="http://soap.sforce.com/schemas/package/WSHelper"/&gt;&lt;se:Body&gt;&lt;setName xmlns="http://soap.sforce.com/schemas/package/WSHelper"&gt;&lt;n&gt;Smith&lt;/n&gt;&lt;/setName&gt;&lt;/se:Body&gt;&lt;/se:Envelope&gt;</textarea>
Response : status - 500
<textarea cols=80 rows=5 wrap=hard>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="http://soap.sforce.com/2006/08/apex"&gt;&lt;soapenv:Body&gt;&lt;soapenv:Fault&gt;&lt;faultcode&gt;sf:INVALID_SESSION_ID&lt;/faultcode&gt;&lt;faultstring&gt;INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session&lt;/faultstring&gt;&lt;/soapenv:Fault&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;</textarea>

 

 

here is the javascript code:

 

<script src="/soap/ajax/17.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/17.0/apex.js" type="text/javascript"></script>

<script>
function test(){
	sforce.debug.trace=true;
	alert('test');
	var name = sforce.apex.execute("WSHelper", "setName", {n:"Smith"});
	alert(name); 
}
</script>

 

 

Here is my web service code:

 

global class WSHelper {
	webService static String getName() {
        return 'WSHelper';
   }
   
   webService static String setName(String n) {
        return n;
   }
}

 

 

and here the generated xml-file of my web service:

 

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Web Services API : WSHelper
-->
<definitions targetNamespace="http://soap.sforce.com/schemas/class/WSHelper" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.sforce.com/schemas/class/WSHelper">
 <types>
  <xsd:schema elementFormDefault="qualified" targetNamespace="http://soap.sforce.com/schemas/class/WSHelper">
   <xsd:element name="DebuggingInfo">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="debugLog" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:simpleType name="ID">
    <xsd:restriction base="xsd:string">
     <xsd:length value="18"/>
     <xsd:pattern value="[a-zA-Z0-9]{18}"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="LogCategory">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="Db"/>
     <xsd:enumeration value="Workflow"/>
     <xsd:enumeration value="Validation"/>
     <xsd:enumeration value="Callout"/>
     <xsd:enumeration value="Apex_code"/>
     <xsd:enumeration value="Apex_profiling"/>
     <xsd:enumeration value="Visualforce"/>
     <xsd:enumeration value="All"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:simpleType name="LogCategoryLevel">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="Internal"/>
     <xsd:enumeration value="Finest"/>
     <xsd:enumeration value="Finer"/>
     <xsd:enumeration value="Fine"/>
     <xsd:enumeration value="Debug"/>
     <xsd:enumeration value="Info"/>
     <xsd:enumeration value="Warn"/>
     <xsd:enumeration value="Error"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:complexType name="LogInfo">
    <xsd:sequence>
     <xsd:element name="category" type="tns:LogCategory"/>
     <xsd:element name="level" type="tns:LogCategoryLevel"/>
    </xsd:sequence>
   </xsd:complexType>
   <xsd:simpleType name="LogType">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="None"/>
     <xsd:enumeration value="Debugonly"/>
     <xsd:enumeration value="Db"/>
     <xsd:enumeration value="Profiling"/>
     <xsd:enumeration value="Callout"/>
     <xsd:enumeration value="Detail"/>
    </xsd:restriction>
   </xsd:simpleType>
   <xsd:element name="DebuggingHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="categories" minOccurs="0" maxOccurs="unbounded" type="tns:LogInfo"/>
      <xsd:element name="debugLevel" type="tns:LogType"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="CallOptions">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="client" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="SessionHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="sessionId" type="xsd:string"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="AllowFieldTruncationHeader">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="allowFieldTruncation" type="xsd:boolean"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="getName">
    <xsd:complexType>
     <xsd:sequence/>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="getNameResponse">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="result" type="xsd:string" nillable="true"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="setName">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="n" type="xsd:string" nillable="true"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="setNameResponse">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="result" type="xsd:string" nillable="true"/>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>
 </types>
 <!-- Message for the header parts -->
 <message name="Header">
  <part name="AllowFieldTruncationHeader" element="tns:AllowFieldTruncationHeader"/>
  <part name="CallOptions" element="tns:CallOptions"/>
  <part name="DebuggingHeader" element="tns:DebuggingHeader"/>
  <part name="DebuggingInfo" element="tns:DebuggingInfo"/>
  <part name="SessionHeader" element="tns:SessionHeader"/>
 </message>
 <!-- Operation Messages -->
 <message name="getNameRequest">
  <part element="tns:getName" name="parameters"/>
 </message>
 <message name="getNameResponse">
  <part element="tns:getNameResponse" name="parameters"/>
 </message>
 <message name="setNameRequest">
  <part element="tns:setName" name="parameters"/>
 </message>
 <message name="setNameResponse">
  <part element="tns:setNameResponse" name="parameters"/>
 </message>
 <portType name="WSHelperPortType">
  <operation name="getName">
   <input message="tns:getNameRequest"/>
   <output message="tns:getNameResponse"/>
  </operation>
  <operation name="setName">
   <input message="tns:setNameRequest"/>
   <output message="tns:setNameResponse"/>
  </operation>
 </portType>
 <binding name="WSHelperBinding" type="tns:WSHelperPortType">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="getName">
   <soap:operation soapAction=""/>
   <input>
    <soap:header use="literal" part="SessionHeader" message="tns:Header"/>
    <soap:header use="literal" part="CallOptions" message="tns:Header"/>
    <soap:header use="literal" part="DebuggingHeader" message="tns:Header"/>
    <soap:header use="literal" part="AllowFieldTruncationHeader" message="tns:Header"/>
    <soap:body use="literal" parts="parameters"/>
   </input>
   <output>
    <soap:header use="literal" part="DebuggingInfo" message="tns:Header"/>
    <soap:body use="literal"/>
   </output>
  </operation>
  <operation name="setName">
   <soap:operation soapAction=""/>
   <input>
    <soap:header use="literal" part="SessionHeader" message="tns:Header"/>
    <soap:header use="literal" part="CallOptions" message="tns:Header"/>
    <soap:header use="literal" part="DebuggingHeader" message="tns:Header"/>
    <soap:header use="literal" part="AllowFieldTruncationHeader" message="tns:Header"/>
    <soap:body use="literal" parts="parameters"/>
   </input>
   <output>
    <soap:header use="literal" part="DebuggingInfo" message="tns:Header"/>
    <soap:body use="literal"/>
   </output>
  </operation>
 </binding>
 <service name="WSHelperService">
  <documentation/>
  <port binding="tns:WSHelperBinding" name="WSHelper">
   <soap:address location="https://na7-api.salesforce.com/services/Soap/class/WSHelper"/>
  </port>
 </service>
</definitions>

 

Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
sebcossebcos

Hi,

I have tried using your code in a custom link and it works fine.

I have created a custom detail page link in an object (any object will do) with the following javascript to execute:

 

 

{!requireScript("/soap/ajax/17.0/connection.js")} 
{!requireScript("/soap/ajax/17.0/apex.js")} 

sforce.debug.trace=true;
alert('test');
var name = sforce.apex.execute("WSHelper", "setName", {n:"Smith"});
alert(name);

This executes whenever I click on the custom link on the page and returns the following on the shell:

 

Using bookmarklet version of shell: commands will run in opener's context.
Request: server- /services/Soap/package/WSHelper
<textarea cols=80 rows=5 wrap=hard>&lt;se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;se:Header xmlns:sfns="http://soap.sforce.com/schemas/package/WSHelper"&gt;&lt;sfns:SessionHeader&gt;&lt;sessionId&gt;00D400000009mCd!ARYAQIC5MYURtut79OYIddddrtr3ddd4IPIHkmYrEVy3PeaoRYGPwHMWYyYoAEnOFs_SrUds8bo23Dh8zPsl9ObtMpFYNWr&lt;/sessionId&gt;&lt;/sfns:SessionHeader&gt;&lt;/se:Header&gt;&lt;se:Body&gt;&lt;setName xmlns="http://soap.sforce.com/schemas/package/WSHelper"&gt;&lt;n&gt;Smith&lt;/n&gt;&lt;/setName&gt;&lt;/se:Body&gt;&lt;/se:Envelope&gt;</textarea>
Response : status - 200
<textarea cols=80 rows=5 wrap=hard>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/package/WSHelper"&gt;&lt;soapenv:Body&gt;&lt;setNameResponse&gt;&lt;result&gt;Smith&lt;/result&gt;&lt;/setNameResponse&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;</textarea>

 

I am assuming that, as the error message says, you have not got a valid session ID. You need to have a session id to invoke the webservice.

Review the Ajax toolkit reference guide to see how to get a session id:

http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_connecting.htm or http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_introducing.htm#sforce_api_ajax_sample_scontrol