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

    Salesforce Einstein Analytics: The Game-Changer for Your Business Intelligence Strategy

    Blog in Others

    As businesses continue to collect massive amounts of data, the need for effective business intelligence (BI) strategies has become more critical than ever. BI tools…

    Artificial Intelligence, Better Understanding of Customer Behavior, BI Strategies, Business Intelligence, CCPA
    Infodriven May 31, 2023
    1,203  Views
    sObject

    Access sObject's (Salesforce Object Type) Fields and Its Record | Apex Developer Guide

    Blog in Others

    There are multiple cases where you need to show sObjects records and its field in Lightning. For example: Consider a use case scenario where you…

    Controller, Helper, Lightning, Lightning Component, Mmultiple Cases
    krati vishnoi Jul 20, 2020
    10,344  Views
    Salesforce User License

    Knowledge of Salesforce User Licenses and its Types

    Blog in Salesforce Admin

    The following license types are available for Salesforce users Standard User Licenses Chatter User Licenses Experience Cloud User Licenses Service Cloud Portal User Licenses Sites…

    App Subscription, AppExchange Apps, Article Management, Article Management Tab, Articles
    SF Jun 9, 2022
    9,328  Views

    Popular Salesforce Videos

    Difference between Profile and Roles in Salesforce

    Difference between Profile and Roles in Salesforce

    Video in Salesforce Admin

    Learn about the difference between Profile and Roles? Watch this video and do let us know if you have any questions or concerns.

    Salesforce Training, Salesforce Tutorial, Salesforce Online Training, Salesforce Certification, Profiles In Salesforce
    CRS Mar 10, 2022
    2,181  Views
    COVID 19 Tracker in Salesforce Lightning Aura Component

    COVID 19 Tracker in Salesforce Lightning Aura Component

    Video in Lightning, Salesforce Training

    In this video, we are developing a lightning Component to track the current pandemic COVID19 Stats Tracker which shows the current situation of all countries…

    Salesforce Training, Salesforce Tutorial, Salesforce Lightning, Salesforce Video, Salesforce Learning
    Shubham Feb 18, 2021
    2,009  Views
    Dreamforce 2020 FAQs | Dreamforce To You | Salesforce Event

    Dreamforce 2020 FAQs | Dreamforce To You | Salesforce Event

    Video in Salesforce Stories, Events

    As the COVID-19 situation continues to evolve, questions around virtual Dreamforce 2020 continue to increase and that's why we've gone ahead and answered all the…

    dreamforce, salesforce, Salesforce Event, Salesforce Video, Salesforce Learning
    Algoworks Nov 19, 2020
    1,281  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.