You need to sign in to do that
Don't have an account?
Erik Rodgers
why does it take soql top populate account field on a new opportunity
While writing a test class for a trigger handler I encountered a situation (while creating a new opportunity record) where the Account field on the opportunity, which is a lookup to the Account parent record, required me to use SOQL in the constructor rather than just assigning the account ID directly. Can anyone please explain why this is necessary for some lookups and not others? Here is the code that worked:
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
Account=[SELECT Id FROM Account WHERE Id= :newAccount.Id]);
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
Account=[SELECT Id FROM Account WHERE Id= :newAccount.Id]);
You could have directly assigned it. It doesn't mandate you to write query for it.
Below is the modify statement:
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
AccountID = :newAccount.Id]);
In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Please try below code :-
Account acc= new Account();
acc.Name ='Test Account';
insert acc;
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
AccountID = acc.id ] );
Insert newOpportunity ;
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Use:
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
AccountID = newAccount.Id]);
Regards
Mohit Bansal
Regards
Mohit Bansal
Account acc= new Account();
acc.Name ='Test Account';
insert acc;
Opportunity newOpportunity = new Opportunity(
CloseDate=date.Today(),
StageName='Quoted',
Name='Test Op',
AccountID = acc.id );
Insert newOpportunity ;
Please try below code
If Still above code will not work Then after adding above code share your full code will error log (please provide full debug log).
Please let us know if this will help you.
Thanks
Amit Chaudhary