7 Practice Problems for APEX Triggers


Here is my list of 7 practice problems for Apex Triggers programs that can help any fresher to get started with the salesforce apex triggers. These are classics, popular, and very effective. You can use the developer console to write the triggers. See all solutions and ask if you have any queries.

For experienced who are more advanced and reading this, please keep in mind that this is merely for the purpose of learning.

Problem 1

Assignment:

Create Account record whenever new contact is created without an account.

Solution :

trigger ContactCustomTriggerExample on Contact (after insert) {
List<Account> accListToInsert = new List<Account>();
for(Contact con : Trigger.New) {
//check if account is null on contact
if(con.AccountId == null ) {
Account acc = new Account();
//Add all required field on Account
acc.Name = con.LastName;
acc.Phone = con.Phone;
accListToInsert.add(acc);
}
}
if(!accListToInsert.isEmpty()){
insert accListToInsert;
}
}


Problem 2

Pre-Reqs:

Create a field on Account called “Only_Default_Contact”, checkbox, default off

Assignment:

When a new Account is created, create a new Contact that has the following data points:

  • First Name = “Info”
  • Last Name = “Default”
  • Email = “info@websitedomain.tld”
  • Only_Default_Contact = TRUE

When the Account has more than 1 Contact, update Only_Default_Contact to FALSE.

Solution :

trigger defaultContact on account(after insert)
{
Account acc = [select id,Only_Default_Contact from account where id in : trigger.new];
contact con = new contact();
con.firstName = 'Info';
con.LastName = 'Default';
con.email = 'info@websitedomain.tld';
con.accountId = acc.id;
insert con;
//If you want to make checkbox true then use this also-
acc.Only_Default_Contact = true;
update acc;
}


Problem 3

Pre-Reqs:

Create a field on Account called “ Contact_Created__c ”, checkbox, default off

Assignment:

Create an apex trigger on the contact object and every time a new contact will be created and an account will be selected in the creation of that contact object then on that account object a checkbox filed ( Contact_Created__c ) will be checked true, which represent that this account has a contact.

Solution :

trigger AcconuntForContact on Contact (after insert , before update){
if(trigger.isInsert && trigger.isAfter){
set<id> ids = new set<id>();
for(contact con: trigger.new){
if(con.accountid!=null){
ids.add(con.accountid);
}
}
list<account> acclist = [select id,name,Contact_Created__c from account where id in: ids];
for(contact con : trigger.new){
for(account acc: acclist){
if(acc.id==con.accountid){
acc.Contact_Created__c= true;
}
}
}
if(accList.size()>0){
update acclist;
}
}
}


Problem 4

Pre-Reqs:

Create a field on Account called “Out_of_Zip”, checkbox, default off

Assignment:

When a Billing Address is modified, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.

Solution :

trigger TriggerExample on Account (before update, before insert) {
Set<Id> changedAccounts = new Set<Id>();
for(Integer i = 0; i < Trigger.New.size(); i++) {
Account newAcc = Trigger.New[i];
Account oldAcc = Trigger.Old[i];
if(newAcc.BillingStreet != oldAcc.BillingStreet ||
newAcc.BillingCity != oldAcc.BillingCity ||
newAcc.BillingState != oldAcc.BillingState ||
newAcc.BillingPostalCode != oldAcc.BillingPostalCode ||
newAcc.BillingCountry != oldAcc.BillingCountry)
changedAccounts.add(newAcc.Id);
Map<Id, Integer> accountsAndOutOfZips = new Map<Id, Integer>();
for(Contact c : [SELECT Id, Account.BillingPostalCode FROM Contact WHERE Id IN :changedAccounts]) {
if(c.MailingPostalCode != c.Account.BillingPostalCode) {
if(accountsAndOutOfZips.get(c.Id) == null)
accountsAndOutOfZips.put(c.Id, 1);
else
accountsAndOutOfZips.put(c.Id, accountsAndOutOfZips.get(c.Id) + 1);
}
}
for(Account acc : Trigger.New) {
if(accountsAndOutOfZips.get(acc.Id) != null && accountsAndOutOfZips.get(acc.Id) > 1)
acc.Out_of_Zip__c = true;
else
acc.Out_of_Zip__c = false;
}
}
}


Problem 5

Pre-Reqs:

Create a field on Contact called Profile, text, 255

Assignment:

When an Account is updated and the Website is filled in, update all the Profile field on all Contacts to:

  • Profile = Website + ‘/’ + First Letter of First Name + Last Name

Solution :

trigger TriggerExample3 on Account (After Update)
{
if(Trigger.isAfter && Trigger.isUpdate)
{
Set<Id> AccountId_Set = new Set<Id>();
new list<contact>();
for(Account ac : trigger.new)
{
if(ac.website != null)
{
AccountId_Set.add(ac.id);
}
}
if(AccountId_Set.size()>0)
{
List<Contact> Contact_List = [select Id,Firstname,Lastname,Profile__c,Accountid,Account.website from contact where Accountid in :AccountId_Set];
for(Contact con : Contact_List)
{
if(con.FirstName != Null)
{
con.Profile__c = con.account.website + '/' + con.FirstName.substring(0, 1) + con.lastname;
}
}
update Contact_List;
}
}
}


Problem 6

Pre-Reqs:

Create a field on Account called “is_gold”, checkbox, default off

Assignment:

When an Opportunity is greater than $20k, mark is_gold to TRUE

Solution :

trigger TriggerExample4 on Account (After Update) {
integer amount=20000;
For(Account acc:trigger.new){
List<opportunity> opps=[select id from opportunity where Account.id=:acc.id And Amount > 20000];
If(opps.size()>0){
acc.Is_Gold__c=True;
}Else{
acc.Is_Gold__c=False;
}
update acc;
}
}


Problem 7

Pre-Reqs:

Create a field on Account called “need_intel”, checkbox, default off

Create a field on Contact called “Dead”, checkbox, default off

Assignment:

If 70% or more of the Contacts on an Account are Dead, mark the need_intel field to TRUE

Solution :

public static void DeadIntel(list<Contact> cont){
Set<Id> accIds = new Set<Id>();
for(Contact conList : cont){
if(conList.accountId!=null){
accIds.add(conList.accountId);
}
}
Map<Id, List<Contact>> accContactMap = new Map<Id, List<Contact>>();
List<Account> accUpdateList = new List<Account>();
for(Contact obj : [SELECT accountId,Dead__c
FROM Contact
WHERE accountId IN :accIds]){
List<Contact> contLists;
if(accContactMap.containsKey(obj.accountId)){
contLists = accContactMap.get(obj.accountId);
}else{
contLists = new List<Contact>();
}
contLists.add(obj);
accContactMap.put(obj.accountId, contLists);
}
for(Id accId : accContactMap.keySet()){
Integer count_of_Dead = 0;
Integer total_con = accContactMap.get(accId).size();
if(accContactMap.get(accId).size() > 1){
for(integer i =0 ; i<accContactMap.get(accId).size(); i++)
if(accContactMap.get(accId)[i].Dead__c == true){
count_of_Dead++;
}
}
if((count_of_Dead*100)/total_con) > 70)
accUpdateList.add(new Account(id = accId, needintel__c = true));
}
if(!accUpdateList.isEmpty()){
update accUpdateList;
}
}
//Trigger
trigger UpdateContact on Contact (after update) {
if(Trigger.isUpdate && Trigger.isAfter){
AccountContact.DeadIntel(Trigger.new);
}
}

2 thoughts on “7 Practice Problems for APEX Triggers

  1. Hi, Thanks for these useful trigger scenarios.

    I got a doubt in “Problem 1″…we are creating an account because a contact is created without associating an account right?. Do we need to add that account Id back to the contact right once Account is created ?

    Regards,
    Bhanu Prakash

    Like

    1. Hi Bhanu Prakash,

      Absolutely, I appreciate your curiosity and I’m happy to help in a friendly way!

      Great question! In the provided trigger scenario, when a Contact is inserted without an associated Account, we create a new Account for it. However, you rightly pointed out that we should also link this newly created Account back to the Contact.

      Here’s a friendly modification to the trigger that addresses this:

      trigger ContactCustomTriggerExample on Contact (after insert) {
      List accListToInsert = new List();
      List conListToUpdate = new List();

      for (Contact con : Trigger.New) {
      // Check if the contact is missing an associated account
      if (con.AccountId == null) {
      Account acc = new Account();

      // Set up the new Account with relevant details
      acc.Name = con.LastName;
      acc.Phone = con.Phone;
      accListToInsert.add(acc);

      // Associate the new Account with the Contact
      con.AccountId = acc.Id;
      conListToUpdate.add(con);
      }
      }

      // Insert the new Accounts
      if (!accListToInsert.isEmpty()) {
      insert accListToInsert;
      }

      // Update Contacts with the associated AccountIds
      if (!conListToUpdate.isEmpty()) {
      update conListToUpdate;
      }
      }

      Now, when a Contact is created without an Account, the trigger not only creates a new Account but also makes sure the Contact is linked to that Account. Feel free to reach out if you have more questions or if there’s anything else you’d like to discuss!

      Like

Leave a comment