Salesforce Apex Trigger Practice Questions 2022

In this article, we have tried to cover frequently asked Salesforce Apex Trigger Practice Questions and understanding the basic concepts of apex triggers which are often asked in salesforce developer interview questions.

Learn: How to schedule apex class for 5 or 10 minutes

What Is Trigger In Salesforce?

A trigger is an Apex script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted. Triggers enable you to perform custom actions before or after changes to Salesforce records. A trigger is Apex code that executes before or after the following types of operations like insert, update, and delete.

There Are Two Types Of Triggers:

  • Before triggers: It is used to update or validate record values before saved to the database.
  • After triggers: It is used to access values of the record that are stored in the database and use this value to make changes with other records.After trigger records are read-only.
Trigger trigger Name on sObject(Trigger event) {
    //logic
}

Bulky Trigger

All triggers are bulk triggers by default and can process multiple records at a time. You should always plan on processing more than one record at a time. Bulk triggers can handle both single record updates and bulk operations like:

  • Data import
  • com Bulk API calls
  • Mass actions, such as record owner changes and deletes
  • Recursive Apex methods and triggers that invoke bulk DML statements

A trigger is Apex code that executes before or after the following types of operations:

  • insert
  • update
  • delete
  • merge
  • upsert
  • undelete

Also See: Amazon OA Online Assessment 2021 Questions and Answers

Salesforce Apex Trigger Practice

Lets understand how a trigger is created? and why a trigger is created. Some best practices to follow during trigger creation:

One Trigger Per Object

A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

Logic-less Triggers

If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.

Context-Specific Handler Methods

Create context-specific handler methods in Trigger handlers

Bulkify your Code

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

Avoid SOQL Queries or DML statements inside FOR Loops

An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

Using Collections, Streamlining Queries, and Efficient For Loops

It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

Querying Large Data Sets

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

Use @future Appropriately

It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

Avoid Hardcoding IDs

When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

Always remember below points before writing trigger :
1) Order Of Execution
2) Recursive Trigger
3) Learn about Other Trigger Framework with Recording

Trigger Handler framework Code

Create one Trigger “AccountTrigger

trigger AccountTrigger on Account( after insert, after update, before insert, before update)
{

    AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if( Trigger.isInsert )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeInsert(trigger.New);
        }
        else
        {
            handler.OnAfterInsert(trigger.New);
        }
    }
    else if ( Trigger.isUpdate )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
        else
        {
            handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
    }
}

Create one Trigger Handler Class

public with sharing class AccountTriggerHandler 
{
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean IsFromBachJob ;
    public static boolean isFromUploadAPI=false;
    
    public AccountTriggerHandler(boolean isExecuting, integer size)
    {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
            

    public void OnBeforeInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On Before Insert');
    }
    public void OnAfterInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On After Insert');
    }
    public void OnAfterUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On After Update ');
        AccountActions.updateContact (newAccount);
    }
    public void OnBeforeUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On Before Update ');
    }

    @future 
    public static void OnAfterUpdateAsync(Set<ID> newAccountIDs)
    {

    }      
    public boolean IsTriggerContext
    {
        get{ return m_isExecuting;}
    }
    
    public boolean IsVisualforcePageContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsWebServiceContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsExecuteAnonymousContext
    {
        get{ return !IsTriggerContext;}
    }
} 

Create one Trigger Action Class

public without sharing class AccountActions 
{
    public static void updateContact ( List<Account> newAccount)
    {
        // Add your logic here
    }
}

Also See: Microsoft Online Assessment Questions and Solution

trigger scenario questions
Trigger scenario questions

Salesforce Apex Trigger Practice Scenario Questions

So far we have understood what is triggers? and what are the best practices to create a trigger. Now we are going to solve some best trigger questions which are often asked during salesforce training or interview. This trigger scenario questions will help you to learn more in depth about triggers including the concept of before insert, after insert, before delete, after delete etc.. Practice this Trigger Sample Questions and your concept regarding triggers will be much more clear an in depth knowledge.

Question 1: Create “Top X Designation” custom object which is the related list to Opportunity (Look up Relationship). In the Top X Designation object, create the fields

  • Type (Picklist),
  • Document Attached (Checkbox)

Create one field Handoff Attached with pick list type with values are Yes, No on Opportunity Object.

Solution

Now the logic is, If Type (Top X Designation) = “Contract Flow Down/Handoff”, and “Document Attached” = True then “Handoff Attached” = True, otherwise false.

trigger UpdateHandoffAttached on Top_X_Designation__c(after insert, after update,
    after delete) {
    List < Opportunity > listoppUpdate = new List < Opportunity > ();
    List < Opportunity > listopp = new List < Opportunity > ();
    Set < Id > setOppId = new Set < Id > ();
    Set < Id > setOppDelete = new Set < Id > ();
    map < id, id > mapDocAttchTrue = new map < id, id > ();
    map < id, id > mapDocAttchFalse = new map < id, id > ();
    map < id, id > mapDelete = new map < id, id > ();
    if (Trigger.isInsert || Trigger.isUpdate) {
        for (Top_X_Designation__c ada: Trigger.New) {
            if (ada.Document_Attached__c == True && ada.Item_Type__c == 'Contract Flow
                Down / Handoff '){
                mapDocAttchTrue.put(ada.Opportunity, ada.id); setOppId.add(ada.Opportunity);
            }
            else
                mapDocAttchFalse.put(ada.Opportunity, ada.id);
            setOppId.add(ada.Opportunity);
        }
    }
    if (Trigger.isDelete) {
        for (Top_X_Designation__c ada: Trigger.old) {
            mapDelete.put(ada.Opportunity, ada.id);
            setOppId.add(ada.Opportunity);
            setOppDelete.add(ada.Opportunity);
        }
    }
    listopp = “select id, Handoff_Attached__c from Opportunity where id in: setOppId”;
    if (listopp.size() > 0 && listopp != null) {
        for (Opportunity opp: listopp) {
            if (mapDocAttchTrue.containskey(opp.id)) {
                opp.Handoff_Attached__c = 'Yes';
            }
            if (mapDocAttchFalse.containskey(opp.id)) {
                opp.Handoff_Attached__c = 'No';
            }
            if (setOppDelete.contains(opp.id)) {
                opp.Handoff_Attached__c = '';
            }
            listoppUpdate.add(opp);
        }
    }
    if (listoppUpdate.size() > 0 && listoppUpdate != null) {
        update listoppUpdate;
        system.debug('LLLLLLLLLLLLLLL' + listoppUpdate);
    }
}

Question 2: The following Trigger will fires when we try to create the account with same name i.e. Preventing the users to create Duplicate Accounts

Solution

trigger AccountDuplicateTrigger on Account(before insert, before update) {
    for (Account a: Trigger.new) {
        List < Account > acc = “Select id from Account where Name =: a.Name and Rating =: a.Rating“;
        if (acc.size() > 0) {
            acc.Name.addError('You Cannot Create the Duplicate Account');
        }
    }
}

Question 3: The following trigger updates the field called “Hello” by the value “World” whenever we are creating an account or updating an account record. Create the field called “Hello” on the Account Object (Data Type = Text)

Solution

trigger HelloWorld on Account(before insert, before update) {
    List < Account > accs = Trigger.new;
    MyHelloWorld my = new MyHelloWorld(); //creating instance of apex class
    my.addHelloWorld(accs); // calling method from the apex class
}

Class:

trigger HelloWorld on Account(before insert, before update) {
    List < Account > accs = Trigger.new;
    MyHelloWorld my = new MyHelloWorld(); //creating instance of apex class
    my.addHelloWorld(accs); // calling method from the apex class
}

Question 4: The following trigger describes when the leads are inserted into the data base it would add Doctor prefixed for all lead names. This is applicable for both inserting and updating the lead records.

Solution

trigger PrefixDoctor on Lead(before insert, before update) {
    List < Lead > leadList = trigger.new;
    for (Lead l: leadList) {
        l.firstname = 'Dr.' + l.firstname;
    }
}

Question 5: The following trigger adds the Opportunity Owner into the sales team automatically whenever the Opportunity is created.

Solution

trigger OppTeam on Opportunity(after insert) {
    List < OpportunityShare > sharesToCreate = new List < OpportunityShare > ();
    List < OpportunityTeamMember > oppteam = new List < OpportunityTeamMember >
        ();
    OpportunityShare oshare = new OpportunityShare();
    oshare.OpportunityAccessLevel = 'Edit';
    oshare.OpportunityId = trigger.new” 0”.Id;
    oshare.UserOrGroupId = trigger.new” 0”.createdbyid;
    sharesToCreate.add(oshare);
    OpportunityTeamMember ot = new OpportunityTeamMember();
    ot.OpportunityId = trigger.new” 0”.Id;
    ot.UserId = trigger.new” 0”.OwnerId;
    ot.TeamMemberRole = 'Account Manager';
    oppteam.add(ot);
    if (Oppteam != null && Oppteam.size() > 0) {
        insert Oppteam;
    }
    if (sharesToCreate != null && sharesToCreate.size() > 0) {
        list < Database.SaveResult > sr = Database.insert(sharesToCreate, false);
    }
}

Question 6: Create the object called “Books” and create field “Price”(data type is Currency) under this object.
Whenever we enter some amount of money in the Price field and once we click on save
button, the value we entered in the Price field is 10% less than the actual price. This is
applicable for while both inserting and updating records.

Solution

trigger DiscountTrigger on Book__c(before insert, before update) {
    List < Book__c > books = Trigger.new;
    PriceDiscount.applyDiscount(books);
}

Class

public class PriceDiscount {
    public static void applyDiscount(List < Book__c > books) {
        for (Book__c b: books) {
            b.Price__c *= 0.9;
        }
    }
}

Salesforce Apex Trigger Practice Questions

Question 7: The following trigger will prevent the users from deleting the Accounts. This is because System Administrator has all the permissions, we cannot change the permissions.

Solution

trigger AccountDelete on Account(before delete) {
    for (Account Acc: trigger.old) {
        acc.adderror('You Cannot Delete the Account Record');
    }
}

Question 8: Create Custom field called “Number of Locations” on the Account Object (Data Type=Number)The following trigger creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object.

Solution

trigger ContactsCreation on Account(after insert) {
    list < contact > listContact = new list < contact > ();
    map < id, decimal > mapAcc = new map < id, decimal > ();
    for (Account acc: trigger.new) {
        mapAcc.put(acc.id, acc.Number_of_Locations__c);
    }
    if (mapAcc.size() > 0 && mapAcc != null) {
        for (Id accId: mapAcc.keyset()) {
            for (integer i = 0; i < mapAcc.get(accId); i++) {
                contact newContact = new contact();
                newContact.accountid = accId;
                newContact.lastname = 'contact' + i;
                listContact.add(newContact);
            }
        }
    }
    if (listContact.size() > 0 && listContact != null)
        insert listContact;
}

Question 9: Create the object called “Customer Project” and create Status field under this object with picklist data type (Values=Active, Inactive). Create the relationship between this custom object and Opportunity so that Customer Project is related list to the Opportunity.

Solution
Create  Active Customer project‑ field on Opportunity object (Data Type=Checkbox)
The logic is when we create Customer Project for an Opportunity with the Status=Active, then Active Customer project check box on the Opportunity Detail page is automatically
checked.

trigger UpdateCPActivenOnOppty on Customer_Project__c(after insert) {
    List < Opportunity > opps = new List < Opportunity > ();
    for (Customer_Project__c cp: Trigger.New) {
        if (cp.Status__c == 'Active') {
            Opportunity opp = new Opportunity(id = cp.Opportunity__c);
            opp.Active_Customer_Project__c = True;
            opps.add(opp);
        }
    }
    update opps;
}

Question 10: Description:We have Stakeholder object that is the related list to Opportunity and Contact. On Contact detail page, we have NPS ID field on the Contact detail page that is look up to the Voice of NPS object (Customer Object). The following code will get the NPS ID field value from the Contact detail page in the Stackholders page which we can able to clickable.
Create NPS Id 1 field on the stackholders object which is the look up to the Voice of NPS object (Customer Object)

Note: we can also get the NPS Id 1 on the Stakeholder’s page using the formula field but will not able to clickable.

Solution

triggerUpdateNPSid on Stakeholder__c(before insert, before update) {
    List < id > ConList = new List < id > ();
    for (Stakeholder__csh: Trigger.New) {
        ConList.add(sh.Contact_Name__c);
    }
    List < Contact > lc = “Select NPS_Id__c From Contact Where id IN: ConList”;
    for (Stakeholder__csh: Trigger.New) {
        sh.NPS_Id1__c = lc” 0”.NPS_Id__c;
    }
}

Code Bulk Records

trigger UpdateNPSid on Stakeholder__c(before insert, before update) {
    List < id > ConList = new List < id > ();
    map < id, id > map_NPS_Cont = new map < id, id > ();
    for (Stakeholder__c sh: Trigger.New) {
        if (sh.Contact_Name__c != null)
            ConList.add(sh.Contact_Name__c);
    }
    List < Contact > lc = “Select Id, NPS_Id__c From Contact Where id IN: ConList”;
    if (lc != null && lc.size() > 0) {
        for (Contact c: lc) {
            If(c.NPS_Id__c != null) {
                map_NPS_Cont.put(c.Id, c.NPS_Id__c);
            }
        }
        for (Stakeholder__c sh: Trigger.New) {
            if (sh.Contact_Name__c != null)
                sh.NPS_Id1__c = map_NPS_Cont.get(sh.Contact_Name__c);
            else
                sh.NPS_Id1__c = null;
        }
    }
}

Question 11: Create “Sales Rep” field with data type (Text) on the Account Object. When we create the Account record, the Account Owner will be automatically added to Sales Rep field. When we update the Account owner of the record, then also the Sales Rep will be automatically updated.

Solution

triggerUpdateSalesRep on Account(Before insert, Before Update) {
    Set < Id > setAccOwner = new Set < Id > ();
    for (Account Acc: trigger.new) {
        setAccOwner.add(Acc.OwnerId);
    }
    Map < Id, User > User_map = new Map < Id, User > (“select Name from User where id in: setAccOwner”);
    for (Account Acc: Trigger.new) {
        User usr = User_map.get(Acc.OwnerId);
        Acc.sales_Rep1__c = usr.Name;
    }
}

Question 12: Create the field called “Contact Relationship” checkbox on the Contact Object and Create the related object called “Contact Relationship” which is related list to the Contacts.(Look Up Relationship).

Solution
Now logic is when we create contact by checking Contact Relationship checkbox, then Contact Relationship will be created automatically for that contact.

trigger CreateCRonContactCreation on Contact(after insert) {
    if (trigger.isAfter) {
        if (trigger.isInsert) {
            ContactMasterHandler ConIns = New ContactMasterHandler();
            ConIns.createContactRelationshipByContact(trigger.New);
        }
    }
}

Class:

Public Class ContactMasterHandler {
    public void createContactRelationshipByContact(list < Contact > List_Contacts) {
        list < Contact_Relationship__c > ConList = new list < Contact_Relationship__c > ();
        for (Contact newconts: List_Contacts) {
            if (newconts.Contact_Relationship__c == true) {
                Contact_Relationship__c CR = new Contact_Relationship__c();
                CR.Name = newconts.Lastname;
                CR.Contact__c = newconts.id;
                ConList.add(CR);
            }
        }
        insert ConList;
    }

Salesforce Apex Trigger Practice Sample Questions

Question 13: When we change the Owner of the Contact Relationship, then the Owner name will be automatically populated in the Contact Relationship Name field.

Solution

trigger ContactRelationshipMasterTrigger on Contact_Relationship__c(before update) {
    if (trigger.isBefore) {
        if (trigger.isUpdate) {
            //call the handler for the before update trigger event
            updateCROwnerName ConRelUpd = New updateCROwnerName();
            ConRelUpd.updateContactRelationshipNameByOwner(trigger.New);
        }
    }
}

Class:

Public Class updateCROwnerName {
    public void updateContactRelationshipNameByOwner(list < Contact_Relationship__c > co nt_Rel) {
        map < Id, Id > map_Id_Own = new map < id, id > ();
        map < Id, string > map_id_Name = new map < id, string > ();
        set < id > Idset = new set < id > ();
        for (Contact_Relationship__c List_recs: cont_Rel) {
            Idset.add(List_recs.Ownerid);
        }
        list < user > u = “select id, Name from user where id in: Idset”;
        for (user list_users: u) {
            map_id_Name.put(list_users.Id, list_users.Name);
        }
        if (u != null && u.size() > 0) {
            for (Contact_Relationship__c List_recs: cont_Rel) {
                if (List_recs.Ownerid != null) {
                    List_recs.Name = map_id_Name.get(List_recs.Ownerid);
                }
            }
        }
    }
}

Question 14: Create the field called “Contact Relationship” checkbox on the Contact Object and Create the object called “Contact Relationship” which is related list to the Contacts.(Look Up Relationship).

Solution
Trigger Scenario 12 logic will says that when we create contact by checking Contact Relationship checkbox, then Contact Relationship will be created automatically for that contact.
No this logic will for when we delete the Contact, Then Contact Relationship will be deleted automatically.

 trigger DeleteCRonContactDeletion on Contact(before delete) {
     if (trigger.isBefore) {
         if (trigger.isDelete)
             for (Contact c: Trigger.old) {
                 Contact_relationship__c CR = new Contact_relationship__c();
                 CR = “Select Id from Contact_Relationship__c where Contact__c IN: GlobalUtility.getUni
                 queIds(Trigger.Old)”;
                 delete CR;
             }
     }
 }
 }

Global Utility Classs:

public static set < Id > getUniqueIds(list < SObject > sobs) {
    set < Id > Ids = new set < Id > ();
    for (SObject sob: sobs) {
        Ids.add(sob.Id);
    }
    return Ids;
}

Question 15: Create the field called “Contact Relationship” checkbox on the Contact Object and Create the object called “Contact Relationship” which is related list to the Contacts.(Look Up Relationship).

Solution
Trigger Scenario 14 will says that when we delete the Contact, Then Contact Relationship will be deleted automatically
Now the Logic is when we undelete the Contact, then Contact Relationship will be undeleted

trigger UnDeleteCRonContactUnDeletion on Contact(After Undelete) {
    if (trigger.isUndelete) {
        //call the handler for the after undelete trigger event
        ContactMasterHandler_Undelete ConIns = New ContactMasterHandler_Undelete();
        ConIns.undeleteContactRelationshipsByContact(Trigger.New);
    }
}

Class

Public Class ContactMasterHandler_Undelete {
    public void undeleteContactRelationshipsByContact(list < Contact > List_Contacts) {
        set < Id > ContactIds = New set < Id > ();
        if (List_Contacts != null && List_Contacts.size() > 0) {
            list < Contact_Relationship__c > List_ConRels = new list < Contact_Relationship__c > ();
            List_ConRels = “select id from Contact_Relationship__c where isDeleted = TRUE and Co
            ntact__c in: GlobalUtility.getUniqueIds(List_Contacts)”;
            undelete List_ConRels;
        }
    }
}

Question 16: Create field called “Count of Contacts” on Account Object. When we add the Contacts for that Account then count will populate in the field on Account details page. When we delete the Contacts for that Account, then Count will update automatically.

Solution

Note: The above logic will be applicable when we have look up relationship. But When we have the Master – Detail relationship, then we can create Rollup Summary field to get the count of child records using “Count” function.

trigger CountOfContacts on Contact(after insert, after delete) {
    set < id > accid = new set < id > ();
    list < contact > contactlist = new list < contact > ();
    list < contact > listcon = new list < contact > ();
    list < account > acclist = new list < account > ();
    list < account > listAcc = new list < account > ();
    map < id, integer > mapCount = new map < id, integer > ();
    if (trigger.isinsert) {
        for (contact con: trigger.new) {
            accid.add(con.accountid);
        }
    }
    if (trigger.isdelete) {
        for (contact con: trigger.old) {
            accid.add(con.accountid);
        }
    }
    acclist = “select id, name from account where id in: accid”;
    contactlist = “select id, name, accountid from contact where accountid in: accid”;
    for (account acc: acclist) {
        listcon.clear();
        for (contact c: contactlist) {
            if (c.accountid == acc.id) {
                listcon.add(c);
                mapCount.put(c.accountid, listcon.size());
            }
        }
    }
    if (acclist.size() > 0) {
        for (Account a: acclist) {
            if (mapCount.get(a.id) == null)
                a.Count_Of_Contacts__c = 0;
            else
                a.Count_Of_Contacts__c = mapCount.get(a.id);
            listAcc.add(a);
        }
    }
    if (listAcc.size() > 0)
        update listAcc;
}

Question 17: Create the object called “Customer” and create the Master-Detail Relationship on Customer object so that Customer will be the related list to Account record. Create the field called “Account Manager” on Customer object which is lookup to the user object.

Solution

Now Logic is when we create Customer record for account record, then the user in Account Manager field will be automatically added to Account Team of that associated account.

trigger InsertAccountTeam on Customer__c(after insert) {
        List < AccountTeamMember > atm_list = new List < AccountTeamMember > ();
        AccountTeamMember atm = new AccountTeamMember();
        List < AccountShare > newShare = new List < AccountShare > ();
        if (trigger.isInsert) {
            For(Customer__c c: Trigger.new) {
                    if (c.Account_Manager__c != null) {
                        atm = new AccountTeamMember();
                        atm.accountid = c.Account__c;
                        atm.teamMemberRole = 'Account Manager';
atm.UserId = c.Account_Manager__c;
AccountShare shares = new AccountShare();
shares.AccountId = c.Account__c;
shares.UserOrGroupId = c.Account_Manager__c;
shares.AccountAccessLevel = 'Read/Write';
shares.OpportunityAccessLevel = 'Read Only';
shares.CaseAccessLevel = 'Read Only';
newShare.add(shares);
atm_list.add(atm);
}
}
if (atm_list != null)
    insert atm_list;
if (newShare != null && newShare.size() > 0)
    List < Database.saveresult > sr = Database.insert(newShare, false);
}
}

Question 18: The above trigger(Trigger Question 17) Logic is when we create Customer record for account record, then the user in Account Manager field will be automatically added to Account Team of that associated account.

Solution
Now the following trigger logic is when we update the user in the “Account Manager”, the Account team will be updated automatically.

trigger UpdateAccountTeam on Customer__c(before update) {
        List < AccountTeamMember > atm_list = new List < AccountTeamMember > ();
        AccountTeamMember atm = new AccountTeamMember();
        List < AccountShare > newShare = new List < AccountShare > ();
        if (trigger.isupdate) {
            if (trigger.isbefore) {
                Set < Id > setAccIds1 = new Set < Id > ();
                Set < Id > setDelATM = new Set < Id > ();
                Map < id, Set < Id >> mapAccMgrs = new Map < id, Set < Id >> ();
                for (Customer__c c: Trigger.new) {
                    if (trigger.oldmap.get(c.Id).Account_Manager__c != c.Account_Manager__c &&
                        c.Account_Manager__c != null) {
                        setAccIds1.add(c.Account__c);
                    }
                }
                List < Customer__c > listPLAccMgrs = “select id, Account_Manager__c, Account__c
                from Customer__c where Account__c in: setAccIds1 and id not in: trigger.newmap.keyset()”;
                if (listPLAccMgrs != null && listPLAccMgrs.size() > 0) {
                    for (Customer__c c: listPLAccMgrs) {
                        Set < Id > idMgrs = mapAccMgrs.get(c.Account__c);
                        if (null == idMgrs) {
                            idMgrs = new set < Id > ();
                            mapAccMgrs.put(c.Account__c, idMgrs);
                        }
                        idMgrs.add(c.Account_Manager__c);
                    }
                }
                Map < id, List < AccountTeamMember >> mapStdAccTeam = new
                Map < id, List < AccountTeamMember >> ();
                List < AccountTeamMember > listStdAcc Team = “select id, UserId, AccountId from
                AccountTeamMember where AccountId in: setAccIds1“;
                if (listStdAccTeam != null && listStdAccTeam.size() > 0) {
                    for (AccountTeamMember recAccTeam: listStdAccTeam) {
                        List < AccountTeamMember >
                            listStdAccTeamMap = mapStdAccTeam.get(recAccTeam.AccountId);
                        if (null == listStdAccTeamMap) {
                            listStdAccTeamMap = new List < AccountTeamMember > ();
                            mapStdAccTeam.put(recAccTeam.AccountId, listStdAccTeamMap);
                        }
                        listStdAccTeamMap.add(recAccTeam);
                    }
                }
                system.debug('***********' + mapAccMgrs);
                for (Customer__c c: Trigger.new) {
                    if (trigger.oldmap.get(c.Id).Account_Manager__c != c.Account_Manager__c &&
                        c.Account_Manager__c != null) {
                        List < AccountTeamMember >
                            listAccTeam = mapStdAccTeam.get(c.Account__c);
                        Set < Id > idMgrs = mapAccMgrs.get(c.Account__c);
                        if (listAccTeam != null && listAccTeam.size() > 0) {
                            if (idMgrs != null && idMgrs.size() > 0 &&
                                !(idMgrs.Contains(trigger.oldmap.get(c.Id).Account_Manager__c))) {
                                for (AccountTeamMember recATM: listAccTeam) {
                                    if (recATM.UserId == trigger.oldmap.get(c.Id).Account_Manager__c)
                                        setDelATM.add(recATM.Id);
                                }
                            } else if (idMgrs == null) {
                                for (AccountTeamMember recATM: listAccTeam)
                                    setDelATM.add(recATM.Id);
                            }
                        }
                        atm = new
                        AccountTeamMember(accountid = c.Account__c, teamMemberRole = 'Account
                            Manager ',UserId=c.Account_Manager__c);
                            AccountShare shares = new AccountShare(); shares.AccountId = c.Account__c; shares.UserOrGroupId = c.Account_Manager__c; shares.AccountAccessLevel = 'Edit'; shares.OpportunityAccessLevel = 'None'; newShare.add(shares); atm_list.add(atm);
                        }
                    }
                    List < AccountTeamMember > listDelATM = “select id from AccountTeamMember
                    where id in: setDelATM”;
                    if (listDelATM != null && listDelATM.size() > 0)
                        delete listDelATM;
                    if (atm_list != null)
                        insert atm_list;
                    if (newShare != null && newShare.size() > 0)
                        List < Database.saveresult > sr = Database.insert(newShare, false);
                }
            }

Question 19: The trigger question 17 Logic is when we create Customer record for account record, then the user in Account Manager field will be automatically added to Account Team of that associated account.

Solution
Now the following trigger gives the logic about when we delete the “Customer” of that account, then the user will deleted automatically from the Account Team of that account.

trigger DeleteAccountTeam on Customer__c(before delete) {
    List < AccountTeamMember > atm_list = new List < AccountTeamMember > ();
    AccountTeamMember atm = new AccountTeamMember();
    List < AccountShare > newShare = new List < AccountShare > ();
    if (trigger.isdelete) {
        set < id > setAccids = new set < id > ();
        Set < Id > setDelATM = new Set < Id > ();
        Map < id, Set < Id >> mapAccMgrs = new Map < id, Set < Id >> ();
        for (Customer__c c: Trigger.old) {
            setAccids.add(c.Account__c);
        }
        List < Customer__c > listPLAccMgrs = “select id, Account_Manager__c, Account__c from
        Customer__c where Account__c in: setAccids and id not in: trigger.oldmap.keyset()”;
        if (listPLAccMgrs != null && listPLAccMgrs.size() > 0) {
            for (Customer__c c: listPLAccMgrs) {
                Set < Id > idMgrs = mapAccMgrs.get(c.Account__c);
                if (null == idMgrs) {
                    idMgrs = new set < Id > ();
                    mapAccMgrs.put(c.Account__c, idMgrs);
                }
                idMgrs.add(c.Account_Manager__c);
            }
        }
        Map < id, List < AccountTeamMember >> mapStdAccTeam = new
        Map < id, List < AccountTeamMember >> ();
        List < AccountTeamMember > listStdAccTeam = “select id, UserId, AccountId from
        AccountTeamMember where AccountId in: setAccids”;
        if (listStdAccTeam != null && listStdAccTeam.size() > 0) {
            for (AccountTeamMember recAccTeam: listStdAccTeam) {
                List < AccountTeamMember >
                    listStdAccTeamMap = mapStdAccTeam.get(recAccTeam.AccountId);
                if (null == listStdAccTeamMap) {
                    listStdAccTeamMap = new List < AccountTeamMember > ();
                    mapStdAccTeam.put(recAccTeam.AccountId, listStdAccTeamMap);
                }
                listStdAccTeamMap.add(recAccTeam);
            }
        }
        for (Customer__c c: Trigger.old) {
            List < AccountTeamMember >
                listAccTeam = mapStdAccTeam.get(c.Account__c);
            Set < Id > idMgrs = mapAccMgrs.get(c.Account__c);
            if (listAccTeam != null && listAccTeam.size() > 0) {
                if (idMgrs != null && idMgrs.size() > 0 &&
                    !(idMgrs.Contains(trigger.oldmap.get(c.Id).Account_Manager__c))) {
                    for (AccountTeamMember recATM: listAccTeam) {
                        if (recATM.UserId == trigger.oldmap.get(c.Id).Account_Manager__c)
                            setDelATM.add(recATM.Id);
                    }
                } else if (idMgrs == null) {
                    for (AccountTeamMember recATM: listAccTeam)
                        setDelATM.add(recATM.Id);
                }
            }
        }
        List < AccountTeamMember > listDelATM = “select id from AccountTeamMember
        where id in: setDelATM”;
        if (listDelATM != null && listDelATM.size() > 0)
            delete listDelATM;
    }
}

Question 20: When we create the Opportunity with the Probability=20, then the opportunity owner will be automatically added to Account Team of the associated account for that Opportunity.

Solution

trigger UpdateATMwithOwneronOptyCreate on Opportunity(after insert, after update) {
        List < AccountShare > list_share = new List < AccountShare > ();
        List < AccountTeamMember > list_atm = new List < AccountTeamMember > ();
        for (Opportunity opp: Trigger.New) {
            if (opp.Probability == 20) {
                AccountTeamMember atm = new AccountTeamMember();
                atm.accountid = opp.accountid;
                atm.teamMemberRole = 'Account Manager';
                atm.UserId = opp.Ownerid;
                AccountShare share = new AccountShare();
                share.AccountId = opp.Accountid;
                share.UserOrGroupId = opp.OwnerId;
                share.AccountAccessLevel = 'Read/Write';
                share.OpportunityAccessLevel = 'Read Only';
                share.CaseAccessLevel = 'Read Only';
                list_atm.add(atm);
                list_share.add(share);
            }
        }
        if (list_atm != null)
            insert list_atm;
        if (list_share != null && list_share.size() > 0)
            List < Database.saveresult > sr = Database.insert(list_share, false);

Conclusion

This 20 questions will help you to master any trigger based questions and ace your salesforce apex trigger interview questions and assessments. We will try to bring some more unique trigger questions stay updated by visiting our page once a week.

Leave a Comment

twenty − three =