In the previous blog post/session, we learned the basics of apex trigger. If you have missed the important first step here is the link for you.
Table of Contents
Introduction
Before we start solving the problems, we would like to discuss the mantra to solve any problem. So, here is my mantra that I always use to solve any problem
Mantra to solve any problem
Read the problem, Again read the problem, and Again read the problem
Able to understand the problem?
YES
Ask Question ( The person who has given the problem )
Remember – No questions is a silly or dumb question
Again read the problem
Repeat until not able to understand
Got a good understanding of the problem
Start Solving
Break the problem into multiple small problems ( Apple Problem / Divide & Conquer / Devide, Solve & Merge )
Solve the individual problem
Merge all the problems into a single unit ( Class / Trigger / LWC )
Questions to develop an Apex Trigger
Which object
Account
What Event
before insert
before update
What functionality
You only need to update the Shipping Address information with Billing Address if the Shipping Address is having blank value.
→ Apex Trigger 1
Develop an Apex Trigger so that every time any account is inserted then set the value of the Industry field to Education if the Industry field is blank. Also, check if the Description is blank then set the value for the description field to “Account Description is blank”
Industry = Education
Description = Account Description is blankif Description is null
Questions to be asked for above problem
Which object
Account
What Event
before
Which DML
insert
What functionality
The industry should be an Education
if the Description is black then set the value for the description to “Account Description is blank”
Trigger Code
→ Apex Trigger 1.2
Develop an Apex Trigger so that every time when any account is created or updated then Set the Value of the Billing Address is to Shipping Address.
Shipping Address
ShippingStreet
ShippingCity
ShippingState
ShippingPostalCode
ShippingCountry
Billing Address
BillingStreet
BillingCity
BillingState
BillingPostalCode
BillingCountry
Questions
Which object
Account
What Event
before
Which DML
insert
update
What functionality
Set the Value of the Billing Address to the Shipping Address.
Code
→ Apex Trigger 1.3
Develop an Apex Trigger so that every time when any account is created or updated then Set the Value of the Billing Address is to Shipping Address.
You only need to update the Shipping Address information with Billing Address if the Shipping Address (Compound Field) is having blank value.
Ask Questions if there are any
Shall we need to check all the fields are blank and then only update – YES ( AND && )
OR if any field is blank then update all the fields – YES ( OR || )
OR if any field is blank then only update that field value
nested if else for individual fields
→ Apex Trigger 2
When the Account is Created, create a Task Record under that Account and assign the Task to the Account Owner. Use the below information
Subject – Created from Apex Trigger
Comments – Created from Apex Trigger
Due Date – Todays Date + 7
Status – Not Started
Priority – High
Related To (What) – Account Id
Assigned To (OwnerId) – AccountOwner Id
Questions –
Which object
Account
What Event
after
Which DML
insert
What functionality ( Want to Achieve )
Task Record under that Account and assign the Task to the Account Owner
Assignments
Develop an Apex Trigger that will update the Contact Phone Value to Contact Fax every time a Contact is updated.
Develop an Apex Trigger so that when an opportunity is created and the account is blank, create a task under Opportunity. Hint: – AccountId == null
Subject – Opportunity is created without Account
Description – Opportunity is created without Account, Please assign the Correct account to Opportunity.
Due Date – Todays Date + 7
Status – Not Started
Priority – High
Related To (What) – Opportunity Id
Assigned To (OwnerId) – Opportunity Owner Id
Create an Apex Trigger on Case Object and print all the Trigger Context Variable
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion