0

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;

     }   
  }
}
0

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.

  • I have updated the question but basically I cannot change method1 as it is already in prod. Any suggestions? – M guy Jan 29 '18 at 10:50
  • @Mguy Are you changing your class at all? If so, you can change the method. I'm not sure why there'd be a problem. Still, provided an edit that shows an alternative, but you're still going to have to modify the method. – sfdcfox Jan 29 '18 at 10:55
  • There are multiple classes and vf pages already referencing method1 so I cannot change the return values or add page redirects within that method. This is why I created method2 as it is specific to me – M guy Jan 29 '18 at 10:57
  • @Mguy So write a method2 that calls method1? There's easier ways to do what you're trying to do. – sfdcfox Jan 29 '18 at 11:19
  • Ah I cannot believe I didn't think of that. Obviously its easier. Thank you – M guy Jan 29 '18 at 11:23
0

In vf page, add the id as paramater in URL and retrieve in method1 in apex using ApexPages.currentPage().getParameters().get('id'), where id is parameter name.

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.