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.
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'); } }
11 comments
Skip to comment form
where is parameter named nameParam being set in your code
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
Where is nameparam used?
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.
Author
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.
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.
Author
Can you please post your code?
Thanks man for this spirit of sharing i m thankful !
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;}
table.bgmpop{
border-collapse:collapse;
border:1px solid #333;
font-family:calibri; font-size:12px;
}
table.bgmpop th {
border-collapse:collapse;
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 = ‘{!acc.id}’;
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);
pageRef.getParameters().put(‘UserId’,userId);
pageRef.getParameters().put(‘accid’,acc.Id);
pageRef.setRedirect(true);
return pageRef;
}
public pagereference cancel(){
pagereference ref;
if(retURL!=null){
if(lkid!=null)
{
ref = new pagereference(‘/’+lkid);
}
}
return ref;
}
private final ApexPages.StandardController controller;
}
you manually need to put the id in the url,
and for list of Sobjects, you will not get any url in the page.
public class AccountListctrl{
public List selreclist;
public list areamaster;
// public String areaIds;
public AccountListctrl(ApexPages.StandardsetController controller)
{
selreclist = controller.getSelected();
//areaIds=”;
list areamaster= [select id,Date__c,Name from Area_Master__c where id in:selreclist];
system.debug(‘test’);
test1();
}
public void test1(){
// list pjhead1= new list();
PJP_Header__c pjhead = new PJP_Header__c();
pjhead.Visit_Date__c =areamaster[0].Date__c ;
//pjhead1.add(pjhead);
// 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.PJP_Header__c=pjhead.id;
pjpline.Visit_Date__c = armaster.Date__c;
pjpline.Id=armaster.id;
// pjpline.PJP_Month__c= armaster.Month__c;
pjpline1.add(pjpline);
}
insert PJPline1 ;
redirectToLC();
}
public PageReference redirectToLC(){
// String returnUrl = ‘/lightning/cmp/c__ProcessListView?Area_Master__c=’+areaIds;
PageReference pgReturnPage = new PageReference(‘https://atctirepvtltd–dev.lightning.force.com/lightning/o/PJP_Header__c/list?filterName=00B0p000002hFkjEAE’);
pgReturnPage.setRedirect(true);
return pgReturnPage;
}
}
vf=========
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName(“input”);
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
}
}
}
this is my apex class in which I am getting error to insert data DML currently not allowed