+ Start a Discussion
Akshat Tiwari 2Akshat Tiwari 2 

Please help me with parent to child test class

I am trying to update the AnnualRevenue of Account into Opportunity amount and it is working for update.....During testing I am getting error System Variable does not exist: Amount
Here is the code
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, opp.Amount);
        
            
        
        
    }

}
What modifications can I make in the code?
 
Best Answer chosen by Akshat Tiwari 2
mukesh guptamukesh gupta
Hi Akshat,

Please use below code:-
 
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
		opp1.AccountId = acc.Id;
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
		opp1.Amount = 2000;
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, opp.Amount);
        
            
        
        
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

All Answers

Akshat Tiwari 2Akshat Tiwari 2
Now I am getting error  List has no rows for assignment to SObject for the query List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity                             where AccountId =:acc.Id];
Bad Bunny MerchBad Bunny Merch
There list has no error, (https://badbunnymerch.biz/) please check the query List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity                             where AccountId =:acc.Id];
mukesh guptamukesh gupta
Hi Akshat,

Please use below code:-
 
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
		opp1.AccountId = acc.Id;
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
		opp1.Amount = 2000;
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, opp.Amount);
        
            
        
        
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
This was selected as the best answer
Maharajan CMaharajan C
Hi Akshat,

The error is coming due to the below line: 

system.assertEquals(50000, opp.Amount);  ====>    opp[0].Amount

Opp is a List so use the opp[0].
List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
system.assertEquals(50000, opp[0].Amount);

Thanks,
Maharajan.C
Akshat Tiwari 2Akshat Tiwari 2
Thank you for responding @Maharajan C, @
Bad Bunny Merch and @mukesh gupta I made 'system.assertEquals(50000, opp[0].Amount)'; and 'opp1.AccountId = acc.Id;' these modifications on the code and its working

Can I get some more clarity on why we are using opp[0] and not any other  integer like opp[1] and when to use this?
 
mukesh guptamukesh gupta
Hi Akshat,
 

Because we are using List that's have index 0,1,2 etc.
 
 List<Opportunity> oppList = [Select AccountId, id, Amount from Opportunity where AccountId =:acc.Id]; system.assertEquals(50000, oppList[0].Amount);
we can access only 0 index because Account have only one opportunity that's why we are using [0],   if we try to use [1] then will face error becaue opportuity available on 0 index.

Please use below code:-
 
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
		opp1.AccountId = acc.Id;
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
		opp1.Amount = 2000;
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> oppList = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, oppList[0].Amount);
        
            
        
        
    }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
James Charles MerchJames Charles Merch
I am trying to update the AnnualRevenue of Account into Opportunity amount, (http://jamescharlesmerch.biz/) and it is working for update.