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;
}
}
}