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 › 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 8 years, 7 months ago by  pawan makkar.
      • This discussion was modified 8 years, 7 months ago by  Forcetalks.
      • This discussion was modified 8 years, 7 months ago by  Forcetalks.
      shariq replied 8 years, 7 months 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

    application solution

    Popular Salesforce Blogs

    Smart Dynamic Forms for Salesforce

    Smart Dynamic Forms for Salesforce

    Blog in Salesforce integration

    Do it yourself - Dynamic forms, conditional logic and realtime field population for salesforce without writing a single line of code. When working with online…

    Salesforce Forms
    Ori Sep 19, 2017
    9,251  Views
    Apex Testing in Salesforce

    All About Apex Testing in Salesforce

    Blog in Salesforce Apex

    Apex Unit Tests  Apex unit tests ensure superior grades for your Apex code and let you meet the essentials for deploying Apex.  Testing is the…

    Apex Testing, Apex Unit Tests, App Developers, Application, Bundle Supporters
    Shweta Choudhary Oct 12, 2021
    4,213  Views

    Introducing the New Salesforce Nonprofit Cloud

    Blog in Salesforce Products

    In collaboration with community users and partners, Salesforce is introducing a new Nonprofit Cloud! Currently, nonprofit organizations install a variety of nonprofit-related applications on top…

    Attendance, Benefit, Budget Information, Care Plans, Case Management
    DB Jun 26, 2023
    1,187  Views

    Popular Salesforce Videos

    How to Configure SAML Single Sign-On with Salesforce as the Identity Provider

    How to Configure SAML Single Sign-On with Salesforce as the Identity Provider

    Video in Salesforce Training

    Configure single sign-on with a Salesforce org as the identity provider for an external Heroku app acting as the service provider, using SAML. Watch this…

    salesforce, Salesforce Org, Salesforce Video, Salesforce leaarning, Configure SAML
    Prafull Aug 11, 2020
    1,412  Views
    Pass Salesforce Certification Exam In Your First Attempt

    Pass Salesforce Certification Exam In Your First Attempt

    Video in Salesforce Stories

    This Salesforce Training Video will help you understand the concepts of Salesforce and Salesforce exam so that you can pass the Salesforce exam in the…

    Salesforce Training, Salesforce Certification, Salesforce Developer, Salesforce Community, Salesforce Tools
    Simplilearn Apr 23, 2019
    1,720  Views
    How to install Salesforce for Outlook in 2019

    How to install Salesforce for Outlook in 2019

    Video in Salesforce Integration

    Watch this video to learn how to install Salesforce for Outlook. ? Also, we cover all the details in our blog post at https://ascendix.com/blog/integrating-salesforce-outlook/

    Salesforce Products, Easy Way To Learn Salesforce, Best Salesforce Tutorials, Salesforce for Outlook, Outlook integration
    Valeria Feb 28, 2019
    1,854  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.