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

Add Task Button to go to associated Opportunity

I would like to add a custom button on the task edit screen that does 2 things:


1. Save the task

2. If the Related To is an Opportunity, go to that Opportunity.


I believe I just need the javascript code that saves the task and sets the return URL. Any help would be appreciated.




I don't think you can do this without VisualForce. I'm fairly sure that all pages in "edit mode" cannot be overridden. You would have to build your own VF Page to achieve your goal.


My approach would be:


  1. Create Apex Controller SaveTaskController
  2. Create method mySaveMethod in SaveTaskController which returns a PageReference
  3. In mySaveMethod you first update the record which you get from the standard controller and then you return a PageReference pointing to the Opportunity. If anything fails (e.g. while updating the record) you should catch the error, add a Message to the page and return null (this will ensure that your users are given a meaningful error message)
  4. Create VF Page SaveTask with the fields needed (same as your original edit task page) and remember to add the pageMessages for displaying errors. The standard controller should be Task and extensions should be SaveTaskController
  5. Create save button and set action = "{!mySaveMethod}"
  6. Done (unless I forgot something ;-))

Of course, there are some holes to fill in the above recipe but it should give you an idea of what needs to be done. If anyone out there thinks I'm way off with my approach please speak up.




Søren Nødskov Hansen