I have detail page and a custom button(Make Callout) which invokes a apex callout and does some validations. Below is the code of the VF page of the custom button:
I have the tag so that I can show the standard detail page after the user click the button.
VF Page:
<apex:page standardController="SObject__c" extensions="sampleControllerClass" action="{!makeCallout}">
<apex:pageMessages />
<apex:detail subject="{!SObject__c.id}"/>
</apex:page>
Controller Class:
public class sampleControllerClass {
public Sobject__C sr{get;set;};
public UnityWorkOrderCreateController(ApexPages.StandardController controller) {
sr = (Sobject__C) controller.getRecord();
}
public PageReference makeCallout()
{
// Does some validations here and gives the error back and returns null so that the message is displayed on the page
If( A validation rule){
ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR,'An Error Occured'));
return null;
}
// Makes Callout
try{
// calllout code here
}Catch(Exception e){
}
}
}
My Problem here:
I start with a detail page before clicking the custom button. WHen I click the custom button my VF page kicks in and if there is any validation error the ApexPages.addMessage line runs and the error gets displayed on the current page(VF Page). Then I click edit button to make changes to the fields so that the validation rule in the if() block in controller passes.
Once I click save after making changes to the fields on the page(the page that comes up after I click edit button)-->>"The Callout happens without clicking the custom button(Make Callout) again" if the validation rule passed. How can I prevent this invocation of makeCallout() function as soon as I click save button. I want the user to first save the record and then click the custom button to make the callout(call the VF page)
Flow:
- Create a new record(standard detail page)->
- save->
- click makecallout button->
- VF Page is invoked(has apex:detail tag)->
- the if loop in controller catches a validation error->
- apex page message gives an error->
- error shows up on page->
- user clicks edit and then save again-
- Makecallout happens on its own with out click the makecallout button again.