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
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
pratiksha mogal 6pratiksha mogal 6
// If you find it helpfull please mark ans a best ans thank you

// 1st Question ans
public class SOQLQuery {
   Public Static void  allContacts(){
        List<Contact> ContactList = new List<Contact>();
       ContactList = [Select Id,Name from Contact];
        System.debug(ContactList);
    }
    // 2nd Question ans
    Public static void Activeusers(){
        map<id,user>activeusersmap = new map <id,user>();
        List<user> userList = [Select Id,Name,Email from user where IsActive = true];
        for(User user : userList){
        activeusersmap.put(user.Id,user);
        }
        system.debug(activeusersmap);
    }
    // 3rd (A)Question ans
    public static void structureofmap1(){
       map<string,id> stucturemap = new map <string,id>();
        List<account> accountList = [Select Id,Name from Account];
        for(account accountname : accountList){
         stucturemap.put(accountname.name,accountname.id);   
        }
        system.debug(stucturemap);
    }
    // 3rd (B) Question ans
    public static void structureofmap2(){
       map<id,account> stucturemaptwo = new map <id,account>();
        List<account> accountList = [Select Id,Name from Account];
        for(account accountname2 : accountList){
         stucturemaptwo.put(accountname2.id,accountname2);   
        }
        system.debug(stucturemaptwo);
    }
    // 4th (AB) Question ans
    public static void Enrollmentofyear(){
      List<Account> AccountList = new List<Account>();
        AccountList = [Select Id,Name From Account Where Enrollment_Year__c IN ('2010','2013;2014')];
         System.debug(AccountList);
    }
    // 5 Question ans
    public static void Billingstate(){
        list<Account> Billingstatelist = new list <Account>();
    Billingstatelist = [Select Id,Name From Account where Billing_State__c NOT IN ('Maharashtra','Kerala') ORDER BY Billing_State__c DESC NULLS LAST LIMIT 10000];
        System.debug(Billingstatelist);
    }
    // 6 Question ans
    Public static Void DisplayOop(){
       List<Opportunity> oppList=new List<Opportunity>();
       oppList = [Select Id,Name,Amount From opportunity where Amount >= 10000 ORDER BY CreatedDate DESC LIMIT 100 OFFSET 50 ALL ROWS];
          System.debug(opplist);
    }
    }