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
DeptonDepton 

error un test class?

 

Hi guys,

 

 

@isTest
private with sharing class Loan
{
    static testMethod void Loan()
    {
        Test.startTest();
        
// set up account
        Account acc = new Account();
        acc.Name = 'Acme Account Test';
        acc.Account_Type__c= 'Audio';
        acc.Strategic_Segmentation__c= 'C - Small';
        acc.Country__c= 'Spain';
        insert acc;
        
        List<Contract> lstContract= new List<Contract>();
        for(Integer iCount = 0; iCount < 20; iCount++)
        {
            Contract objContr = new Contract();
            objContr.Account= acc.id;
            objContr.StartDate= Date.today();
            objContr.ContractTerm= '11111';
            objContr.CurrencyIsoCode= 'EUR';
            objContr.Status= 'Borrador';

            lstContract.Add(objContr);
        }
        insert lstContract;
        Test.StopTest();
    }
}

 

Error: Compile Error: Illegal assignment from Id to SOBJECT:Account at line 20 column 13

 

Any ideas??

Best Answer chosen by Admin (Salesforce Developers) 
SidharthSidharth

Try adding this :

 

objContr.AccountId= acc.Id;

 

All Answers

SidharthSidharth

Try:                  objContr.Account = acc;

instead of :objContr.Account = acc;.id

DeptonDepton

Method NameTotal Time (ms)MessageStack Trace

AccountTest.AccountTest461.0System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AccountId]: [AccountId]Class.AccountTest.AccountTest: line 44, column 9 External entry point

 

 

Got this error with this code??

@isTest
private with sharing class AccountTest
{
    static testMethod void AccountTest()
    {
        Test.startTest();
        
        
     // set up Country
        List<Country__c> lstCountry= new List<Country__c>(); 
        
        Country__c count = new Country__c();
        count.Name= 'Spain';
        count.CurrencyIsoCode= 'EUR';
        lstCountry.Add(count);

        insert lstCountry;
        
    // set up account
        List<Account> lstAcc= new List<Account>();

        Account acc = new Account();
        acc.Name= 'Acme Account Test';
        acc.Account_Type__c= 'Audio';
        acc.Strategic_Segmentation__c= 'C - Small';
        acc.Country__c= count.id;
        lstAcc.Add(acc);
        
        insert lstAcc;
        
        List<Contract> lstContract= new List<Contract>();
        for(Integer iCount = 0; iCount < 20; iCount++)
        {
            Contract objContr = new Contract();
            objContr.Account= acc;
            objContr.Name= 'Borrador';
            objContr.StartDate= Date.today();
            objContr.ContractTerm= 1;
            objContr.CurrencyIsoCode= 'EUR';
            objContr.Status= 'Borrador';

            lstContract.Add(objContr);
        }
        insert lstContract;
        Test.StopTest();
    }
}

 

 

I have tried what you recommended before but got an error with the Country ID, so I have added a few lines to it, but not sure if it is correct!!

 

Thanks for your help!

SidharthSidharth

Try adding this :

 

objContr.AccountId= acc.Id;

 

This was selected as the best answer
DeptonDepton

You ROCK!!!!!!

 

:))

 

Thanks!!