function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Team WorksTeam Works 

How can i invoke apex code from a button?

Hello All,

How can i invoke apex code from a button, where using the apex i should be able to aggregate the fields from the child records.?

Please suggest

Thanks!
Best Answer chosen by Team Works
GlynAGlynA
Namo,

Create a VF page that uses the standard controller for your object, and a controller extension that you have written.
Create a custom button whose Content Source is "VisualForce Page" and select your VF page.
This will open your VF page and run your controller extension constructor.  You can include an action property in the page that will run on load.  The action can return a PageReference to the url given as the "returl" parameter.  This should navigate back to the original record detail page.

Alternatively, you can create an OnClick JavaScript button that sets a custom checkbox field on the record and updates it.  Write a "Before" trigger that detects the checked box and computes your aggregations and writes them to the record, then clears the checkbox.  This method has the advantage that it doesn't navigate away from the detail page.

I have seen both of these methods used in practice.  Let me know if you have any questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator

All Answers

GlynAGlynA
Namo,

Create a VF page that uses the standard controller for your object, and a controller extension that you have written.
Create a custom button whose Content Source is "VisualForce Page" and select your VF page.
This will open your VF page and run your controller extension constructor.  You can include an action property in the page that will run on load.  The action can return a PageReference to the url given as the "returl" parameter.  This should navigate back to the original record detail page.

Alternatively, you can create an OnClick JavaScript button that sets a custom checkbox field on the record and updates it.  Write a "Before" trigger that detects the checked box and computes your aggregations and writes them to the record, then clears the checkbox.  This method has the advantage that it doesn't navigate away from the detail page.

I have seen both of these methods used in practice.  Let me know if you have any questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
This was selected as the best answer
DevADSDevADS
Hello Namo,

You can use ajax toolkit for this. You can refer my blog as follow  for the same :
http://salesforce-atom.blogspot.in/2014/01/invoke-apex-code-on-custom-button-click.html

Let me know If you face any issues.
Team WorksTeam Works
Thanks for your feed back guys. I am currently working on another requirement and in this case i need a button on Account Object. This button will just display 20 read only fields from the same Account sObject. How do i pass the ID of the current Account sObject i am on when i click the button. Also when i click close button on the VF page i stay on the Account record where i clicked the button.

Many Thanks!
Team WorksTeam Works
Hi Guys,

I have done this now and i observe that the ID of the current account is automatically passed to the visual force page.
Thanks Amit and GlynA for contribution and help.Great!!