+ Start a Discussion
DeptonDepton 

Variable does not exist: Account at line....

Hi,

 

I got this error trying to create this test class:

 

@isTest private with sharing class Opportunity
{
    static testMethod void Opportunity()
    {
        Test.startTest();
        List<Opportunity> lstOp= new List<Opportunity>();
        for(Integer iCount = 0; iCount < 20; iCount++)
        {
            Opportunity objOpp = new Opportunity();
            objOpp .Account= 'Test';
            objOpp .Name= '11111';
            objOpp .StageName= 'Visit';
            objOpp .CloseDate= Date.today();

            lstOp.Add(objOpp);
        }
        insert lstOp;
        Test.StopTest();
    }
   }

 

I have tried

 

Account

AccountName

Account__c

Account__r

Account__r.id

 

And always the same errror??

 

Thank you

 

 

Best Answer chosen by Admin (Salesforce Developers) 
steve456steve456

First give the test values for account

 

 

Then for opportunity test values give 

opp.accountid=acc.id

All Answers

steve456steve456

First give the test values for account

 

 

Then for opportunity test values give 

opp.accountid=acc.id

This was selected as the best answer
DeptonDepton

Ofcourse!!

 

Thank you!!

 

:)

abhishektandon2abhishektandon2

Opportunity record has a reference of Account, which means it has the ID of the Account Record not the name

 

First try to create a Account and then use its ID .

 

Account acc = new Account(Name='Test Account');

 

Insert acc;

 

 

for(Integer iCount = 0; iCount < 20; iCount++)
        {
            Opportunity objOpp = new Opportunity();
            objOpp .Account= acc.id;
            objOpp .Name= '11111';
            objOpp .StageName= 'Visit';
            objOpp .CloseDate= Date.today();

            lstOp.Add(objOpp);
        }

 

 

DeptonDepton
@isTest private with sharing class Opportunity
{
    static testMethod void Opportunity()
    {
        Test.startTest();
        
        List<Account> lstacc= new List<Account>(); 
        
        Account account= new Account();
        acccount. Name= 'Spain';
        lstacc.Add(account);
        insert lstacc;
        
        List<Opportunity> lstOp= new List<Opportunity>();
        for(Integer iCount = 0; iCount < 20; iCount++)
    {
            Opportunity objOpp = new Opportunity();
            objOpp .Account__r= acc.id;
            objOpp .Name= '11111';
            objOpp .StageName= 'Visit';
            objOpp .CloseDate= Date.today();

            lstOp.Add(objOpp);
        }
        insert lstOp;
        Test.StopTest();
    }
   }

 Error: Compile Error: Invalid type: acccount. at line 10 column 9

 

??

 

Thank you

steve456steve456

just give AccountId

 

dont give it as Account__r

DeptonDepton

Thank you,

 

I got the error now inthis line

 

        acccount. Name= 'Spain';

 

Error: Compile Error: Invalid type: acccount. at line 10 column 9

 


steve456steve456

Take ou the space

 

 

give

 

Account.Name

 

 

DeptonDepton
@isTest
 private with sharing class Opportunity
{
    static testMethod void Opportunity()
    {
        Test.startTest();
        
        List<Account> lstacc= new List<Account>(); 
        
        Account a= new Account();
        a.Name= 'Spain';
        lstacc.Add(a);
        insert lstacc;
        
        List<Opportunity> lstOp= new List<Opportunity>();
        for(Integer iCount = 0; iCount < 20; iCount++)
    {
            Opportunity objOpp = new Opportunity();
            
            objOpp.Account= a.id;
            objOpp.Name= '11111';
            objOpp.StageName= 'Visit';
            objOpp.CloseDate= Date.today();


            lstOp.Add(objOpp);
        }
        insert lstOp;
        Test.StopTest();
    }
   }

 Still getting an error in the line: objOpp.Account= a.id;

 

 

But I have modified the order and I got the error in all the objOpp

            objOpp.Account= a.id;
            objOpp.StageName= 'Visit';
            objOpp.CloseDate= Date.today();
            objOpp.Name= '11111';

 

Error: Compile Error: Variable does not exist:

bob_buzzardbob_buzzard

The Account field on an opportunity is a reference to an object.  You are providing the id, so you need to change the field to AccountId, e.g.

 

objOpp.AccountId= a.id;

 

DeptonDepton
@isTest
public class OpportunityTestSuite
{
    static testMethod void Opportunity()
    {
        Test.startTest();
        
        Account a= new Account();
        a.Name= 'Spain';
        insert a;
        
        List<Opportunity> lstOp= new List<Opportunity>();
        for(Integer iCount = 0; iCount < 20; iCount++)
        {
            Opportunity objOpp = new Opportunity();
            objOpp.AccountId = a.id;
            objOpp.StageName= 'Visit';
            objOpp.CloseDate= Date.today();
            objOpp.Name= '11111';
            objOpp.PreviousStage__c= 'Visit';

            lstOp.Add(objOpp);
        }
        insert lstOp;
        Test.StopTest();
    }
}

 Thank you Bob!!

I am not marking yours as the Solution because I did before!

 

And thank you all for the explanations!!:)

bob_buzzardbob_buzzard

Hah - no problem.  Mine wasn't exactly a huge contribution, just the final nudge.