+ Start a Discussion
sgsssgss 

Soql test classes

Can anyone help me in the following questions along with each questions test cases?

1. Query on all Contact records and add them to the List. Print that contents of this list.
2. Write a SOQL query to retrieve/print all active Users. Prepare a Map having User Id as key and User record as value. (Hint : Map)
3. Prepare the following map structures : a. Account Name as key and AccountId as value. b. Account Id as key and entire Account object as value.
4. Create a multi-select picklist on Account object called as 'Enrollment Year' with values - 2010, 2011, 2012, 2013, 2014, 2015 and 2016.
Get all account records where in selected 'Enrollment Year' is:
a. 2010
b. 2013 and 2014
5. Write a SOQL query to find all Account records where 'Billing State' is not 'Maharashtra' and 'Kerala'. Order the results by Billing State in descending order with null values at the end. Display first 10,000 records only. NOTE: do not use AND operator.
​6. Write a SOQL query to display 100 opportunity records with amount greater than 10,000 order by created date. Skip first 50 records and include records from recycle bin.
Thanks
Best Answer chosen by sgss
Akshay_DhimanAkshay_Dhiman
Hi Supriya

Try for This code,
 
public static void data()
    {
        
        //11111111111111111111111111
        List<Contact> conList=new List<Contact>();
        conList=[SELECT lastname FROM contact limit 100];
        System.debug(conList);
        //22222222222222222222222222222222 
        List<user> userList=new List<user>();
        Map<id,list<User>> userMap=new   Map<id,list<User>>();
        userList=[SELECT id,name,email FROM User WHERE IsActive=true];
        System.debug('userList'+userList);
        for(User obj:userList)
        {
            if(userMap.get(obj.id)==null)
            {
                userMap.put(obj.id,new List<User>());
            }
            userMap.get(obj.id).add(obj);
        }
        System.debug('Map Of User'+userMap);
        //333333333333333333333333333333
        List<Account> accList=new List<Account>();
        Map<id,list<Account>> accMap=new   Map<id,list<Account>>();
        Map<String,id> accidMap=new   Map<String,id>();
        accList=[SELECT id,name FROM Account limit 100];
        System.debug('accList'+accList);
        for(Account obj:accList)
        {
            if(accMap.get(obj.id)==null)
            {
                accMap.put(obj.id,new List<Account>());
            }
            accMap.get(obj.id).add(obj);
        }
        System.debug('map of account'+accMap);
        // ----------------------------
        for(Account obj:accList)
        {
            accidMap.put(obj.name,obj.id);
        }
        System.debug('map of account'+accidMap);  
        //44444444444444444444444444444444444
        List<Account> accList1=new List<Account>();
        
        //  Enrollment Year 2010
        accList1=[SELECT name FROM account WHERE Enrollment_Year__c='2010' ];
        //  //  Enrollment Year 2013,2014
        List<Account> accList2=new List<Account>();
        accList2=[SELECT name FROM account WHERE Enrollment_Year__c='2013' OR Enrollment_Year__c='2014' LIMIT 1000];
        //5555555555555555555555555555555555555
        List<Account> accList3=new List<Account>();
        accList3=[SELECT name FROM account WHERE BillingState!='Maharashtra' OR BillingState!='Kerala' ORDER BY BillingState DESC LIMIT 10000];
        System.debug('accList3'+accList3); 
        
        //66666666666666666666666666666666666666
        List<Opportunity> oppList=new List<Opportunity>();
        oppList=[SELECT name,
                 amount
                 FROM Opportunity
                 WHERE (isDeleted=true 
                        OR isDeleted=false) 
                 AND  amount >=10000 
                 ORDER BY createddate DESC
                 LIMIT 100 
                 OFFSET 50 
                ];
        System.debug('oppList'+oppList); 
    }


    
Thanks.

All Answers

devedeve
1.
    List<Contact> contactList = [SELECT Id from Contact];
    System.debug(contactList);

2.
List<User> userList = [SELECT Id,Name FROM User WHERE Active=true];
Map<Id, User> userMap = new Map<Id, User>();
for(User u: userList) {
    userMapp.add(u.Id, u);
}

3.
List<Account> accList = [SELECT Id FROM Account];
Map<String, Id> map1 = new Map<String, Id>();
Map<Id, Account> map2 = new Map<Id, Account>();
for(Account a: accList) {
    map1.add(a.Name, a.Id);
    map2.add(a.Id, a);
}
Akshay_DhimanAkshay_Dhiman
Hi Supriya

Try for This code,
 
public static void data()
    {
        
        //11111111111111111111111111
        List<Contact> conList=new List<Contact>();
        conList=[SELECT lastname FROM contact limit 100];
        System.debug(conList);
        //22222222222222222222222222222222 
        List<user> userList=new List<user>();
        Map<id,list<User>> userMap=new   Map<id,list<User>>();
        userList=[SELECT id,name,email FROM User WHERE IsActive=true];
        System.debug('userList'+userList);
        for(User obj:userList)
        {
            if(userMap.get(obj.id)==null)
            {
                userMap.put(obj.id,new List<User>());
            }
            userMap.get(obj.id).add(obj);
        }
        System.debug('Map Of User'+userMap);
        //333333333333333333333333333333
        List<Account> accList=new List<Account>();
        Map<id,list<Account>> accMap=new   Map<id,list<Account>>();
        Map<String,id> accidMap=new   Map<String,id>();
        accList=[SELECT id,name FROM Account limit 100];
        System.debug('accList'+accList);
        for(Account obj:accList)
        {
            if(accMap.get(obj.id)==null)
            {
                accMap.put(obj.id,new List<Account>());
            }
            accMap.get(obj.id).add(obj);
        }
        System.debug('map of account'+accMap);
        // ----------------------------
        for(Account obj:accList)
        {
            accidMap.put(obj.name,obj.id);
        }
        System.debug('map of account'+accidMap);  
        //44444444444444444444444444444444444
        List<Account> accList1=new List<Account>();
        
        //  Enrollment Year 2010
        accList1=[SELECT name FROM account WHERE Enrollment_Year__c='2010' ];
        //  //  Enrollment Year 2013,2014
        List<Account> accList2=new List<Account>();
        accList2=[SELECT name FROM account WHERE Enrollment_Year__c='2013' OR Enrollment_Year__c='2014' LIMIT 1000];
        //5555555555555555555555555555555555555
        List<Account> accList3=new List<Account>();
        accList3=[SELECT name FROM account WHERE BillingState!='Maharashtra' OR BillingState!='Kerala' ORDER BY BillingState DESC LIMIT 10000];
        System.debug('accList3'+accList3); 
        
        //66666666666666666666666666666666666666
        List<Opportunity> oppList=new List<Opportunity>();
        oppList=[SELECT name,
                 amount
                 FROM Opportunity
                 WHERE (isDeleted=true 
                        OR isDeleted=false) 
                 AND  amount >=10000 
                 ORDER BY createddate DESC
                 LIMIT 100 
                 OFFSET 50 
                ];
        System.debug('oppList'+oppList); 
    }


    
Thanks.
This was selected as the best answer
sgsssgss
Thanks Akshay can you provide test class of 4,5,6 considering positive negative test cases