[SalesForce] Pass ID of current record to apex controller from Visualforce

So I have a few issues that I don't know how to resolve. The first is when a button is pressed, I need to pass the current record id to method1. As seen below method1 is expecting an id and I do not know how to pass a argument from Visualforce.

The second issue is once a value is returned from method1, if it is 0, call method2 (in apex class). How do I call an apex method from JS?

Just as an FYI – I cannot change method1 as it is already in use. I realize I am doing a lot of extra work but this is because method1 is currently in prod. I wrote method2.

Visualforce controller:

<apex:page controller="customObject" extensions="apexClass">
  <apex:form >
      <apex:commandButton action="{!method1}" value="aNumber" id="theButton"/> <!--NEED TO PASS IN ID OF CURRENT RECORD to method 1 -->
      <apex:actionFunction action="{!method2}"/>
  </apex:form>
  <script>
  if(value of method1 == 0)
  {
    call method2 //NO arguments required
  }
  else if(value of method1 == 1)
  {
    alert("Cannot do this!");
  }
  else
    alert("error!");
  </script> 
</apex:page>

Apex class:

Global class apexClass{
 Public customObject(ApexPages.StandardController controller) {
}

WebService static Integer method1(Id someID)
{
    if(everythings ok)
        return 0;

    return 1;
}

public pagereference method2()
{
    try{
    pagereference newPageRef=new pagereference('/one/one.app#/home');
    newPageRef.setRedirect(true);
    return newPageRef;
    }
     catch(Exception ex1)
     {        
            ApexPages.addMessages(ex1);
            return null;

     }   
  }
}

Best Answer

There's no reason to go through all of this work, as far as I can tell. You could restructure your class as follows:

public class apexClass{
 Id recordId;
 Public customObject(ApexPages.StandardController controller) {
   recordId = controller.getId();
 }

 public PageReference method1()
 {
    if(everythingIsOkay) {
      return new PageReference('/one/one.app#/home').setRedirect(true);
    } else {
      ApexPages.addMessage(
        new ApexPages.Message(ApexPages.Severity.ERROR, 'You cannot do this'));
      return null;
    }
  }
}

If you absolutely insist on using two methods, then you'd use a @RemoteAction for the initial call, followed by an actionFunction to redirect:

<apex:page controller="customObject" extensions="apexClass">
  <apex:form >
      <script>
      function methodCall1() {
        apexClass.method1('{!$CurrentPage.parameters.Id}',
          function(result, event) {
            if(event.status && result == 0) {
              method2();
            } else {
              if(result == 1) {
                alert('Not allowed');
              }
            }
            alert('Error');
          }
        }
      </script>
      <apex:commandButton onclick="methodCall1(); return false;" value="aNumber" id="theButton"/>
      <apex:actionFunction name="method2" action="{!method2}"/>
  </apex:form>
  <script>
  if(value of method1 == 0)
  {
    call method2 //NO arguments required
  }
  else if(value of method1 == 1)
  {
    alert("Cannot do this!");
  }
  else
    alert("error!");
  </script> 
</apex:page>

Global class apexClass{
 Public customObject(ApexPages.StandardController controller) {
}

@RemoteAction static Integer method1(Id someID)
{
    if(everythings ok) {
        return 0;
    }
    return 1;
}

public pagereference method2()
{
    pagereference newPageRef=new pagereference('/one/one.app#/home');
    newPageRef.setRedirect(true);
    return newPageRef;
}

This is probably the worst possible solution, and I'd recommend that you avoid it unless you absolutely need to.

Related Topic