Toggle Side Panel

  • Blogs
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How to Write Salesforce Triggers for this Scenario?

    Tagged: Account, Contact, Salesforce Help and Training, Salesforce Triggers

    • Salesforce® Discussions

      How to Write Salesforce Triggers for this Scenario?

      Posted by Pavan on February 10, 2019 at 1:25 PM

      How to write a triggers for this scenario?

      1.Create a date field on Contact - MyDate
      Create a date field on Account - MyDate

      Write a trigger, Whenever a contact record is inserted under an account then the
      new record's MyDate field should be 1 month ahead that of its parent Account's MyDate field. And if multiple
      contact records
      are inserted under one account then the contact records's MyDate field should be 1 month ahead of each other.
      For example:
      There are 3 records of accounts - A1 (1st July 2017), A2 (1st Jan 2018), A3 (1st Oct 2017)
      Now we are inserting multiple contacts under these 3 accounts like this:
      A1 --> C1, C2, C3
      A2 --> C4, C5, C6, C7
      A3 --> C8, C9

      So the MyDate fields of new contact records should be like this:
      C1 - 1st Aug 2017
      C2 - 1st Sep 2017

      Jade replied 7 years, 7 months ago 2 Members · 4 Replies
      • Account
      • Contact
      • Salesforce Help and Training
      • Salesforce Triggers
    • 4 Replies
    • Jade

      Member
      February 12, 2019 at 7:48 AM

      Hi Pavan,

      You can achieve it in before Insert and before update of the contact.

      PFB the code for the same -

      public class ContactTriggerHelper {
          
          /* *call this method on before Insert and before upate of Contact
           * before insert -
           * Parameters - Trigger.New, null
           * 
           * before update -
           * Parameters - Trigger.New, Trigger.oldMap
           *  */
          public static void contactDateUpdate(List<Contact> newList, Map<Id, Contact> oldMap){
              Map<Id, List<Contact>> 	accContactMap 	= new Map<Id, List<Contact>>();
              
              for(Contact con : newList){
                  if(oldMap == null && con.AccountId != null){
                      if(accContactMap.containsKey(con.AccountId)){
                          accContactMap.get(con.AccountId).add(con);
                      }else{
                          accContactMap.put(con.AccountId, new List<Contact>{con});
                      }
                  }else if(oldMap != null && con.AccountId != null && (con.MyDate__c == null || con.MyDate__c != oldMap.get(con.Id).MyDate__c)){
                      if(accContactMap.containsKey(con.AccountId)){
                          accContactMap.get(con.AccountId).add(con);
                      }else{
                          accContactMap.put(con.AccountId, new List<Contact>{con});
                      }
                  }
              }
              
      		Map<Id, Account> accountMap	= new Map<Id, Account>([SELECT Id, MyDate__c FROM Account WHERE Id IN: accContactMap.keySet()]);        
              for(Id accId : accContactMap.keySet()){
                  for(Contact con : accContactMap.get(accId)){
                      con.MyDate__c = accountMap.get(accId).MyDate__c.addMonths(1);
                  }
              }
          }
      }

       

      I hope this will help.

      Thanks

      Avinash

      • This reply was modified 7 years, 7 months ago by  Jade.
      • This reply was modified 7 years, 7 months ago by  Jade.
      • This reply was modified 7 years, 7 months ago by  Jade.
    • [adinserter block='9']
    • Jade

      Member
      February 12, 2019 at 8:30 AM

      Hi Pavan,

      Above was a rough code to achieve the requirement.. Although that will also work in every scenario.

      Please find more optimized code for the same -

      public class ContactTriggerHelper {
          
          /* *call this method on before Insert and before upate of Contact
          * before insert -
          * Parameters - Trigger.New, null
          * 
          * before update -
          * Parameters - Trigger.New, Trigger.oldMap
          *  */
          public static void contactDateUpdate(List<Contact> newList, Map<Id, Contact> oldMap){
              Set<Id> accIds = new Set<Id>();
              for(Contact con : newList){
                  if(con.AccountId != null){
                      accIds.add(con.AccountId);
                  }
              }
              
              Map<Id, Account> accountMap	= new Map<Id, Account>([SELECT Id, MyDate__c FROM Account WHERE Id IN: accIds]);
              for(Contact con : newList){
                  if(oldMap == null && accountMap.get(con.AccountId).MyDate__c != null ){
                  	con.MyDate__c = accountMap.get(con.AccountId).MyDate__c.addMonths(1);
                  }else if(oldMap != null && con.AccountId != null && (con.MyDate__c == null || con.MyDate__c != oldMap.get(con.Id).MyDate__c || con.MyDate__c != accountMap.get(con.AccountId).MyDate__c)){
                      con.MyDate__c = accountMap.get(con.AccountId).MyDate__c.addMonths(1);
                  }
              }
          }
      }

      Hope this will help.

      Thanks

      Avinash

      • This reply was modified 7 years, 7 months ago by  Jade.
    • Pavan

      Member
      February 12, 2019 at 2:59 PM

      Thanks a lot for your kind help...

    • Jade

      Member
      February 13, 2019 at 6:25 AM

      No Problem 🙂

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    Popular Salesforce Blogs

    Azure with Salesforce for Customer Engagement and Operational Excellence

    Blog in Others

    The retail landscape is in constant flux, driven by evolving customer expectations and disruptive technologies. Staying ahead requires more than just keeping up; it demands…

    Azure, CRM, Microsoft Azure, Salesforce
    Cloudy Aug 1, 2025
    362  Views

    Migrating to Salesforce: Key Steps for a Seamless Transition

    Blog in Salesforce

    Migrating to Salesforce can transform how your business operates. Whether you're looking to streamline your sales process, improve customer service, or better analyze your data,…

    Business Growth, Business Transformation, CRM Migration, Custom Integrations, Custom Solutions
    Winobell Sep 16, 2024
    677  Views
    Batchable Classes

    Guide To Writing Batchable Classes In Salesforce

    Blog in Salesforce Apex

    Ever heard of “Batch classes” in Salesforce and felt a bit puzzled? Don’t worry; it’s simpler than you think! Imagine you’re baking cookies. You don’t…

    Application, approach, Async Apex Job, Batch, Batch Apex Class
    SF Nov 23, 2023
    1,326  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.