Create Contact or Opportunity Based on Checkbox

Smart automation doesn’t always mean doing everything by default. Sometimes, you want control—logic that only fires under the right conditions. In Salesforce, one such scenario is deciding whether to create a Contact or Opportunity based on specific field values at the time of Account creation.

This blog covers a clean, efficient after-insert Apex trigger that checks custom checkboxes on the Account record—Contact__c and Opportunity__c—to determine what should be created. If the Account has Contact__c checked, a new Contact is created. If Opportunity__c is checked and the Account is marked Active, then and only then, a new Opportunity is generated.

This kind of automation reduces unnecessary data clutter, respects business rules, and keeps your CRM lean while still being powerful.

🧠 Why Conditional Record Creation Matters


In many Salesforce implementations, it’s tempting to auto-create related records across the board. But that often leads to:

  • Unused or cluttered data

  • Unwanted automation triggering downstream

  • Loss of flexibility for different business scenarios

By applying field-driven conditions, you make your automation:

  • Smarter and context-aware

  • User-controlled via UI-based inputs

  • More scalable for real-world complexity

  • Aligned with business intent, not just object relationships

This trigger ensures that Contacts and Opportunities are only created when they’re supposed to be, based on user selections or integration flags—making it ideal for both small teams and enterprise setups.

🔍 What This Blog Covers


  • How to conditionally generate related records from a parent object using Apex

  • How to check for custom checkboxes and field values like Active__c = 'Yes'

  • The advantages of using after-insert logic for record relationships

  • How to implement bulk-safe triggers with separate lists for each object type

  • How to ensure clean, modular logic using a handler class

  • Use cases that benefit from this type of automation

The logic ensures that your Salesforce system remains intentional and efficient, while still offering users the benefits of automation where needed.

🎯 Use Cases Where This Trigger Shines


  • Customer onboarding workflows, where some Accounts require Contacts or early-stage Opportunities

  • Sales operations, where only “Active” accounts should generate sales pipeline entries

  • Partner programs, where Opportunities are created only if a checkbox is selected during signup

  • Data imports, where mass Account creation includes flags to control what gets auto-generated

  • Marketing-to-Sales handoffs, where Contact creation is triggered only when a rep opts in

This kind of conditional automation helps maintain clean data hygiene, while offering room for configuration and control over how Salesforce behaves post-record creation.

👨‍💻 Developer & Admin Tips


Want more flexibility? Convert the checkbox and status-based logic into Custom Metadata or Custom Settings so admins can control behavior without touching code. You can also:

  • Enhance naming conventions dynamically (e.g., using Account Name in the related records)

  • Add logic to assign record owners based on territory or user role

  • Extend the pattern to other objects like Case, Task, or custom objects

Always bulk-test this trigger with multiple records at once, especially if you’re working with integrations or batch inserts. Keep governor limits in check by separating your DML operations and ensuring clean list operations.

🎥 See It in Action – YouTube Playlist Included


This automation is featured in the Salesforce Makes Sense YouTube playlist, where you’ll find:

  • Real-time walkthroughs of how the trigger works

  • Code explanations with comments and context

  • Testing tips and deployment considerations

Whether you’re a beginner learning triggers or a seasoned dev looking to optimize your automation logic, this playlist makes it easy to follow and implement in your own org.

Solution:

trigger AccountTrigger on Account (after insert) {
             if(Trigger.isInsert){ if(Trigger.isAfter){
                   AccountTriggerHandler.createContactOrOpp(Trigger.New);
        }
}
}
           public class AccountTriggerHandler {

                public static void createContactOrOpp(List accList){
                          List<Contact> conList= new List<Contact>(); List oppList=new
                          List<Opportunity>(); for(Account acc:accList){ if(acc.Contact__c){
                          Contact con = new Contact(); con.FirstName =
                         ‘con1’; con.LastName = ‘last’; con.AccountId =
                          acc.Id; conList.add(con);
   }
                             if(acc.Opportunity__c && acc.Active__c == ‘Yes’){
                                     Opportunity opp = newOpportunity(); opp.AccountId = acc.Id;
                                     opp.StageName = ‘Prospecting’; opp.CloseDate =
                                     System.today();
                                     opp.Name = ‘Opp1’;
                                     oppList.add(opp);
             }
}
                      if(oppList.size()>0){
                               insert oppList;
   }
                   if(conList.size()>0){
                               insert conList;
           }
      }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)