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
chesschess 

invoke apex method on a custom button

I think this topic has been discussed before but I had a couple of questions. 

It seems like there are 2 approaches to doing this, one is to use javascript and the other is to use a dummy visualforce page that invokes the method we want executed and returns to the page we want to navigate.

Suppose on a standard detail page on an object there are other fields which the user has modified before clicking the custom button,

1. is the data in the fields saved to the object and the triggers fired in both methods (javascript and visualforce page).

2. If there are errors, how do we display the errors in the standard visualforce page? I have not yet tried the javascript approach but in the visualforce approach the errors seems to be raised in the dummy page.

 

Imran MohammedImran Mohammed

Suppose on a standard detail page on an object there are other fields which the user has modified before clicking the custom button,

1. is the data in the fields saved to the object and the triggers fired in both methods (javascript and visualforce page).

 

  • Yes, the data gets saved and triggers get fired if you have a DML operation in the code when button is clicked.

 

2. If there are errors, how do we display the errors in the standard visualforce page? I have not yet tried the javascript approach but in the visualforce approach the errors seems to be raised in the dummy page.

 

  • In Javascript, you can show an alert if any error is thrown due to a DML operation.

 

chesschess

In visualforce method, is there any way to capture errors on the detail page instead of the dummy page.

Even with alerts, would all the trigger validations etc which show errors specific to each field  be preserved?