2

I have used following the code in my Onclickjavascript(custom button).

I have used two buttons in his pop and I would like to call the apex method from here.So, kindly let me know how to achieve this?

Code:

{!REQUIRESCRIPT('/soap/ajax/26.0/connection.js')} 
    var j$ = jQuery.noConflict();
    var iframe_url =  '{!URLFOR("/apex/apexpage")};
    var j$modalDialog = j$('<div id="opppopup"></div>')
    .html('<div align="center" style="margin-bottom:5px;"><input type="button" value="Save" class="btn" style="width:50px;"/>

<input type="button" value="Update" class="btn" /></div>



    <iframe  />') 

    .dialog({ 
    autoOpen: false, 
    title: 'PDF Preview', 
    resizable: true, 
    width: 800, 
    height: 540, 
    autoResize: true, 
    modal: true, 
    draggable: true 
    });

Apex method:

Public Class TestMethod(){        
    Public void Update(){

    }        
}

How to call the "Update" method when click the button in the above javascript frame code?

  • @highfive do you know the answer? – SFDC LEARNER Jul 18 '14 at 6:43
  • Can't you have <apex:commandButton /> for this instead an HTML button? Is so you can simply set action="{!update}" to call the method. Also is this your controller of your VF page? – highfive Jul 18 '14 at 6:47
  • Yes, but I would like to call from that html button. – SFDC LEARNER Jul 18 '14 at 7:01
2

You can achieve by using the java script remoting.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

1

There are several ways to do this.

  1. Using JavaScript Remoting
  2. Using <apex:actionFunction />

If you go with <apex:actionFunction /> add following line in your VF page

<apex:actionFunction action="{!update}" name="callJS" />
<input type="button" value="Update" onclick="callJS();" class="btn" />

This will trigger your Update() function in the controller.

Based on your requirement you can decide what option to be selected. Basically if you need to manage post back and rerender some areas upon the postback, you have to go with <apex:actionFunction /> while remote call methods call back functions handle it asynchronously.

  • the button is in the ifram(javascript) so How can I call the apex method. – SFDC LEARNER Jul 18 '14 at 7:09
0

If want to call apex from Javascript then you'll need to make your class global and your method a webservice.

e.g.

global with sharing class myClass {


  webService static void myMethod(id anId){ // do something}
}

Then call your apex via:-

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

var retStr; 
retStr = sforce.apex.execute("myClass", "myMethod", {anId:'{!mySObject.Id}'}); 

More info here (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm)

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.