You need to sign in to do that
Don't have an account?
sgss
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
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
Try for This code,
Thanks.
All Answers
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);
}
Try for This code,
Thanks.
// 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);
}
}