0

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:

  1. Create a new record(standard detail page)->
  2. save->
  3. click makecallout button->
  4. VF Page is invoked(has apex:detail tag)->
  5. the if loop in controller catches a validation error->
  6. apex page message gives an error->
  7. error shows up on page->
  8. user clicks edit and then save again-
  9. Makecallout happens on its own with out click the makecallout button again.
  • Comments are not for extended discussion; this conversation has been moved to chat. – Adrian Larson Apr 24 '17 at 13:07
  • @AdrianLarson Thanks for cleaning up. I have this challenging situation where I need to redirect the page back to the detail page(with error message on top). I though your insights might be helpful here. Do you have any suggestion for my problem? – SfdcBat Apr 24 '17 at 13:28
-1

You should really be returning back to the calling page if there is an error and display the error there. That means that you will have to make the calling page a vf page as well, but that should be simple.

Your calling page would just be

<apex:page standardController="SObject__c" extensions="PageMessageController">
    <apex:pageMessages />
    <apex:detail subject="{!SObject__c.id}"/>
</apex:page>

The apex:detail tag should have your "make callout" button on it.

The controller would just display page messages passed to it:

public class PageMessageController {
    private static final Map<String, ApexPages.Severity> severityMap = new Map<String, ApexPages.Severity>{ 'ERROR' => ApexPages.Severity.ERROR, 'CONFIRM' => ApexPages.Severity.CONFIRM };
    public PageMessageController() {
        String pageMessage = ApexPages.currentPage().getParameters().get('pageMessage');

        if (pageMessage != null) {
            ApexPages.add(new ApexPages.Message(ApexPages.Severity.ERROR, pageMessage));
        }
    }
}

The make callout button will call a different vf page:

<apex:page standardController="SObject__c" extensions="sampleControllerClass" action="{!makeCallout}">
</apex:page>

Controller:

public class sampleControllerClass {

    // snipped some code

    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){
            PageReference pr = new PageReference('/' + sr.Id);
            pr.getParameters().put('severity', 'ERROR');
            pr.getParameters().put('pageMessage', 'An error occurred');
            return pr;
        }

        try{
            // callout code here
        }Catch(Exception e){

        }
    }
}

I'm not sure what should happen if the make callout is successful, but you can just return back to the calling page in that instance with a different message saying the callout was successful with the appropriate severity.

  • Thanks I do not understand why there is first VF page pagemessage. I am currently using the standard salesforce detail page with a button on it. Where and how I should use the pagemessage VF page? – SfdcBat Apr 24 '17 at 4:31
  • Dont seem to follow on the implementation of it. how does apex:detail tag have the button there? And am I overrriding any standard button with the VF page. Thanks – SfdcBat Apr 24 '17 at 4:58
  • 1
    apex:detail merely renders what is on the page layout, including the buttons. you need replace the standard salesforce detail page with a vf page. The vf page is just the standard salesforce detail page with an extra apex:pageMessages tag at the top of the page so you can show messages from sent from other pages. – willard Apr 24 '17 at 15:57
  • Thanks. Do I replace the new button with the VF page with pageMessage? Is that correct? – SfdcBat Apr 24 '17 at 16:51
  • Hi, I replaced the view page with the pagemessage VF page but when I click edit and save again I still see the error message on the page? I want the user to click the button again to see any validation error. Thanks for your help. Appreciate it. – SfdcBat Apr 24 '17 at 17:59

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.