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
PatcsPatcs 

Error in Query

Account acc = new Account(Name='Test1',Type='Prospect',Country_Listing__c='India',RecordTypeId='XXXXXXXXX');


insert(acc);

 

Account acc1=[select Name,id from Account where id=:acc.Id limit 1];

 

opportunity opp1=new opportunity(Account=acc1.Name,Name='test5',CloseDate=system.Today());

 

This is the Query written in test class.when i try to insert like this it throws an error

 

Error: Compile Error: Invalid initial expression type for field Account, expecting: SOBJECT:Account (or single row query result of that type) at line 14 column 25

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
hgarghgarg
Try Following : opportunity opp1=new opportunity(AccountId=acc1.Id ,Name='test5',CloseDa​te=system.Today()); also mind required fields of opportunity.

All Answers

kritinkritin

Use like this...

 

Account acc = new Account();

acc.Name='Test1';acc.Type='Prospect';

acc.Country_Listi​ng__c='India';

acc.RecordTypeId='XXXXXXXXX';

insert acc;

 

Account[] acc1=[select Name,id from Account where id=:acc.Id limit 1];

 

opportunity opp1=new opportunity(Account=acc1[0].Name,Name='test5',CloseDa​te=system.Today());

Ritesh AswaneyRitesh Aswaney

To the best of my knowledge, lookup fields are references and hold the Id, rather than the name !

In my opinion, it should read

 

opportunity opp1=new opportunity(Account=acc1.Id ,Name='test5',CloseDa​te=system.Today());

hgarghgarg
Try Following : opportunity opp1=new opportunity(AccountId=acc1.Id ,Name='test5',CloseDa​te=system.Today()); also mind required fields of opportunity.
This was selected as the best answer
PatcsPatcs

Thanks. It worked well.