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
Cheryl RivardCheryl Rivard 

Create a custom button to open a related record

I need to create a button that is on the Account Page that opens a related (custom object) record where the Account ID is the same as the related record. There is only suppose to be one related record for each account.   I'm stumped on how to do it.  Thanks
Best Answer chosen by Cheryl Rivard
Cory CowgillCory Cowgill
If this is a Master-Detail relationship you could use a Cross-Object workflow to copy the Child Records ID into a Text Field on the Account. Then you could use a Hyperlink Formula based on the Text Field to generate a hyperlink that will work and put on Page Layout.


 

All Answers

Balaji BondarBalaji Bondar
Hi Cheryl,

Please use below approach to create button and to call the apex code:http://blog.shivanathd.com/2014/07/call-apex-class-from-custom-button-salesforce.html

In Apex code , query your custom object wrt to the accountid and rediect user to the custom object record using pagereference.


Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Cory CowgillCory Cowgill
If the records are linked via a Lookup relationship as you say you should be able to simply add the related list to the page layout and avoid coding altogether.

Have you treid simply updating the page layout to include the related list for the custom object? This will save you from writing Apex code which will require unit test coverage, more maintenance expenses, etc. 
Cheryl RivardCheryl Rivard
Well, I did have a button working that opened the related list (the sponsor didn't want the related list to show on the Account page).  But now I've told that there is only one of this type of related record (ever) and that it needs to open up to the related record's detail page view when the user clicks the button.  It's been years since I've done javascript.  
Cory CowgillCory Cowgill
If this is a Master-Detail relationship you could use a Cross-Object workflow to copy the Child Records ID into a Text Field on the Account. Then you could use a Hyperlink Formula based on the Text Field to generate a hyperlink that will work and put on Page Layout.


 
This was selected as the best answer
Cory CowgillCory Cowgill
In general I would avoid using JavaScript Button / Hacks if at all possible for such a simple request. You're adding techincal debt to your implementation that will cost more money long term for something that is very simply solved by either a Related List or a Formula Field. Hope that helps.
Cheryl RivardCheryl Rivard
Thanks for the reality check!  Like the hyperlink formula idea.