Quick HowTo - Salesforce Apex Trigger
Do you know how easy it is to create custom trigger actions on your Salesforce objects? If you don't, scroll down through our step-by-step screenshots.
Overview
In this post we show you how to use a checkbox to run an Apex Trigger that clears all the values of a list of object properties. In this scenario, we are using the concept of "Feature Flags" being cleared by a checkbox that essentially mimics the behavior of a button.
Some Context
Apex triggers allow you to write custom object logic. Custom code helps you automate trivial tasks as well as complex business rules. Sometimes custom business logic should be developed using low-code Workflow Automation while sometimes its code through Apex Triggers.
Step 1 - Add New Custom Fields
Step 2 - Accept All Defaults
Step 3 - Update Page Layout
It helps to have a section dedicated to "trigger actions". This can serve as your section that contains checkboxes that behave like buttons.
Step 4 - View Your Custom Flags (You'll use this as a reference)
Step 5 - Write Apex Code & Mark "Is Active"
trigger AccountTriggerActions on Account(before insert, before update) {
for(Account a : Trigger.New) {
If(a.Action_ClearAllFeatures__c){
// primary logic - disable account features
a.Feature_AB_Testing__c = false;
a.Feature_AlphaTester__c = false;
a.Feature_DarkMode__c = false;
a.Feature_LoadBalancer__c = false;
// reset trigger action checkbox value
a.Action_ClearAllFeatures__c = false;
}
}
}
Step 6 - Test It Out
Things To Keep In Mind
This approach should only be used in appropriate use cases. You should never generalize and assume that you can resolve all automation using this approach.
Why Is Salesforce The #1 CRM?
Extensibility. This article is a perfect example showcasing how you can extend CRM functionalities and capabilities to meet even the most complex business scenarios while gracefully handling simple use cases.
Additional Info
Subsari Tech Consulting - Dallas TX
https://www.dhirubhai.net/company/subsari/about/
Feedback
Please continue to send us your comments and thoughts.