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
Madhu007Madhu007 

How to Get the parent id from related list button

Hi,

I have a related list button on detail page. I have wriiten some java script code on the button. I want to gett the parent id on this button. Can you please tell me how can i get the id?
Best Answer chosen by Madhu007
Leslie  KismartoniLeslie Kismartoni
If you're using the onclick javascript for a detail page custom button, use the field type for the parent.

E.g. if I'm trying to put a button on the Contact related list off the Account page, I'd do this:
alert('{!Account.Id}');

The account.id referenced will be from the parent object.

User-added image

User-added image

All Answers

MagulanDuraipandianMagulanDuraipandian
Check this - http://www.infallibletechie.com/2015/04/hyperlink-field-to-open-visualforce.html
Leslie  KismartoniLeslie Kismartoni
If you're using the onclick javascript for a detail page custom button, use the field type for the parent.

E.g. if I'm trying to put a button on the Contact related list off the Account page, I'd do this:
alert('{!Account.Id}');

The account.id referenced will be from the parent object.

User-added image

User-added image
This was selected as the best answer
Leslie  KismartoniLeslie Kismartoni
Here's the screen shot of it working... 
User-added image
Madhu007Madhu007
Thanks Leslie Kismartoni. I got it.
Paul DevenportPaul Devenport
What if you are wanting to create a generic object that can be used on any partent object as a related list.  So you don't know what type the parent is when you are creating it...  How can you get something like <parent>.Id ?  Also how could you obtain the parent's object type?
Paul DevenportPaul Devenport
Here is a hack to get the generic parent id...(you do have to list them all in your code in advance, which is not too big of a deal)
I tested it with three parents (Contact, CONTRACT, Opportunity), all having a Lookup relationship from the Attachment.
 
Here is what I did:
 
On the customer “Add Attachment” button, added this URL:  {!URLFOR("/apex/PD_CustomAttachment?id=" +Contact.Id + Contract.Id + Opportunity.Id +
"&obj1=" +  Contact.Id + "&obj2=" + Contract.Id + "&obj3=" + Opportunity.Id )}
 
In the controller, check to see which url variable is set and use it as a flag to determine the parent object type:
public PD_CustomAttachmentController(){
        String contactId = ApexPages.currentPage().getParameters().get('obj1');
        String CONTRACTId = ApexPages.currentPage().getParameters().get('obj2');
        String opportunityId = ApexPages.currentPage().getParameters().get('obj3');
        if(contactId != ''){
            parentId = contactId;
            parentType = 1;
        }else if(CONTRACTId != ''){
            parentId = CONTRACTId;
            parentType = 2;
        }else if(opportunityId !=''){
            parentId = opportunityId;
           parentType = 3;
        }       
    }
 
Then in the code that saves the attachment, add the relationship reference from attachment to correct parent variable:
private Database.SaveResult saveCustomAttachment(){
        Custom_Attachment__c obj = new Custom_Attachment__c();
        if(parentType == 1){
            obj.contact__c = parentId;
        }else if(parentType == 2){
            obj.CONTRACT__c = parentId;
        }else if(parentType == 3){
            obj.opportunity__c = parentId;
        }
       
 
In visualforce page, just use my controller instead of standardController/extentioncontroller.
<apex:page   controller="PD_CustomAttachmentController">