Learn with the real-time Trigger Scenarios in Salesforce –
1. Create a field on Opportunity as Client Contact, Once an Opportunity Client Contact updates then update Account Client Contact with the same on Opportunity Client Contact.
Before getting into the trigger we have to create a lookup field on both the account and opportunity with respect to contact with the Name “Client Contact”. Now let’s see why are we using “after update”, as mentioned in the question we need to update the Account Client Contact field on the 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 the AccountId field is Empty or not, if 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 the 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 the 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 a 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 creating an OpportunityLineItem with associated Account.
In the above question, it tells us that if a OpportunityLineItem with an associated Account is created then 0an Asset has to be created. So it simply means that after creating 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 the AccountId field for that particular Opportunity is not null then only we will be creating the Asset and insert 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 the 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 fill in the mandatory fields and assign the AccountId field the 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 the 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 the Account field on updating Contact (i.e, there is an 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 is 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 also 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 an AggregateResult of the sum of the Amount field of all Opportunity then we are getting the result of it and converting it to Double and storing the result in a separate variable. Now we get all the Accounts whose Id is present in Set<Id> through query and then check if it’s empty or not, if not we are looping all those Accounts and assigning the “Complete Opportunity Amount” field with the result of the sum of the 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 updated is empty or not, if not then we are updating that List<Account>.
4. Once a user is updated an email should go to his Manager’s email Id,
with the total number of Account Owned by him,
You have assigned a number of accounts and Account Contains a Number of Contacts
Like
Subject: Your Accounts and Number of Related Contacts
Body: Dear User,
You have assigned a number of accounts and Account Contains the 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 the Opportunity line item, Add two picklist values in the Product Family field Electronic and Books, and a picklist Product type on Opportunity with the same picklist values. Now once you add a line item into Opportunity, check if Opportunity Product Type is the same as OpportunityLineItem Product Family let it be inserted or add an error The Product family does not match.
Before we get into the Trigger part there are some things that have been mentioned in the question to do 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 the search box search for “Product” and Click on Product > Fields and Relationships then edit the Product Family field under “Product Family 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 the 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 let’s decide when the trigger has to be called, as per the question when the insertion is happening we need to check if the Opportunity Product Type is the 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 an 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 check 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 check if that List<Product> is not empty, then we are looping that List<Product> and check 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 deducted from the Product Object
e.g. If we have Total Quantity 100 on the Product object and we have added 50 in line items, then the product must be updated 100-50 = 50
Create Total Quantity, and Available Quantity Fields on Product2 object.
Firstly we need to create a field on Product Object with Label “Total Quantities” and Datatype Number. Now let’s get see when the Trigger has to be called, as per the question once the OpportunityLineItem is inserted successfully then we should deduct the total quantity of a product so we need to use “after insert” here.
Let’s 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 would 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
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.
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
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.
Hi Chaitra,
It is working but I think it will be more relevant if it sums up amount only for the related opportunity of account instead of all.
Thanks
Hi Anubhav,
Yes, we do know that it would be relevant if it sums up amount only for the related opportunity of account instead of all but according to the question or the scenario we have to sum up all the opportunity amount. If you want you can change according to your scenario.
Thank You.
Q.5. Write a trigger on the Opportunity line item, Add two picklist values in the Product Family field Electronic and Books, and a picklist Product type on Opportunity with the same picklist values. Now once you add a line item into Opportunity, check if Opportunity Product Type is the same as OpportunityLineItem Product Family let it be inserted or add an error The Product family does not match.
Trigger : before Update
Apex : public static void oppLineItemPicklistHandler(List opitemList ){
for(OpportunityLineItem oItem : opitemList){
if(oItem.Product2.Family != oItem.Opportunity.Product_Type__c ){
oItem.addError(‘Product Type Is Mismatch !!!’);
}
}
}
I think we dont need any soql query and map to compare instead we can directly refer product2 family and opportunity product type for comparision .
let me know will this approach fine .
Hi priyanka Satpute,
I have tried that scenario you have told me but the problem in your case is the error message doesn’t get displayed because “oItem.Product2.Family” and “oItem.Opportunity.Product_Type__c” will be actually showing null value even if it actually contains data in it. As I know this happens because the Trigger.new will only have the values of the single pointer like “oItem.Product2” but doesn’t contain the data for “oItem.Product2.Family”. You can check out once by yourself as well by debugging those “oItem.Product2.Family” and “oItem.Opportunity.Product_Type__c” inside the loop before the if condition for the records which has value for both Product Type and Family.
Thank You.
6. Once Opportunity Line Item is added to Opportunity with the specified product, then Product Total Quantities must be deducted from the Product Object
e.g. If we have Total Quantity 100 on the Product object and we have added 50 in line items, then the product must be updated 100-50 = 50
Create Total Quantity, and Available Quantity Fields on Product2 object.
The solution Provided By This Req . Here You do not have used “Available_Quantity__c” Field And Inside this Field “Total_Quantities__c” We have to First Fill the Value From UI Then Available Quantity Field will be Updated ?
Don’t You Think there Is Some Mistake In this Line Of code
” pro.Total_Quantities__c = pro.Total_Quantities__c-proIdQuantitySum.get(pro.Id);
proListToUpdate.add(pro); ”
Instead Of This i Think This Line Of Code Will be There isn’t It ??
” pro.Available_Quantities__c = pro.Total_Quantities__c-proIdQuantitySum.get(pro.Id);
proListToUpdate.add(pro); ”
“