Preface: this post is part of the SOQL: A Beginner’s Guide series.
Chapter 2 Questions:
1. Why do 95% of triggers use SOQL?
2. What are the two words that appear in every SOQL statement?
3. Name at least three places you can write SOQL.
4. Which of these two statements will break and why?
SELECT Id FROM User WHERE IsActive = true
SELECT Id FROM User WHERE IsActive = ‘false’
5. Finish this statement which wants to query all Leads with an email address:
SELECT Id FROM Lead WHERE Email
6. Finish this statement, which wants to query all Accounts with names that start with an ‘A’
SELECT Id FROM Account WHERE Name LIKE
7. Write a query that gets all Opportunities with a Close Date in the future.
8. Which direction are these relationships? Upwards or downwards.
a. Contacts to Accounts
b. Accounts to Tasks
c. Opportunity Line Items to Opportunities
d. Accounts to Quote Line Items
e. Campaign Members to Accounts
f. Opportunity History to Accounts
9. When do you use __r instead of __c?
10. Write a query that gets a Contact’s Account owner’s username and their best friend as well as all related Tasks that were created in the last 90 days or have both a comment that contains the words ‘nutella’ and ‘krispy kreme’ and the Task creator’s title is populated. Oh yes – all the contacts have to weigh more than 120 pounds and have a best friend whose owner’s name ends in a ‘d’. No more than 100 records please… whew!
Hi David,
I know I am getting a little a head but I have been getting adventurous. I have a question about some code I wrote. I wrote a trigger on the Account object to create a Opportunity when a certain criteria is met. The problem I am having is the test class is not giving me a 100% code coverage and I don’t know why, it is giving me 22% coverage. I would really appreciate your help. Here is the trigger code:
trigger EmployeeGreater100CreateOpp on Account (after insert) {
for(Account acc : Trigger.new){
if(acc.NumberOfEmployees > 100){
Opportunity opp = New Opportunity();
opp.Name = acc.Name + ‘-Opp’;
opp.StageName = ‘Qualification’;
opp.CloseDate = Date.Today().addDays(30);
opp.Amount = 10000;
opp.AccountId = acc.Id;
insert opp;
}
}
}
Here is the Test class code:
@isTest
public class TestEmployeeCreateOpp{
static testMethod void insertAcct(){
Account acct = New Account();
acct.Name = ‘Test Account’;
insert acct;
Opportunity opp = New Opportunity();
opp.Name = ‘Test Opp’;
opp.CloseDate = Date.Today().addDays(60);
opp.StageName = ‘Qualification’;
opp.Amount = 1000;
opp.AccountId = acct.Id;
insert opp;
}
}
Your Account needs to have over 100 employees =)
trigger employeeAndOppy on Account (after insert, after update) {
Opportunity oppy = new Opportunity ();
for (Account acct : Trigger.new){
if (acct.NumberOfEmployees>100){
oppy.AccountID = acct.ID;
oppy.Name = acct.Name + ‘-Opp’;
oppy.StageName = ‘Closed Won’;
oppy.CloseDate = Date.Today().addDays(30);
Insert oppy;
}
}
}
@isTest
Public Class testEmpOppy {
Static testMethod void testOppy (){
//Lets create an account with employee more than 100
Account acct1 = new Account ();
acct1.Name = ‘Citrus’;
acct1.NumberOfEmployees = 200;
Insert acct1;
//find the opportunity
List oppy1 = [SELECT ID FROM Opportunity WHERE Name = ‘Citrus-Opp’];
system.assertEquals(1, oppy1.size());
//lets create another account with 50 employees
Account acct2 = new Account ();
acct2.Name = ‘Blue-Citrus’;
acct2.NumberOfEmployees = 50;
try {
Insert acct2;
} catch (Exception e){
system.debug (‘no oppy there which is expected’);
}
//find the opportunity
List oppy2 = [SELECT ID FROM Opportunity WHERE Name = ‘Blue-Citrus-Opp’];
system.assertEquals(0, oppy2.size());
}
}
I can get 100% coverage with the above trigger and test class.
I cannot thank enough to you and your website @David. Your website is helping me tremendously.
1. Since the SOSL works on actual data, we can’t write test classes for trigger. Hence 95%of the triggers uses the SOQL.