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
Erik RodgersErik 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]);
 
Mohit Bansal6Mohit Bansal6
Hi Erik

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.
Erik RodgersErik Rodgers
But it did in fact error when I tried to assign it directly with this error Invalid initial expression type for field Account, expecting: Account (or single row query result of that type)
Amit Chaudhary 8Amit Chaudhary 8
Hi Erik,

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
Erik RodgersErik Rodgers
That is exactly what I was doing when I received this error. It is clearly designed to require it for some reason. For other object types I do direct assignment of the object ID with no problem.
Mohit Bansal6Mohit Bansal6
Erik

Use:
Opportunity newOpportunity = new Opportunity(
                                    CloseDate=date.Today(), 
                                    StageName='Quoted', 
                                    Name='Test Op', 
                                    AccountID = newAccount.Id]);

Regards
Mohit Bansal
Mohit Bansal6Mohit Bansal6
Is it working now Erik, or are you still facing issue over direct assignment?

Regards
Mohit Bansal
Erik RodgersErik Rodgers
It will not work using direct assignment. It will only work using the SOQL approach
CaptainObviousCaptainObvious
Try the code Amit Chaudhary and others have suggested, but remove the extra bracket after the  acc.id:

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 ;
Erik RodgersErik Rodgers
I have tried this code over and over and over. It does not work. Only the SOQL approach works in this case
Amit Chaudhary 8Amit Chaudhary 8
Hi Erik,

Please try below code
Account acc= new Account();
acc.Name ='Test Account';
insert acc;

Opportunity newOpportunity = new Opportunity();
newOpportunity.CloseDate=date.Today();
newOpportunity.StageName='Quoted';
newOpportunity.Name='Test Op';
newOpportunity.AccountID = acc.id;
insert newOpportunity;

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