+ Start a Discussion
ssssssss 

how to call methods from apex controoler to java script in visual force

In my application if i click on "ok" alert will be raised.If we click on"ok" button it should save the record and go back to home tab.If we click on "cancel" i dont want to save the record and simply go back to home tab.This is my requirement.Here i am facing the problem how to call methods from apex controoler to java script.

If any one knows plz help me.

 

My sample code is as follows:

 

VF page

 

<apex:page standardController="Holding_Instruments__c" extensions="aa_validate">
<script type="text/javascript">

function valid()
{
 var val = confirm ("Do U Want To Save The Record?")
 if (val)
 {
  saveandmove();//here i want to call the method from apex controller
 }
 
 else
 {
  dontsave();//here i want to call the method from apex controller
 }
 
}


</script>
     <apex:form >
     <apex:outputLabel value="SFN Control No." />
     <apex:inputField id="sfn" value="{!Holding_Instruments__c.SFN_Number__c}"/>
     <apex:commandButton value="OK"  onclick="return valid()"  />
    </apex:form>
</apex:page>

 

Controller:

 

public class aa_validate
{
  ApexPages.StandardController con;
  public aa_validate(ApexPages.StandardController controller)
  {
      this.con = controller;
  }
  public void save()
  {
  try
  {
   con.save();
  }
  catch(Exception ex1)
  {
  }
  }
  public PageReference saveandmove()
 {
  con.save();
  PageReference pr =new PageReference('https://ap1.salesforce.com/home/home.jsp'); 

  pr.setRedirect(true);
  return pr;
 }
 public PageReference dontsave()
 {
 
  PageReference pr =new PageReference('https://ap1.salesforce.com/home/home.jsp');

  pr.setRedirect(true);
  return pr;
 }
}

 

Thanks In Advance,

Manu..

shillyershillyer

Take a look at Apex in AJAX.

 

Hope that helps,

Sati

Himanshu SHimanshu S

Even I have same requirement. 

any help?

 

you got answer to your problem?