Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
public class Map_Opportunities {

    public Map<string,List<Opportunity>> oppMap        {set;get;}    

    public List<Account> accList                    {set;get;}

    

    public Map_Opportunities(){

        oppMap = new Map<string,List<Opportunity>>();  

        accList=new List<Account>();

        

        accList = [SELECT name,(SELECT Name,stageName FROM Opportunities)FROM Account];

        for (Account a :accList){

            oppMap.put(a.Name, a.Opportunities);

        }                    

    }

}

-------------------------------------------

Test Class

--------------------------------------------

@isTest

    static void mapOpportunits(){

        Map_Opportunities mp = new Map_Opportunities();

        List<Opportunity> optyList = new List<Opportunity>();

        account acc = new Account();

        acc.name = 'Ravi';

        insert acc;

        

        opportunity op = new Opportunity();

        op.Name = 'Xyz';

        op.AccountId = acc.id;

        op.CloseDate = system.today();

        op.StageName ='Closed Won';    

        optyList.add(op);

        insert optyList;

        mp.oppMap.put(acc.Name, optyList);

    }

85% code is covered and Displaying Error : oppMap.put(a.Name, a.Opportunities);
1 answer
  1. Apr 24, 2018, 7:04 AM
    try this :

     

    @isTest

    private class testClass{

    static testMethod void TestCls(){

    account acc = new account();

    acc.Name = 'test';

    // add all required fields here

    insert acc;

    opportunity op = new Opportunity();

    op.Name = 'Xyz';

    op.AccountId = acc.id;

    op.CloseDate = system.today();

    op.StageName ='Closed Won';

    insert op;

    Map_Opportunities mapOpp = new Map_Opportunities();

    }

    }

    Thanks, let us know if it helps you

     
0/9000