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
Yadhagu LYadhagu L 

How can I get the parent ID from the related list

i have a custom button in campaign object related list of campaignmember object and I need to call vf page that will bring the campaign record id using the custom button.
Best Answer chosen by Yadhagu L
Arun Kumar 1141Arun Kumar 1141

Hi Yadhagu,

To create a custom button on the Campaign Member related list in the Campaign object that calls a Visualforce page and passes the Campaign record ID, you can follow these steps:

1. Create a Visualforce page:
  1. Go to Setup in Salesforce.
  2. Search for "Visualforce Pages" and select it.
  3. Click on "New" to create a new Visualforce page.
  4. Write the Visualforce page code that includes the logic you need. In this case, you want to access the Campaign record ID.

Here's an example of a basic Visualforce page that retrieves the Campaign ID:
<apex:page controller="CampaignMemberButtonController">
         <apex:form>
             <apex:commandButton action="{!redirectToPage}" value="Go to Campaign" />
         </apex:form>
</apex:page>
     
2. Create an Apex controller:

Create an Apex controller that retrieves the Campaign record ID and redirects to the desired page. Here's an example of an Apex controller:
public class CampaignMemberButtonController {
       public String campaignId { get; set; }
   
       public CampaignMemberButtonController() {
           campaignId = ApexPages.currentPage().getParameters().get('id');
       }
   
       public PageReference redirectToPage() {
           // Add logic or redirect to the desired page with the campaignId
           // Example: PageReference pageRef = new PageReference('/apex/YourPage?id=' + campaignId);
           // return pageRef;
           return null;
       }
   }

3. Customize the Campaign page layout:
  1. Go to the Campaign object's page layout that you want to modify.
  2. Edit the related list section for Campaign Members.
  3. Add a custom button and specify the Visualforce page you created as the content source.
  4. Save the page layout changes.

Now, when you navigate to the Campaign object and view the Campaign Member related list, you will see the custom button. Clicking on the button will invoke the Visualforce page, retrieve the Campaign record ID, and redirect to the desired page or perform any other desired logic. Make sure to customize the redirectToPage() method in the Apex controller to implement your specific logic or redirect to the appropriate page using the campaignId parameter.

Note: Ensure that the Visualforce page and Apex controller have the necessary access and visibility settings based on your organization's security requirements.

Hope this will be helpful.
Thanks!

All Answers

Arun Kumar 1141Arun Kumar 1141

Hi Yadhagu,

To create a custom button on the Campaign Member related list in the Campaign object that calls a Visualforce page and passes the Campaign record ID, you can follow these steps:

1. Create a Visualforce page:
  1. Go to Setup in Salesforce.
  2. Search for "Visualforce Pages" and select it.
  3. Click on "New" to create a new Visualforce page.
  4. Write the Visualforce page code that includes the logic you need. In this case, you want to access the Campaign record ID.

Here's an example of a basic Visualforce page that retrieves the Campaign ID:
<apex:page controller="CampaignMemberButtonController">
         <apex:form>
             <apex:commandButton action="{!redirectToPage}" value="Go to Campaign" />
         </apex:form>
</apex:page>
     
2. Create an Apex controller:

Create an Apex controller that retrieves the Campaign record ID and redirects to the desired page. Here's an example of an Apex controller:
public class CampaignMemberButtonController {
       public String campaignId { get; set; }
   
       public CampaignMemberButtonController() {
           campaignId = ApexPages.currentPage().getParameters().get('id');
       }
   
       public PageReference redirectToPage() {
           // Add logic or redirect to the desired page with the campaignId
           // Example: PageReference pageRef = new PageReference('/apex/YourPage?id=' + campaignId);
           // return pageRef;
           return null;
       }
   }

3. Customize the Campaign page layout:
  1. Go to the Campaign object's page layout that you want to modify.
  2. Edit the related list section for Campaign Members.
  3. Add a custom button and specify the Visualforce page you created as the content source.
  4. Save the page layout changes.

Now, when you navigate to the Campaign object and view the Campaign Member related list, you will see the custom button. Clicking on the button will invoke the Visualforce page, retrieve the Campaign record ID, and redirect to the desired page or perform any other desired logic. Make sure to customize the redirectToPage() method in the Apex controller to implement your specific logic or redirect to the appropriate page using the campaignId parameter.

Note: Ensure that the Visualforce page and Apex controller have the necessary access and visibility settings based on your organization's security requirements.

Hope this will be helpful.
Thanks!
This was selected as the best answer
SubratSubrat (Salesforce Developers) 
Hello Yadhagu ,

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

Also please refer these discussions for further understanding :
https://salesforce.stackexchange.com/questions/334967/get-the-record-id-from-the-related-list

https://stackoverflow.com/questions/71395168/how-to-get-parent-id-on-related-list-in-salesforce


If this helps , please mark this as Best Answer.
Thank you.