+ Start a Discussion
dmchengdmcheng 

Best practice for calling Apex methods from custom button?

Hello.  I want to put a custom button on a standard detail page layout to execute Apex code, and I'm wondering if using VF page action attribute is a best practice.
 
Here are the details.  When the user clicks the custom button on the detail page, I need to check a couple field values in the record.  If they are valid, I want to insert 8 child records, popup a window saying insert is complete, then return to the original record detail page.  If the field values are not valid, then I want to popup a window saying the child records were not inserted, then return to the original record detail page.
 
So, there's no real user interaction here, just record creation where appropriate and some popup messages.
 
In the past, I have used a VF page with the action attribute to call a method in the VF controller. The constructor is empty and the VF page also has no content.  This worked fine, but I have read some old posts that say using action attribute this way is not best practice.
 
Is this still true?  If so, what's a better approach for this -- webservice methods, or using action function in the VF page?
 
Thanks
David
Avidev9Avidev9
Well it depends a lot. I would have preferred VF over webservice calls.
Because any webservice call that you do will be counted against the daily api governor limit. I dont think you need to make a webservice call here.

So looking at the UI perpective. We webservice call would have been done by staying in the detail page only, without going to VF page but again since you are creating child records you have to reload the page using js to make the child records appear in the related list.

Its more up to you .. what you opt for
dmchengdmcheng

I guess my underlying problem is: I am not displaying the VF page at all since all the activity happens without user intervention.  I do want to display popup windows to tell the user to say "Child records created successfully" or "Cannot create records because Field A is blank", etc, then always return to the original standard detail page.

 

It seems like I cannot use actionFunction because that requires commandButton onClick, and I don't have a commandButton in the VF page because I am using the page action attribute.

 

I wrote a Javascript custom button to do all the field validation and call an Apex webservice method and it works, but I would like to understand how to do this with VF.

 

Thanks

Avidev9Avidev9

Let me give you an example on how to show an alert. Even after using action attribut of the page.

 

<apex:page controller="Test_con" action="{!myMethod}">
    <apex:form>
      <apex:outputPanel rendered="{!NOT(ISNULL(myMsg))}"> 
        <script>
        alert('{!myMsg}');
        </script>
      </apex:outputPanel>
    </apex:form>
</apex:page>
        
        
        public class Test_con(){
          public String myMsg{get;set;}
          public void myMethod(){
          	myMsg='Sample Alert'
          }
        
        }

 Well in above example you can leave the myMsg variable null if you dont want to show any alert. Set the message according to your conditons

dmchengdmcheng

Thanks for your reply.  The problem is, when the controller method has finished its work, I want it to immediately redirect to the original detail page.

 

It seems to me that the VF page with its outputPanels will be displayed for only a fraction of a second, and then the original detail page will be displayed, so the user won't be able to see see the messages.

 

Here's what I want to happen:

User clicks button.  If Field A or Field B is empty, or child records already exist, then the user should see an alert window with a message, then return to the original detail page.  If Field A and Field B have values and there are existing child records, then I want to create 8 child records, popup an success message, and return to the original detail page.

Avidev9Avidev9

Why dont you give it a try ?
Until and unless user clicks ok button on the alert, page will not navigate back. This is default behaviour of an alert