+ Start a Discussion
uuuu 

Access current record ID

Hii Guys,
Please help me! I am new to apex code.
I want to access the current Record ID.
and assign that ID to another variable in apex class.

 
public class SampleCode{
    Public Offers_Appraisals__c currentRecord{get; set;}
 
        public SampleCode(ApexPages.StandardController controller) {
        currentRecord = [SELECT ID FROM Offers_Appraisals__c
                                               WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        } 

public static void HRSalarySheet()
    {
        List<Offers_Appraisals__c> OfferList=new List<Offers_Appraisals__c>();
        OfferList=[SELECT ID,Name,
                                              From_Date__c,To_date__c
                                              FROM Offers_Appraisals__c
                                             WHERE ID=:currentRecord.ID];
    }


When i use the above code  it shows error msg 
Variable Does not exist currentRecod.
Please Help me to solve error.
Where my code is going wrong?

Thanks you
Best Answer chosen by uu
AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

There seems to be nothing wrong with the usage of your instance variable (currentRecord)

To verify, I tried the following sample code snippet in my local org and it worked just fine
List<contact> conList = new List<Contact>();
Public Account currentRecord{get; set;}
currentRecord  =  [Select Id from Account limit 1]; 
conList = [select id from contact where AccountId =:currentRecord.ID];
System.debug('conList is'+ conList);
However, I believe the variable is inaccessible because your method, being static, can't see the instance variables. Removing the "static" keyword should allow the variable to be visible. Came across this:

https://salesforce.stackexchange.com/questions/194119/variable-does-not-exist-even-though-declared-at-top-of-apex-file

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

There seems to be nothing wrong with the usage of your instance variable (currentRecord)

To verify, I tried the following sample code snippet in my local org and it worked just fine
List<contact> conList = new List<Contact>();
Public Account currentRecord{get; set;}
currentRecord  =  [Select Id from Account limit 1]; 
conList = [select id from contact where AccountId =:currentRecord.ID];
System.debug('conList is'+ conList);
However, I believe the variable is inaccessible because your method, being static, can't see the instance variables. Removing the "static" keyword should allow the variable to be visible. Came across this:

https://salesforce.stackexchange.com/questions/194119/variable-does-not-exist-even-though-declared-at-top-of-apex-file

Anudeep
This was selected as the best answer
uuuu
Hii Anudeep,
Thanks for reply.
It resolve my error.

So basically i want to ask you to get the current page record id there is no need write constructor and ApexPages.currentPage().getParameters().get('id')].

By using above code you have written, does it acccess the current record ID?

Thank you.
AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

Yes, it does

I am able to see the results for System.debug('conList is'+ conList); in the code I executed in the execute anonymous window

Thanks, 
Anudeep
uuuu
Hii Anudeep
Thank you .It worked.