36 Trigger Scenarios in Salesforce with Solution – Part 4

Learn with the real-time Trigger Scenarios in Salesforce –

1. Create a fields on Opportunity as Client Contact, Once an Opportunity Client Contact updates then update Account Client Contact with same on Opportunity Client Contact.

Before getting into the trigger we have create a lookup field on both account and opportunity with respect to contact with Name “Client Contact”. Now lets see why are we using “after update”, as mentioned in the question we need to update the Account Client Contact field on update of Opportunity Client Contact fields, so after the opportunity is updated then we would take the value of that particular Client Contact field and assign it to Account Client Contact and update the Account whose Client Contact fields are changed so we need to use “after update” trigger.

Learn more about Salesforce Flow Bootcamp

In the below code we are looping all the Opportunities which are getting updated then we are checking if the Client Contact field is empty or not and also if AccountId field is Empty or not, if the they are not empty as per the question we are checking if the value of Client Contact is changing during the update i.e, we are checking if the old value of Client Contact field is not equal to new value of Client Contact field. If there is a change then we are added that particular Opportunity Id to a Set<Id>. Then we are getting all the Opportunities whose Id is present in the Set<Id> (i.e, oppId) using query and also getting that particular Opportunities AccountId and Account’s Client Contact also in the query. Later we are checking if the list is not empty, if not empty then we are looping it and checking if the Opportunity Client Contact is not equal to Opportunities Account Client Contact. If its not equal then we are creating a new account and assigning the AccountId of Opportunity and assigning the Opportunity Client Contact that particular Account and adding that Account to separate List<Account> for updating it later after all the Opportunity are looped we are checking if the List<Account> (i.e, accList this is where we added the Account whose Client Contact field was changed ) is empty or not, if its not empty then we are updating that List<Account>.

Register for Salesforce Flow Bootcamp

2. Create an asset when create an OpportunityLineItem with associated Account.

In the above question it tells us that if a OpportunityLineItem with associated Account is created then 0an Assest has to be created. So it simply means that after create of OpportunityLineItem we should run this trigger, hence we will create a trigger for ” after insert”.

Here we need to get the OpportunityLineItem’s Opportunity Id first and then check if AccountId field for that particular Opportunity is not null then only we will be creating the Asset and inserting it. So first we need to loop the OpportunityLineItem which is getting inserted and then get its Opportunity Id and put it in a Set<Id>, then we get the List<Opportunity> whose Ids are present in Set<Id> (i.e, oppId variable) using query.Then we are checking if that List<Opportunity> is null or not, if not we are looping all those List<Opportunity> and checking if the AccountId field is empty or not. If the AccountId field is not empty then we are creating a new Asset and filling the mandatory fields and assigning the AccountId field same as Opportunity AccountId and then adding that Asset to a List<Asset>. Now we check if the List<Asset> is empty or not, if not then we are inserting that List<Asset>.

3. When Contact update collect all opportunity Amount And Update this amount on Account Level Field (Total Opportunity Amount).

Before we even move for the trigger we need to create a field on Account for storing the sum of  Amount field of Opportunity. So we need to create a field on Account with Name “Complete Opportunity Amount” with datatype as Currency. Now we are ready to write the Trigger, let me tell you why we are using “after update”. So as given in the question we need to update Account field on updating Contact (i.e, there is AccountId field on Contact so after updating the Contact we are going to update Account) that means Contact has to be updated first then we can get the Account Id from Account field and then update Account object. This the reason we are using “after update”.

In the below code we are first looping the Contacts which are getting updated and checking if the AccountId field is empty or not and also we checking if the value of the AccountId field is changing (because there might be a chance where the user might not change the Account instead changes some other field and update it  in that case performing all this will be of waste), if the condition gets satisfies then we are adding that particular Contact’s AccountId in a Set<Id>. Then we are getting a AggregateResult of sum of Amount field of all Opportunity then we are getting the result of it and converting it to Double and storing the result in separate variable. Now we get all the Accounts whose Id is present in Set<Id> through query and then check if its empty or not, if not the we are looping all those Account and assigning the “Complete Opportunity Amount” field with the result of sum of Amount which we stored in a variable(i. e, amt ), then add that particular Account to separate List<Account>. After this we will check if the List<Account> that has to be update is empty or not, if not then we are updating that List<Account>.

4. Once an user is update an email should go to his Manager email Id,
with the total number of Account Owned by him,
You have assigned number of account and Account Contains Number of Contacts
Like

Subject: Your Accounts and Number of Related Contacts

Body : Dear User,
You have assigned number of account and Account Contains Number of Contacts.
Total Accounts = 3
A – 10 (i.e Account Name – Total number of Contacts)
B – 10
C – 10

5. Write a trigger on Opportunity line item , Add two picklist values in Product Family field Electronic and Books and a picklist Product type on Opportunity with same picklist values. Now once you add a line item into Opportunity, check if Opportunity Product Type is same as OpportunityLineItem Product Family let it be insert or add error The Product family does not match.

Before we get into the Trigger part there are some things that has been mentioned in question to do that is, First we need to add Electronic and Books values to the Product Family field in Product Object. So go to Setup > Object Manager and in search box search for “Product” and Click on Product > Fields and Relationships then edit the Product Family field under “Product Familt Picklist Values” Click new and add “Electronic” and “Books” and Save it. After this we need to create a new field on Opportunity, go to Setup >Object Manager and in search box search for “Opportunity” and Click on Opportunity > Fields and Relationships then click on New and add a field with Datatype as “Picklist”, Label “Product type” and Values as “Electronic” and “Books” and Save it.

Now lets decide with when the trigger has to be called, as per the question when insertion is happening we need to check if the Opportunity Product Type is same as OpportunityLineItem’s Product Family, if not then we should show the Error Message. This clearly means that the Trigger should be called “before insert” because the question tells us to show error during insertion if the Product Family doesn’t match.

In the below trigger we are first looping all the OpportunityLineItem which is getting inserted and getting its OpportunityId and Product2Id into two Set<Id>called oppId and proId respectively. After that we are getting all the Opportunities whose Ids contain in oppId and checking if that List<Opportunity> is not empty , then we are looping that List<Opportunity> and checking if the Product Type field is null or not. If not then we are adding the Opportunity Id and its Product Type to a Map<Id,String> called oppIdProductTypeMap. After that we are getting all the Products whose Ids contain in proId and checking if that List<Product> is not empty , then we are looping that List<Product> and checking if the Product Family field is null or not. If not then we are adding the Product Id and its Product Family to a Map<Id,String> called proIdFamilyMap. Now after all this is done we are again looping the OpportunityLineItem which are getting inserted and checking if the OpportunityId is present in the Map<Id,String> oppIdProductTypeMap, if present then we are getting its value(previously we have passed only the Product Type to the values of the Map<Id,String>) and passing it to a variable “producttype” and then checking if the ProductId is present in the Map<Id,String> proIdFamilyMap, if present then we are getting its value(previously we have passed only the Product Family to the values of the Map<Id,String>) and passing it to a variable “family”. Now we will check if the “producttype” is not equal to “family”, if its not equal the we will add Error while insertion showing “The Product family does not match.”.

6. Once Opportunity Line Item is added to Opportunity with the specified product, then Product Total Quantities must be deduct from the Product Object
e.g. If we have Total Quantity 100 on Product object and we have added 50 in line items , then product must be update 100-50 = 50
Create Total Quantity, Available Quantity Fields on Product2 object.

Firstly we need to create a field on Product Object with Label “Total Quantities” and Datatype Number. Now lets get see when the Trigger has to be called, as per the question once the OpportunityLineItem is inserted successfully then the we should deduct the total quantity of a product so we need to use “after insert” here.

Lets go to the Trigger part now, first we are looping all the inserted OpportunityLineItem and getting its ProductId to Set<Id>. Then we are getting the AggregateResult of OpportunittLineItem’s Quantity by grouping it with ProductId which we added in Set<Id>, now lets loop all the List<AggregateResult> and get the ProductId and sum(Quantity) in separate variables and add the ProductId and sum(Quantity) in  Map<Id,Double> called proIdQuantitySum. Now we would get all the Product List in a List<Product2> and loop all the Products and check if the ProductId contains in the Map<Id,Double> if present then we woulf check if the Product Total Quantity field is null or not, if the field is not null then we will subtract the value of that particular ProductId from the Map<Id, Double> with the Total Quantities field and add that Product in separate List<Product2> called proListToUpdate. Later we will check if the List<Product> called proListToUpdate is null or not, if not then we will update it.

Looking for part 5? – Trigger Scenarios Solution 5 (Coming Soon)

If you didn’t go through the 36 Apex Triggers Scenarios in Salesforce Post Make sure to visit over there to check out the Trigger Scenarios
36 Trigger Scenarios in Salesforce

If you feel the solution can be optimized further please reach out to us.

Get complete Roadmap To Learn Salesforce Admin And Development 👇

Spread the love

Related Posts

3 thoughts on “36 Trigger Scenarios in Salesforce with Solution – Part 4

  1. hii,
    as you written trigger in part4

    3. When Contact update collect all opportunity Amount And Update this amount on Account Level Field (Total Opportunity Amount).

    I have tried the same but it’s not working, Please re-verify that one. So that i’ll learn quick

    1. Hi Govardhan Kumar,

      The trigger does work. When you are updating the Contact please make sure that Account Id field is not empty. Because we are populating the
      Total Opportunity Amount on Contact’s Account field.

      Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *