7

I was trying to use the sforce.apex.execute in javascript using

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

i got the following error in connection.js

uncaught exception: {faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', }

How can i overcome this error

16

Sometimes the session ID isn't set correctly, so you have to set it yourself.

sforce.connection.sessionId = "{!$Api.Session_ID}";

Updated: Here's an example usage.

<apex:page >
<apex:includeScript value="/soap/ajax/28.0/connection.js"/>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
function doAnon() {
    sforce.apex.executeAnonymous(document.getElementById("input").value, anonResult);
}
function anonResult(result) {
    alert(result);
}
</script>
<textarea id="input"></textarea>
<button onclick="doAnon()">Execute</button>
</apex:page>
  • where should i exactly call this code – Aravind30790 Jul 19 '13 at 6:01
  • why am i getting this error while implementing your code 'sforce' is undefined – Aravind30790 Jul 19 '13 at 11:46
  • 1
    I've updated my answer to show a code example. – sfdcfox Jul 19 '13 at 13:18
  • receiving errors Refused to set unsafe header "User-Agent" – Aravind30790 Jul 20 '13 at 3:53
  • 1. Refused to set unsafe header "User-Agent" 2. Failed to load resource: the server responded with a status of 500 (Server Error) c.ap1.visual.force.com/services/Soap/u/27.0 3. Uncaught {faultcode:'sf:INVALID_SESSION_ID', faultstring:'INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session', detail:{UnexpectedErrorFault:{exceptionCode:'INVALID_SESSION_ID', exceptionMessage:'Invalid Session ID found in SessionHeader: Illegal Session', }, }, } – Aravind30790 Jul 20 '13 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.