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
OlbrestOlbrest 

Got error Invalid initial expression type for field Account.

Code:
Account ac = [select Name from Account where Id= :si.Myne_Account__c];
Opportunity s = new Opportunity(Name = "Hello", Account = ac.Name);
insert s;

I Got Error:
 

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

Why ?
JimRaeJimRae
I see a few potential issues there.  First, the ac object you create only supports the return of one row from your select.  If there is a possibility it might return more, you need to either use LIMIT 1 or set the ac object as an array to  be processed.  If you are certain it can only retun one result, you should be OK.
Second, what is the Account you are setting as a parameter in your new opportunity creation?  If that is the account that they Opportunity is related to, you should retrieve the id in your SOQL and use the whatid, and set it equal to the id of the account being returned.

Code:
Account ac = [select Id, Name from Account where Id= :si.Myne_Account__c];
Opportunity s = new Opportunity(Name = "Hello", Whatid= ac.Id);
insert s;

if the Account is a custom field on your opportunity, make sure the type you are setting it equal to is the same as the value you are using.  (IE text to text, ID to ID, etc.)