You need to sign in to do that
Don't have an account?
Jev
When does Opportunity.Id get established? Does this have something to do with me doing this in a @isTest test class?
Thanks,
-Jev
When does the Id of a new Opportunity get populated?
I'm creating a new Opportunity() and I want to get the Id of this new opportuinity, but it always appears to be NULL.
static testMethod void testLaunchSuperReviewer() {
//Create a new Opportunity for testing
Opportunity testOp = new Opportunity(CloseDate = System.today(), Name = 'Unit Test Opportunity');
try {
insert testOp;
} catch(System.DMLException e){
System.debug('Failed to insert test opportunity');
}
System.debug('Op ID: ' + testOp.id); //WHY IS THIS ALWAYS NULL???
When does Opportunity.Id get established? Does this have something to do with me doing this in a @isTest test class?
Thanks,
-Jev
There are 2 ways you can get his id, which is generated after the insert has completed.
You could create a string to capture it, and populate it inside the try block, or you could requery for the opportunity after the try catch, and it would be populated.
I would recommend the first method, like this:
static testMethod void testLaunchSuperReviewer() { String tOppid; //Create a new Opportunity for testing Opportunity testOp = new Opportunity(CloseDate = System.today(), Name = 'Unit Test Opportunity'); try { insert testOp; tOppid = testOp.id; } catch(System.DMLException e){ System.debug('Failed to insert test opportunity'); } System.debug('Op ID: ' + tOppid); //SHOULDN't BE NULL!! }
All Answers
There are 2 ways you can get his id, which is generated after the insert has completed.
You could create a string to capture it, and populate it inside the try block, or you could requery for the opportunity after the try catch, and it would be populated.
I would recommend the first method, like this:
static testMethod void testLaunchSuperReviewer() { String tOppid; //Create a new Opportunity for testing Opportunity testOp = new Opportunity(CloseDate = System.today(), Name = 'Unit Test Opportunity'); try { insert testOp; tOppid = testOp.id; } catch(System.DMLException e){ System.debug('Failed to insert test opportunity'); } System.debug('Op ID: ' + tOppid); //SHOULDN't BE NULL!! }
Thanks Jim!
This worked after I added StageName when creating the Opportunity instance. Your solution was bang on. Thnak you!
Opportunity testOp = new Opportunity(CloseDate = System.today(), Name = 'Unit Test Opportunity', StageName = 'Prospecting');
-Jev