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

    application solution

    Popular Salesforce Blogs

    spring 20 release

    Salesforce Spring ’20 Release Notes - Top Features

    Blog in Salesforce Releases

    Salesforce Spring '20 Release have been made universally accessible on February 17, 2020. The Spring '20 discharge conveys extended advancements over the whole Customer 360…

    Aura Components, Best Practices, Customer 360 stage, Lightning Experience, Lightning Experience Configuration
    MOHIT Apr 15, 2020
    3,039  Views
    DML Operations in Apex | All You Need to Know

    DML Operations in Apex: All You Need to Know

    Blog in Salesforce

    In this Blog, we will study DML Operations in Apex. DML is used to insert, update, delete and undelete records.  We can use upsert to…

    Data Type, Database Queries, Database Query Languages, Developers, DML
    bhawana Jan 17, 2023
    2,544  Views
    Inbound web services

    Inbound and Outbound Web Services in Salesforce

    Blog in Others, Salesforce

    Introduction to Inbound web Service It's associate degree inbound call to Salesforce, however outward-bound call to the external system. Here, the external system is the…

    AWS, Chatter Extension, HyperText Transfer Protocol, Inbound Web Services, Internet Services
    Ratnesh Jul 10, 2020
    17,168  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

    © 2025 - 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.