get current record id salesforce

get current record id salesforce

apexpages.currentpage().getparameters().get(‘id’) can be used to get current record id or other url parameters in apex code.

Many times we have requirement to get current record id or url parameters of visualforce page in apex code.

Many times we require the current record id in controller. For example we are redirecting from one visualforce page to another visualforce page using apex code and we have also set some parameters for second visualforce page. Then, in that case we can use apexpages.currentpage().getparameters().get(‘id’) to get parameter with id name or any other name.

One more example 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.

In the following example we will use a custom extension to get current record id and one parameter with name nameParam. Then we are 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.

Click for Demo

get current record id salesforce

Visualforce Page:

<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>

Apex 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');
    }
}

Permanent link to this article: https://www.sfdcpoint.com/salesforce/get-current-record-id-salesforce/

8 comments

Skip to comment form

    • girish on December 9, 2014 at 9:13 am
    • Reply

    where is parameter named nameParam being set in your code

    1. We are not setting nameParam in code. But we are getting it in code. Also we are passing this in URL
      You can check following link for output of above visualforce page
      http://demopoint-developer-edition.ap1.force.com/apex/currentRecordIdDemo?id=0019000000nPeLS&nameParam=SFDCPOINT

    • rvpadman on July 24, 2015 at 2:03 am
    • Reply

    Where is nameparam used?

    • neha on April 10, 2020 at 9:08 am
    • Reply

    I have copy your code for apex and VF.
    I am not able to get the record id .My visual force page does not display any value .
    error is showing :List has no rows for assignment to SObject
    An unexpected error has occurred. Your development organization has been notified.

    I solved the above error by putting the account in list. still my value is not displaying.

    Please let me know what is the problem.

  1. Please make sure that you are passing correct parameter in url. Your url should be like this
    instance url + /apex/currentRecordIdDemo?id=0019000000nPeLS&nameParam=SFDCPOINT
    Here you need to replace 0019000000nPeLS with some account id that exist in your org.

    Please let me know if you still face any issue.

      • neha on April 11, 2020 at 1:48 pm
      • Reply

      Thank for reply Ankush. Now it is working fine . I have one more concern .As we are not using the list return type for account then we are facing this issue “List has no rows for assignment to SObject” .
      when i used the class variable name in vf page error is displaying “Could not resolve the entity from value binding ‘{!acc.Type}’. can only be used with SObjects, or objects that are Visualforce field component resolvable.”
      and to solve the above issue i have changed my code

      For me it’s little bit confusing . It will be a great help if you tell the reason behind this. Thanks a lot.

      1. Can you please post your code?

    • Saad on May 20, 2020 at 8:55 pm
    • Reply

    Thanks man for this spirit of sharing i m thankful !

Leave a Reply

Your email address will not be published.