1. Write a trigger on Opportunity, when an Opportunity will insert an Opportunity Line Item should be insert by default with any of the Products associated with Opportunity.
In the above question we have to insert Opportunity Line Item once the Opportunity is insert, so we are using “after insert” because after the Opportunity is inserted only we can get its Opportunity Id and insert its Opportunity Line Item.
When we insert an Opportunity Line Item it is mandatory to insert the Opportunity Id, Product2 Id (i.e. Product Id), Product Quantity, Price Book Entry Id and Total Price. Since Product2 and PriceBookEntry are related to each other we have to gets Product Id and its related PriceBookEntry Id. Then for each Opportunity getting created we have to create a new Opportunity Line Item and assign values for the mandatory field and Product Opportunity Id should be the Opportunity Id that got newly created. After this add the Opportunity Line Item to a separate list and check if it is null or not if not then we will insert that list of Opportunity Line Item.
2. Write a trigger on Account when an account is update when account type changes send an email to all its contacts that your account information has been changed.
Subject: Account Update Info
Body: Your account information has been updated successfully.
Account Name: XYZ.
In this, we are using “after update” because once the account is updated then only we should send mail, not before it is updated.
Now let’s go to the Trigger explanation, so here we should first check whether the field Type is changing during the update or not. So we are looping all the record which is getting updated and checking if the new value of the Type field is equal to the old value of the Type field. If there is a change in the value of the Type field then we are adding that Account Id to Set<Id> and later getting all the Contacts whose Account Id exists in Set<Id>. Then as usual we are declaring the List of SingleEmailMessage and looping all the Contacts and checking if their Email field is empty or not and if it is not empty then we are declaring the SingleEmailMessage and assigning all the required fields and assigning Subject and Body and ToAddress all and the adding that SingleEmailMessage to list of SingleEmailMessage and checking if the list of SingleEmailMessage is empty or not, if not then we are sending the mails.
3. Write a trigger on the Opportunity line item when a line item deletes delete an opportunity as well.
So this Question tells us to delete the Opportunity once the OpportunityLineItem is deleted that is after the OpportunityLineItem is deleted then we should delete Opportunity so we have to use “after delete”.
Firstly we will loop all the OpportunityLineItem and get its Opportunity Id from it and then get all the Opportunities using those Ids through Query and then check if it is not empty, if not then we will perform delete operation for all the Opportunities which we got through a query.
4. Once an Opportunity line item is created, insert a quotation also.
In the Question, it is mentioned that “Once an Opportunity line item is created” which means after the Opportunity line item is created then we should insert the quotation so for that we have to use “after insert”.
Before we get into an explanation let’s check whether we have a related Quote for the Opportunity. Go to App Launcher -> then go to Opportunities and open one record ->Later check there is a Quotes related Tab. If you did not find any Quotes relate tab then you have to enable Quote Settings. Go to Setup-> Search for “Quote Settings” and click on Quote Settings-> Now click on “Enable”-> it will redirect to a Page Layout Selection then select all the Page layouts and click on “Save”.
Now we are ready to write the Trigger. So the first thing we need is the Opportunity Id of the OpportunityLineItem that is getting inserted. So we loop all the OpportunityLineItem that is getting inserted and get all its Opportunity Ids and get those records, later loop all those Opportunities which we got through query and create a Quote and assign the Opportunity Id with the Opportunity Id that is looped and assign the other mandatory fields to add it to list of Quote for later insertion. Now check if the List of Quotes is null or not, if not then insert the List of Quote.
5. When an account BillingCity is updated, update all its contacts MailingCity with account BillingCity.
In the Above Question, we need the Update Contacts MailingCity with its Accounts BillingCity that too only when the BilligCity of Account is Updated. Here we are using “after update” because after updating only we need to change the data of its related Contacts.
Now let’s get to the Trigger, firstly we should check if the BillingCity value is not equal to empty and also check if there is change in the BillingCity then we should get all those Account Ids. Now we should get all the related Contacts of those particular Account Ids using the Query, then loop all the Contacts and check if the Contact’s MailingCity is not equal to Account’s BillingCity, if not equal then we should assign the BillingCity of Account to Contact’s MailingCity and add that Contact to List of Contacts for later update. Now we should check if the List of Contacts is Empty or not, if not then update that List of Contact.
6. Add a field Multi-select Picklist on Account and Opportunity as well and add values A,B,C,D,F. Now if we update an Opportunity with this multi-select value Account should also update with the same picklist values.
In this Question, they have told us to update the Account Field using the Field in Opportunity. We are using “after update because after the Opportunity field is updated then only we have the change the Account field.
The very first thing we have to do is create a “Multi-Select Picklist” on both Account and Opportunity(I have created a “Multi-Select Picklist with name “ABCDF” with values “A,B,C,D,F”), whatever Name you write remember to change its API Name here in the code and make sure that u have added the values “A,B,C,D,F” to that field.
Now let’s get through the Trigger, first, we should see if there is any change in the values of that field during the update, so we loop all the records which are updating and check for it then if there is change then we are adding that Opportunity to a Set<Id> and we not only need the Account Ids we also need the values of that field so we are declaring a Map<Id, String> where Id contains the Account Id and String contains the value of the selected Multi-Select Picklist field. Now we get all the Accounts of that Set<Id> using Query and loop it and check if that particular Id is present in Map, if present get its values and assign it to the Account’s Multi-Select Picklist field. Later add that Account to the List of Accounts for a later update. Now check if the List of Account is empty or not, if not then update the List of Account.
Looking for part 4? – Trigger Scenarios Solution Part 4
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.