Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • 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
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • 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
    • Marketplace
    • Advertise With Us
    • 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, 1 month 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, 1 month ago by  Jade.
      • This reply was modified 7 years, 1 month ago by  Jade.
      • This reply was modified 7 years, 1 month ago by  Jade.
    • 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, 1 month 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

    application solution

    Popular Salesforce Blogs

    A Complete Introduction to Heroku

    Blog in Salesforce Products

    There are still minutes when I specify Heroku just to be met with a blank gaze. Which is really awful on the grounds that Heroku…

    AWS, Azure, Cloud Computing, Heroku, IaaS
    Abhay Sep 15, 2017
    5,956  Views
    Lightning Experience

    Visualforce in Lightning Experience | Salesforce Guide

    Blog in Lightning, Visualforce

    Although Lightning Experience introduces a whole new user interface to your Salesforce organisation, your Visualforce apps will continue to function. Many Visualforce pages operate in…

    ApexPage, API, Calls, Class, Code
    Arpit Singhal Mar 22, 2022
    4,551  Views
    Why Salesforce Manufacturing Cloud Monitors Vendor Parts Arrivals Before Lines Stop

    Why Salesforce Manufacturing Cloud Monitors Vendor Parts Arrivals Before Lines Stop

    Blog in Salesforce

    Factories lose millions from production stops. Vendor delays cause 30% of downtime. Manufacturers track 5000+ parts yearly. Late deliveries hit 25% of orders. Production lines…

    Salesforce, Salesforce Development
    George Mar 17, 2026
    55  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®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.