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
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN 

get id of record and assign to variable in controller on click of button on detail page

Hi all,

I have requirement where i have to get the id of a record and assign it to a variable in my extension on clicking a custom button i added in the detail page of object.

Please let me know how to achieve this. If sample code is available that will be great.

Best Regards,
Mohammad Yaseen
Best Answer chosen by SHAIK MOHAMMAD YASEEN
Ashutosh Tripathi 34Ashutosh Tripathi 34
Hi Shaik,

apexpages.currentpage().getparameters().get(‘id’) can be used to get current record id or other url parameters in apex code.
If we have a button on detail page overridden by visualforce page and once the button is pressed you require the id(or the other field values of that record) of the record from whose detail page the button was clicked. For this requirement also we can use ApexPages.CurrentPage().getparameters().get(‘id’) to get other parameters. So you need to call this on the button click.

In the following example a custom extension to get current record id and one parameter with name nameParam. Then using one visualforce page to display current record id and all fields related to that record id by querying using SOQL query and also displaying value of one more parameter with name nameParam. In this way we can get value of any parameter in Apex code.

Vf Page Code:
===========
<apex:page standardController="Account" extensions="CurrentRecordIdDemoController">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Current account record Id is : {!currentRecordId}" collapsible="false">
            <apex:outputField value="{!acc.name}"/>
            <apex:outputField value="{!acc.AccountNumber}"/>
            <apex:outputField value="{!acc.Type}"/>
            <apex:outputField value="{!acc.Industry}"/>
        </apex:pageBlockSection>
         
        <apex:pageBlockSection title="Testing parameter" collapsible="false">
            Name is <b>{!parameterValue}</b>
        </apex:pageBlockSection>
         
    </apex:pageBlock>
  </apex:form>
</apex:page>

Class Code:
=========
public class CurrentRecordIdDemoController{
public String currentRecordId {get;set;}
public String parameterValue {get;set;}
public Account acc{get;set;}
 
    public CurrentRecordIdDemoController(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        acc = [select id ,name, AccountNumber, Type, Industry from Account where id =: currentRecordId ];
        parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');
    }
}

I hope it helps.

Please mark it as "Best Answer" right below the comment if it gives you idea regarding your solution, so that it will help others if they have similar issues in future.