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

    Salesforce AppExchange

    Publishing an App on Salesforce AppExchange

    Blog in Others

    Introduction There are three major steps to be followed to get your App on Salesforce AppExchange. Plan ( what type of App you want to…

    App, App on Salesforce AppExchange, App Payments, AppExchange Applications, AppExchange for Security Review
    Kirandeep Oct 22, 2020
    8,068  Views
    AMAZON CTI

    Amazon CTI Adapter and Service Cloud Voice: What’s Your Choice?

    Blog in Salesforce integration

    Service Cloud Voice (SCV) was released in 2020 to replace and extend the functionality of the Amazon Connect CTI Adapter, which is used to link…

    Appex, AppExchange, AppExchange Packages, Automation, AWS
    Twistellar May 29, 2023
    1,859  Views

    No Code Salesforce and JotForm Integration | Salesforce Guide

    Blog in Salesforce

    One of the most popular cloud-based form automation solutions, JotForm enables users to design and manage web forms as well as track user feedback. It…

    Customization, Developer Org, Digital Experience, Digital Experience Bundle, Digital Websites
    Apphienz Mar 14, 2023
    1,695  Views

    Popular Salesforce Videos

    Easy Salesforce Lightning Experience Tricks for Admins

    Easy Salesforce Lightning Experience Tricks for Admins

    Video in Lightning

    Learn how to enhance the screens for your end users by leveraging Related List - Quick Links, Report Charts and dynamic filters to show the…

    Salesforce Online Training, Salesforce Lightning, Salesforce Lightning Experience, Salesforce Administrator, Lightning App Builder
    Prafull Feb 21, 2018
    1,798  Views
    DreamTX Welcome Day 2 | Dreamforce 2020 | Salesforce

    DreamTX Welcome Day 2 | Dreamforce 2020 | Salesforce

    Video in Salesforce Stories, Events

    If you missed DreamTX DAY 2, then live this amazing experience here, we have covered it all for you. See the power of Customer 360,…

    dreamforce, salesforce, Salesforce Event, Salesforce Video, Customer 360
    Pooja Dec 16, 2020
    2,268  Views
    Create a Multi-page App Using LWC OSS | Salesforce Lightning

    Create a Multi-page App Using LWC OSS | Salesforce Lightning

    Video in Lightning, Salesforce Apps

    The default LWC OSS project created using the create-lwc-app tool lets you create a single-page application. But did you know that by slightly modifying the…

    Salesforce Training, Salesforce Tutorial, Salesforce Lightning, Salesforce Apps, Salesforce Video
    Jogender Jun 16, 2021
    3,275  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.