1. Write a trigger on Account, when an account is inserted, automatically account billing address should populate into the account shipping address.
When it comes to Account Billing and Shipping Address, we cannot directly enter the values to the field Address because the Address is split into Street, City, State, PostalCode, and Country. We are using ” before insert ” because we need to change the values of the account which is getting inserted. If we use ” after insert ” then the fields of the account we are inserting will become read-only and we will not be able to update that account. This is the reason we are using ” before insert “.
2. Write a trigger on the Account when the Account is updated check all opportunities related to the account. Update all Opportunities Stage to close lost if an opportunity created date is greater than 30 days from today and stage not equal to close won.
Here we have to update Account related Opportunities based on a condition which is mentioned above in Question. So here we are using ” after update ” because once the Account is updated we are not making changes for the Account which is getting updated instead we are making changes for its related Object Opportunity.
First, we have to get the Account Id’s of the Account which is getting updated in a Set<Id> and write a query to get that particular Account Id’s Opportunities, then loop the list of that opportunities and check for the condition given in question then change the values according to the question and also check for the mandatory fields to update in opportunity. Later update the list of Opportunities for which the changes have been applied by adding those to a separate list and updating that list.
3. Once an Account is inserted an email should go to the System Admin user with specified text below.
An account has been created and the name is “Account Name”.
From the above question, we can clearly come to the conclusion that Email has to be sent after the Account is Inserted, so we use “after insert “. When there is bulk insertion then we have to send a list of emails that is why we are using List<Messaging.SingleEmailMessage>. Since the Email has to be sent to System Administrator, we should first get the Email of the Admin using the query. Then we have to loop the Accounts which are getting inserted, then check whether the email id of the Admin is null or not before creating a mail to send. Later for each Account, we have to add a single mail because when each Account is added the System Admin should get an email. Then pass the required fields for sending mail add it to the List of mails and check if the list of emails is empty or not. After that, we are ready to send the list of emails. We can also check whether the mail is sent or not, if not sent you can also check the errors in the logs using the last ” if ” condition which I have written below code.
4. Once an Account will update then that Account will update with the total amount from All its Opportunities on the Account Level. The account field name would be ” Total Opportunity Amount “.
The first thing we have to see is which trigger to use ” before ” or ” after “, here we have to update the Account itself again so we cannot use the ” after ” trigger because once the Accounts are updated the fields of the Account becomes ” read-only ” i.e, we will not be able to do changes to the Account data. So if we use ” before update ” then we will be able to make changes to the Account fields later it will get updated by default. This is the reason we are using ” before update “.
In the below code first, we are getting the list of Account Id’s to get its related Opportunities. We have created a Map<Id,Double> where Id contains the Account Id and Double contains the sum of Amount of a particular Account’s related Opportunities so that it will be easy for us to insert the sum of amount according to the Account Id’s. Then we are getting the “Aggregate Result” i.e, we use this when we use sum or average in a query. We are putting all the data of the Aggregate Result to the Map which we have written before. Later we are looping the Accounts which are getting updated and check if that Account Id is there in Map, if it is there then we are changing the value of the ” Total Amount Opportunity ” field in Account.
5. Create a field on Account Named (Client Contact lookup to Contact). Once an Account is inserted a Contact will create with the name of the Account and that Contact will be the Client Contact on the Account.
Here we are using “after insert” because here the condition is first we should insert contact and then a related contact with the same name as account name, so to get the Account Id to make its related contact first the Account has to be inserted then only we will be able to get the Account Id. So we have to use ” after insert”.
We are looping the Accounts which are getting inserted and then also creating a Contact that has the same name as the Account and we are making it related to that particular Account only. Immediately after this, we are inserting those Contacts. Then we are getting the records of the inserted Account to put it into the Map<Id,Account> where Id contains Account Id and Account contains that particular Id’s records. After this, we are looping the inserted Contact and checking if the Contact’s Account Id is present in the Map. If present we are getting that particular Account’s record and given the Contact Id value to the field “Client Contact” of Account. Then we are adding it to a separate list to update it later. And finally, we are updating the Accounts for which the changes have been done.
6. Create a field on Opportunity Line item(Serial No (Text)) and populate increment values once an Opportunity Line Item is added. Let’s say if we add 3 products then the sequence would be “1,2,3”. Now if we delete 2 and again add one more product this must be 4 irrespective of the deleted sequence number.
The very first thing we have to do is create a field with the name “Serial No” of datatype “Text” in an “Opportunity Product” (also called as OpportunityLineItem). Later check whether there is “Product” on the related tab of “Opportunity”. So when you add a new “Product” in a particular Opportunity this Trigger is going to work. And also we will write a trigger on insert because the question tells on adding a product.
Here we are using “before insert” because when we add a new Product on a particular Opportunity then we also need to update the Serial No field of that particular OpportunityLineItem. And if we use ” after insert ” then the OpportunityLineItem field will become read-only and we will not be able to update that OpportunityLineItem.
In the below code we are getting all the OpportunityId in one Set so that we can get all the OpportunityLineItem of that particular Opportunity and the Serial No of that Opportunity Product. We are also using Map<Id, String> where Id contains OpportunityId and String contacts the new Serial No of that particular Opportunity Product. We are looping the OpportunityLineItem’s which we got through the query and checking if the Serial NO field is null, if it is null then we are making the Serial No field as 1. If not then we are getting the last number of the old value of the Serial No and converting it to Integer because we need to Increment it and append it. Then we are making chances in the Serial No field then adding the OpportunityLineItem to List so that we can update it later, and also adding the values to the Map. After this, we are updating the OpportunityLineItem’s which was previously present, and in which we made changes for the Serial No. Now the only thing left is changing the Serial No field of the newly inserting OpportunityLineItem, so we loop the inserting List of OpportunityLineItem check if its OpportunityId is present in Map, if present then get that Map values and place it to Serial No field else we are just passing 1 to that Serial No field.
Looking for part 2? – Trigger Scenarios Solution Part 2
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.