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 › Salesforce Trigger Question

    Tagged: Account Field, Count, Gender Field, Salesforce Fields, Salesforce Trigger

    • Salesforce® Discussions

      Salesforce Trigger Question

      Posted by pawan makkar on August 2, 2017 at 1:07 PM

      create two fields in account one is no of male and second is no of females.and create a one fields in contact gender. write a trigger to count the no of male and females and dispaly on account object

      • This discussion was modified 9 years ago by  pawan makkar.
      • This discussion was modified 9 years ago by  Forcetalks.
      • This discussion was modified 9 years ago by  Forcetalks.
      shariq replied 9 years ago 2 Members · 1 Reply
      • Account Field
      • Count
      • Gender Field
      • Salesforce Fields
      • Salesforce Trigger
    • 1 Reply
    • shariq

      Member
      August 14, 2017 at 10:06 AM

      Hi Pawan,

      The trigger provided below will work for newly created account and contact's records, set the default value 0 for the two number fields created on account. This trigger will work for insert/update/delete on contact.

      trigger Count_MalesAndFemales on Contact (after insert, after update, before delete)
      {
      //System.debug('ggg'+Trigger.Old[0].AccountId);
      List<Account> accList = new List<Account>();
      Map<Id,Contact> mapIdVsCon = new Map<Id,Contact>();

      if(Trigger.isAfter&&Trigger.isInsert)
      {
      List <Contact> conListNew = [SELECT Gender__c, AccountId, Account.news__No_Of_Males__c, Account.news__No_Of_Females__c FROM Contact WHERE AccountId !=Null And Id IN : Trigger.new ];
      for(Contact con : conListNew)
      {
      if(con.Gender__c == 'Male')
      {
      con.Account.No_Of_Males__c++;
      System.debug('sss'+con.Account.No_Of_Males__c);
      }
      else if(con.Gender__c == 'Female')
      {
      con.Account.No_Of_Females__c++;
      System.debug('hhh'+con.Account.No_Of_Females__c);
      }
      mapIdVsCon.put(con.AccountId,con);
      //accList.add(acc);
      }
      }

      if(Trigger.isAfter&&Trigger.isUpdate)
      {
      List <Contact> conListNew = [SELECT Gender__c, AccountId, Account.news__No_Of_Males__c, Account.news__No_Of_Females__c FROM Contact WHERE AccountId !=Null And Id IN : Trigger.new ];
      for(Contact con : conListNew)
      {
      if(Trigger.newMap.get(con.Id).Gender__c == 'Male' && Trigger.oldMap.get(con.Id).Gender__c == 'Female')
      {
      con.Account.No_Of_Females__c--;
      con.Account.No_Of_Males__c++;
      }
      else if(Trigger.oldMap.get(con.Id).Gender__c == 'Male' && Trigger.newMap.get(con.Id).Gender__c == 'Female')
      {
      con.Account.No_Of_Females__c++;
      con.Account.No_Of_Males__c--;
      }
      mapIdVsCon.put(con.AccountId,con);
      //accList.add(acc);
      }
      }

      if(Trigger.isBefore&&Trigger.isDelete)
      {
      List <Contact> conListOld = [SELECT Gender__c, AccountId, Account.news__No_Of_Males__c, Account.news__No_Of_Females__c FROM Contact WHERE AccountId !=Null And Id IN : Trigger.old ];
      for(Contact con : conListOld)
      {
      System.debug('sss'+con);
      System.debug('sss'+Trigger.oldMap.get(con.Id));
      if(con.Gender__c == 'Female')
      {
      System.debug('sss'+Trigger.oldMap.get(con.Id));
      con.Account.No_Of_Females__c--;
      }
      else if(con.Gender__c == 'Male' )
      {
      System.debug('sss'+Trigger.oldMap.get(con.Id));
      con.Account.No_Of_Males__c--;
      }
      mapIdVsCon.put(con.AccountId,con);
      //accList.add(acc);
      }
      }

      for(Id ide : mapIdVsCon.keySet())
      {
      Account a = new Account();
      a.Id = ide;
      a.news__No_Of_Males__c = mapIdVsCon.get(ide).Account.news__No_Of_Males__c;
      a.news__No_Of_Females__c = mapIdVsCon.get(ide).Account.news__No_Of_Females__c;
      accList.add(a);
      System.debug('key===='+ide);
      System.debug('id====='+mapIdVsCon.get(ide));
      }
      update accList;
      }

       

    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

    BOFC for Consultants: Speed Up Metadata Projects

    Blog in Others

    If you’re a Salesforce Consultant, chances are you’ve juggled multiple orgs, tight deadlines, and metadata-heavy tasks — all at once. Whether you’re managing object structures,…

    AYAN Jul 30, 2025
    356  Views

    Salesforce Dynamic Forms: How to Get Started?

    Blog in Salesforce

    Salesforce Dynamic Forms improves the productivity of organizations and provides users only what they require, transforming static data entry into an intuitive experience. Plus, dynamic…

    Cases, CheckBox, Code, Configure, Contacts
    Sparkybit Apr 17, 2023
    1,404  Views

    Salesforce Chatbots are Changing the Customer Service Game

    Blog in Salesforce

    Chatbot, A brief A chatbot (short for "chat robot") is computer software that replicates genuine interaction via voice or text. Such programs are adaptable and…

    Apps, Artificial Intelligence, B2B, B2C, Building Strategies
    Apphienz Oct 21, 2021
    3,789  Views

    Popular Salesforce Videos

    myTrailhead Product Filters | Salesforce Tutorial Video

    myTrailhead Product Filters | Salesforce Tutorial Video

    Video in Others, Salesforce Training, Trailhead

    Salesforce myTrailhead has now released their next update in product filters. You can now create your own product filters that are relevant to your business…

    Salesforce Training, Salesforce Tutorial, Trailhead, Salesforce Learning, Business
    Stimulus Consulting Jan 20, 2021
    1,808  Views
    Getting Business Insights From Telephony Data in Salesforce

    Getting Business Insights From Telephony Data in Salesforce

    Video in Sales

    Big Data, Data Analytics and Data mining are big terms that often sound like they are only for the ‘giant corporation‘ and put people off…

    salesforce, Data Integration, Saleforce Big Data, CTI, phone
    Ian Mar 19, 2020
    1,582  Views
    The History of Salesforce

    The History of Salesforce

    Video in Salesforce Stories

    Well, it's about time to talk about the history of the company that became a $10 Billion Dollar Industry from an efficient CRM. Watch this…

    Salesforce Training, CRM, Learn Salesforce, Salesforce Video, Salesforce History
    Algoworks Sep 23, 2020
    2,151  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.