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="{!}"/>
            <apex:outputField value="{!acc.AccountNumber}"/>
            <apex:outputField value="{!acc.Type}"/>
            <apex:outputField value="{!acc.Industry}"/>
        <apex:pageBlockSection title="Testing parameter" collapsible="false">
            Name is <b>{!parameterValue}</b>

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:


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

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

    • Ashish on November 5, 2020 at 2:31 pm
    • Reply

    Hi Ankus,

    I want to send account id in url while selecting account and click on continue button my page is redirecting proper but not getting account id in url showing null can you please help me where need to change in my below code.

    Vf Page

    .txtfield{width:65px; margin-bottom:5px;}

    border:1px solid #333;
    font-family:calibri; font-size:12px;
    table.bgmpop th {
    background:#6B95A0; color:#fff;
    border-right:1px solid #fff; text-align:center;
    font-family:calibri; font-size:14px;
    padding:3px; border:1px solid #fff;
    table.bgmpop td {
    color:#333; border:1px solid #ccc;
    font-family:calibri; font-size:12px;
    padding:3px; text-align:center;

    if( (typeof sforce != ‘undefined’) && (sforce != null) ) {
    var lkid = ‘{!lkid}’;

    var insertId = ‘{!}’;

    if(insertId!=” && lkid!=”){

    window.location = ‘/Account/’+lkid+’/view’;


    function confirmCancel() {
    var isCancel = confirm(“Are you sure you wish to cancel?”);
    if (isCancel) return true;

    return false;

    Apex code

    public with sharing class PurchaseOrderAccount {
    public Account acc{get;set;}
    public String targetField {get;set;}
    public String busAssoID {get;set;}
    public String retURL {get;set;}
    public String conid {get;set;}
    public String message{get;set;}
    public List contactList {get;set;}
    public List accountList {get;set;}
    public List options {get;set;}
    public string bl1 {get;set;}
    public boolean contactsection {get;set;}
    public boolean desabl {get;set;}
    public String lkid{get;set;}
    public Account selectedAccount{get;set;}

    public PurchaseOrderAccount()
    acc=new Account();


    public PurchaseOrderAccount(ApexPages.standardController controller)
    this.controller = controller;

    public PageReference Redirect(){
    String userId = UserInfo.getUserId();
    PageReference pageRef = new PageReference(‘/apex/OrderUploadPage_HO?id’+acc.Id);
    return pageRef;

    public pagereference cancel(){
    pagereference ref;
    ref = new pagereference(‘/’+lkid);
    return ref;
    private final ApexPages.StandardController controller;

    • manu on January 22, 2021 at 8:59 pm
    • Reply

    you manually need to put the id in the url,
    and for list of Sobjects, you will not get any url in the page.

    • simmy on February 8, 2022 at 3:38 pm
    • Reply

    public class AccountListctrl{
    public List selreclist;
    public list areamaster;
    // public String areaIds;

    public AccountListctrl(ApexPages.StandardsetController controller)

    selreclist = controller.getSelected();
    list areamaster= [select id,Date__c,Name from Area_Master__c where id in:selreclist];
    public void test1(){
    // list pjhead1= new list();
    PJP_Header__c pjhead = new PJP_Header__c();
    pjhead.Visit_Date__c =areamaster[0].Date__c ;
    // pjhead.No_of_Attached_Beats__c =;
    insert pjhead;
    // list save = database.insert (pjhead1,false);

    list pjpline1 = new list ();
    for( Area_Master__c armaster : areamaster ){
    // areaIds += armaster.Id + ‘,’; //build list of ids string concatenated with comma
    // system.debug(‘areaIds’+areaIds);

    PJP_LIne__c pjpline = new PJP_LIne__c();;
    pjpline.Visit_Date__c = armaster.Date__c;;
    // pjpline.PJP_Month__c= armaster.Month__c;

    insert PJPline1 ;

    public PageReference redirectToLC(){
    // String returnUrl = ‘/lightning/cmp/c__ProcessListView?Area_Master__c=’+areaIds;
    PageReference pgReturnPage = new PageReference(‘https://atctirepvtltd–’);
    return pgReturnPage;

    function selectAllCheckboxes(obj,receivedInputID){
    var inputCheckBox = document.getElementsByTagName(“input”);
    for(var i=0; i<inputCheckBox.length; i++){
    inputCheckBox[i].checked = obj.checked;

    this is my apex class in which I am getting error to insert data DML currently not allowed

Leave a Reply

Your email address will not be published.