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, 8 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, 8 months ago by  Jade.
      • This reply was modified 7 years, 8 months ago by  Jade.
      • This reply was modified 7 years, 8 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, 8 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

    See the differences between С# and Apex with a handy cheat sheet!

    Blog in Coding and Scripting

    Are you starting to code with Apex, Visualforce, or Lightning? If you trail from .Net to Salesforce development, it would be good to start from…

    Force.com, Lightning Component Framework, Salesforce Apex Code, Salesforce Development, Salesforce Lightning Development
    The Welkin Suite Jun 3, 2017
    17,753  Views
    Salesforce Email Studio

    Salesforce Email Studio – Top compelling reasons why you need it

    Blog in Salesforce Products

    Salesforce Email Studio is among Salesforce’s Marketing Cloud tools designed to deliver the right message to the right audience at the right time, run email marketing…

    Cloud Computing, Customer Success, Digital Marketing, Salesforce Advertising Studio, Salesforce email Studio
    LevelShift (formerly DemandBlue) Mar 15, 2019
    3,295  Views
    Salesforce AppExchange

    Why ISVs Must Strive Harder To Be On AppExchange?

    Blog in AppExchange

    Are you on Salesforce AppExchange? How many apps do you have on Salesforce AppExchange? If you are an ISV then you must be pretty aware…

    app development, App on Salesforce AppExchange, AppExchange, Application Development, Apps from AppExchange
    Algoworks Apr 27, 2020
    2,408  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.