You need to sign in to do that
Don't have an account?
Sarju Mulmi
sforce.apex.execute not working
All,
I'm trying to call an apex function from a javascript button. I've read the developer guide and several relevant posts on this forum, but still can find out the issue. I have not created any namespace prefix for my dev org. The conroller is different from the Helper class with the web service method.
VF page:
Apex class for webservice:
Sarju
I'm trying to call an apex function from a javascript button. I've read the developer guide and several relevant posts on this forum, but still can find out the issue. I have not created any namespace prefix for my dev org. The conroller is different from the Helper class with the web service method.
VF page:
<apex:page showHeader="false" > <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 testRemote(){ var id = sforce.apex.execute("HelperClass","getContactId",{}); alert('id is: '+id); } </script> <div> <button onclick="testRemote();"> Remote </button> </div> </apex:page>
Apex class for webservice:
global class HelperClass { webService static String getContactId(){ String id = '003j0000002CQ3B'; return id; } }Much appreciated,
Sarju
All Answers
The code is not having session ID so it is breaking.
Use below code:- Hope this helps.
Try below vf page code:
<apex:page showHeader="false" >
<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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function testRemote(){
sforce.connection.sessionId="{!$Api.Session_ID}";
var id = sforce.apex.execute("
ahome.HelperClass","getContactId",{});alert('id is: '+id);
}
</script>
<div>
<button onclick="testRemote();">
Remote
</button>
</div>
</apex:page>
If any name space is registering in your org replace it with “ahome” otherwise remove the ahome. From code before replacing you vf page code.
My only gripe is not asking the forum sooner!!!!!:P
In the above apex class, for the getContactId() method sarju is not passing any parameters!! So, what is that method had some parameter say like an Id, Will the javascript code have any changes in the vf page or will it remain the same?
Thank u