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, 1 month ago by  pawan makkar.
      • This discussion was modified 9 years, 1 month ago by  Forcetalks.
      • This discussion was modified 9 years, 1 month ago by  Forcetalks.
      shariq replied 9 years, 1 month 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

    Switching to Salesforce Lightning and Overcoming Challenges

    Blog in Lightning, Salesforce

    Time is of the essence in a saturated business landscape, and getting ahead of the competition gives your company enough edge to thrive despite the…

    Business Landscape, Business Needs, Business Process, Business Processes, Business Sales Initiatives
    Apphienz Jun 23, 2021
    2,339  Views

    Working with Files in Salesforce

    Blog in Others

    Most data in Salesforce is stored as records in the Salesforce database for your organization (technically, Salesforce uses a multi-tenant architecture that uses a combined…

    Data Access, File Sharing, Files, Salesforce Cloud, Salesforce Data
    PowerUser Apr 17, 2019
    28,337  Views
    Google Tag Manager

    Implementation of Google Tag Manager For Salesforce Communities

    Blog in Salesforce integration, Salesforce Training

    Introduction Without depending upon the developer, your marketing team can deploy marketing tags and tracking pixels. Google Tag Manager gives you permission to track video…

    Browsing Session, Community Builder, Configure Triggers, Content Security Policies, Events
    Deepak Jan 4, 2021
    35,707  Views

    Popular Salesforce Videos

    Use Salesforce CMS to Manage and Publish Content for Experience Cloud Sites

    Use Salesforce CMS to Manage and Publish Content for Experience Cloud Sites

    Video in Salesforce Cloud Platform

    Learn how Salesforce CMS helps you create, manage, and publish your content in Experience Cloud. With Salesforce CMS, you can also customize content for specific…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Video, Salesforce Learning
    Jogender Apr 25, 2022
    2,677  Views
    Join Grazitti Interactive At Dreamforce 2022 | Salesforce

    Join Grazitti Interactive At Dreamforce 2022 | Salesforce

    Video in Salesforce Stories

    Dreamforce, Salesforce’s flagship conference is here once again. Grazitti Interactive, as a proud Navigator Sponsor invites you to meet our experts and discuss our custom…

    Salesforce Training, dreamforce, salesforce, Salesforce Video, Salesforce Learning
    Sushmita Mehta Aug 23, 2022
    1,762  Views
    Streamline Sales Goals and Add Value to Your B2B Organization With Salesforce Marketing Automation

    Streamline Sales Goals and Add Value to Your B2B Organization With Salesforce Marketing Automation

    Video in Salesforce Products

    Salesforce Marketing Automation enables businesses to automate marketing processes, track customer engagement while delivering personalized experiences to each customer. With it, you can streamline and…

    Salesforce Training, Salesforce Developer, Customer Engagement, Salesforce Video, Salesforce Learning
    Kcloud Dec 16, 2020
    2,304  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.