You need to sign in to do that
Don't have an account?
ethanone
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()
Method #2: using Page Parameters
Alternatively, if there is another method better than either of these, please let me know.
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.
Recommended to go for second one...
Thanks,
Rockzz
All Answers
Recommended to go for second one...
Thanks,
Rockzz
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;
}
}