You need to sign in to do that
Don't have an account?
Team 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!
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!
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
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
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.
Many Thanks!
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!!