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 › SOQL Query ( Accounts and Contacts ) in Salesforce

    Tagged: Account, Contact Record, Developer Console, Salesforce Apex, Salesforce SOQL, SOQL, SOQL Query

    • Salesforce® Discussions

      SOQL Query ( Accounts and Contacts ) in Salesforce

      Posted by Rahul Kr on August 10, 2017 at 4:12 AM

      Dear Sir / Mam,
      I have one question :-

      Write an SOQL Query in Developer Console where I want to Display Accounts and their Corresponding Contacts ( First Name, Last Name etc ) BUT only those Accounts who have more than ONE Contact.
      Thanks
      Regards

      Rahul

      # 9958495609

      Aman replied 8 years, 3 months ago 3 Members · 2 Replies
      • Account
      • Contact Record
      • Developer Console
      • Salesforce Apex
      • Salesforce SOQL
      • SOQL
      • SOQL Query
    • 2 Replies
    • Shaharyar

      Member
      August 10, 2017 at 5:12 AM

      Hello Rahul,

      #Apex Class

      public class AccountAndContacts {

      public static void displayContacts(){
      List<Account> acc=[Select Id,Name from Account];
      for(Account a:acc){
      List<Contact> con=[Select LastName from contact where AccountId=:a.id];
      for(Contact c:con){
      if(con.size()>1){
      System.debug('AccountName '+''+a.Name+'RelatedContacts '+''+c.LastName);
      }}}}}

       

       

      #Anonymous Window

      AccountAndContacts.displayContacts();

       

      #Check the output in debugger log...

      Hope it will help you.

      Note:I have written it in simple understandable ,way you can optimize code by using nested query.

    • Aman

      Member
      August 28, 2017 at 10:54 AM

      Hello Rahul ,

      you can try this :

      public class SearchCont {
      public static List<Account> acct;
      public static List<AggregateResult> aggRes;
      public static void contSearch(){
      List<String> ide=new List<String>();
      aggRes =[select accountId,count(id) from contact where account.name !=null group by accountId having count(id) >1];
      for (AggregateResult ar : aggRes) {
      ide.add((String)ar.get('accountId'));
      }
      acct=[select name,(select LastName,FirstName from contacts) from account where id in :ide ];
      for(account acc: acct){
      system.debug('###'+acc);
      for(contact con: acc.contacts){
      system.debug('###'+con.LastName);
      system.debug('###'+con.FirstName);

      }
      }
      }
      }

    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

    Data-Protection-Law-In-Europe

    Salesforce : Data Protection Law In Europe

    Blog in Others

    European Union Privacy Law Basics Europe was facing issues of privacy and data protection since decades. Now, the European Union (EU) has introduced a comprehensive…

    Business Process, Data Access, GDPR, Salesforce Data Security, Salesforce Security
    Archit Nov 6, 2018
    2,623  Views
    ClickJack Protection for Salesforce

    ClickJack Protection for Salesforce

    Blog in Others

    ClickJacking name sounds like an unfamiliar jargon for those who have never heard of it. Let me clarify it for you, it is a technique…

    ClickJack Protection, ClickJacked Page, ClickJacking, ClickJacking in Salesforce, Framebusting
    Manpreet Jan 24, 2018
    24,625  Views
    salesforce app dev

    The Future of Salesforce App Development

    Blog in Others

    The use of mobile phones has made a lot of transformation with the way businesses are getting their products and services available to people. There…

    App Developers, App Development Platform, Backend Service, Business Information, Business Performance
    Mobilunity Jan 18, 2021
    3,351  Views

    Popular Salesforce Videos

    Salesforce Einstein Keynote: AI for CRM

    Salesforce Einstein Keynote: AI for CRM

    Video in Salesforce Einstein

    Salesforce Einstein is delivering more than one billion predictions every day, helping companies get a new level of insight about their business and their customers.…

    Salesforce Apps, Artificial Intelligence, Sales Cloud Einstein, Salesforce Einstein, Salesforce CRM
    Prafull Apr 19, 2018
    1,795  Views
    Introduction to Salesforce Einstein OCR

    Introduction to Salesforce Einstein OCR

    Video in Salesforce Einstein

    In this session, we will learn about Einstein OCR and Create/Update Leads and Contacts using Business Cards. Let’s welcome an amazing #Summer20 release gem –…

    Salesforce Training, salesforce, Salesforce Einstein, Salesforce Video, REST API
    Jogender Aug 13, 2020
    2,467  Views
    Salesforce TrailheaDX '18 Opening Keynote - Part 4: Build Apps Fast

    Salesforce TrailheaDX '18 Opening Keynote - Part 4: Build Apps Fast

    Video in Trailhead

    Salesforce TrailheaDX ’18 Opening Keynote – Part 1: Create Connected Customer Experiences TrailheaDX ’18 Opening Keynote – Part 2: Salesforce Platform TrailheaDX ’18 Opening Keynote…

    Salesforce Records, Salesforce Lightning, Salesforce Einstein, Salesforce Trailhead, Lightning App Builder
    Abhay Apr 3, 2018
    1,642  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

    © 2025 - 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.