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
ethanoneethanone 

What is the best way to get current record id

I'm new to VF development. I'm trying to create a button that makes a new opportunity from an existing opportunity and uses some of the values from the old opportunity and changes others. Similar to a clone button but with control over what goes into each field.

I'm using a controller extension for this operation.

I've seen 2 examples of how to get the Id of the current record I'm starting with and I'm trying to determine method is best (or at least the pros/cons of each).  Both methods are in the controller constructor.

Method #1: Using controller.getRecord()
public class myExtension {

    Opportunity currentRecord;
    
    public myExtension(ApexPages.StandardController controller) {
        this.currentRecord = (Opportunity)controller.getRecord();
        currentRecord = [SELECT Id, Name, Amount FROM Opportunity WHERE Id = :currentRecord.Id];
    }

    public Opportunity getcurrentRecord(){
        return currentRecord;
    }
}

Method #2: using Page Parameters
public class myExtension {

    Opportunity currentRecord;
    
    public myExtension(ApexPages.StandardController controller) {
        currentRecord = [SELECT Id, Name, Amount FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Opportunity getcurrentRecord(){
        return currentRecord;
    }
}

Alternatively, if there is another method better than either of these, please let me know.
Best Answer chosen by ethanone
SFDC_DevloperSFDC_Devloper
Hi ,

  Recommended to go for second one...
public class myExtension {

    public Opportunity currentRecord{get; set;}
    
    public myExtension(ApexPages.StandardController controller) {
        currentRecord = [SELECT Id, Name, Amount FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

}

Thanks,
Rockzz

All Answers

Anil KamisettyAnil Kamisetty
The Second approach is preferred over the first one, we have used it extensively.
SFDC_DevloperSFDC_Devloper
Hi ,

  Recommended to go for second one...
public class myExtension {

    public Opportunity currentRecord{get; set;}
    
    public myExtension(ApexPages.StandardController controller) {
        currentRecord = [SELECT Id, Name, Amount FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

}

Thanks,
Rockzz

This was selected as the best answer
ethanoneethanone
Thanks for all your help!
Michaela ChrysostomouMichaela Chrysostomou
hi,
Can you please provide us with the test class ?  I get 71% code covarage and I need 75%!! 
------------------------------------------------------------------------
public class TestingFlag{


   public Testimonies__c objt {get; set;}
   public String currentRecordId {get; set;}
    
    public TestingFlag(ApexPages.StandardController controller){
           currentRecordId =  ApexPages.CurrentPage().getparameters().get('id');
           objt = [Select Id, Name, Gender__c, Approved_Story__c,Country_v__c,Contact_Country__c, Age__c,Approved_Name__c, Flag__c From Testimonies__c Where Id = :currentRecordId ];
    }
    
    public Testimonies__c getcurrentRecord(){
            return objt;

        }
        
}

---------------------------------------------------------------------------

@isTest
private class ControllerTest{

PageReference pageRef = Page.TestingFlag;
   
    
    static testMethod void myUnitTest() 
    {
       Testimonies__c obj= new Testimonies__c(English_Translation__c='saf',Audience_Identifier__c='Pars',Country_v__c= 'Cyprus',Status__c='Decide');
        insert obj;

     PageReference pageRef = Page.TestingFlag;
     pageRef.getParameters().put('Id', String.valueOf(obj.Id));
     Test.setCurrentPage(pageRef);
   
   
    ApexPages.StandardController sc = new ApexPages.StandardController(obj);
     TestingFlag testAccPlan = new TestingFlag(sc);
     testAccPlan.currentRecordId= obj.Id; 
    }
    
         
}
Anusha Bansal 9Anusha Bansal 9
@ethanone: what is the code for visual force page?