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
LloydSilverLloydSilver 

Error: Invalid initial expression type for field Account

Working on a test class.  I'm getting the error:

 

"Invalid initial expression type for field Account, expecting: SOBJECT:Account(or single row query result of that type). I'm new, and am at wit's end trying to figure this out. I'd appreciate some help. Thanks.

 

@isTest
private class TestOpportunityAgentStatus {

    static testMethod void myUnitTest() {
        
        Profile pf = [Select Id from Profile where Name = 'System Administrator'];
		
		User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;
        
        system.runAs(u){
        	
       	
        	Account xxxacc1 = new Account(Name = 'Test Company');
        	insert xxxacc1;


        	Contact xxxcon1 = new Contact(FirstName = 'Test', LastName = 'Guy', Account = xxxacc1.Id);
        	insert xxxcon1;
        	
  	    	
        	Opportunity xxxopp1 = new Opportunity(
        							Name = 'Test Company - New Opp',
        							Account = xxxacc1.Id,
        							CloseDate = Date.today(),
        							StageName = 'Prospecting'
        							);
        	insert xxxopp1;
        	
        	
        	OpportunityContactRole xxxocr1 = new OpportunityContactRole(
        							ContactID = xxxcon1.Id,
        							OpportunityId = xxxopp1.Id,
        							Role = 'Decision Maker'
        							);
        	insert xxxocr1;
        	
        	
        	
        }
        
        
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
liron169liron169
Use the same for the Opportunity object

Opportunity xxxopp1 = new Opportunity(
Name = 'Test Company - New Opp',
AccountId = xxxacc1.Id,
CloseDate = Date.today(),
StageName = 'Prospecting'
);

All Answers

liron169liron169

Hello,

Try to use:

Contact xxxcon1 = new Contact(FirstName = 'Test', LastName = 'Guy', AccountId = xxxacc1.Id);

LloydSilverLloydSilver

Thanks for the response. Unfortunately I got the same error message.

liron169liron169
Use the same for the Opportunity object

Opportunity xxxopp1 = new Opportunity(
Name = 'Test Company - New Opp',
AccountId = xxxacc1.Id,
CloseDate = Date.today(),
StageName = 'Prospecting'
);
This was selected as the best answer
LloydSilverLloydSilver

Perfect. Thanks!