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 › 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 9 years 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.

    • [adinserter block='9']
    • 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

    Popular Salesforce Blogs

    Salesforce vs ERP: Understanding the Key Differences

    Blog in Others

    It's very common to encounter misconceptions about Salesforce being just another ERP system. However, understanding the distinct functionalities and capabilities of Salesforce compared to traditional…

    api-integration, Business Efficiency, Business Management, Cloud Based CRM, Comprehensive Insights
    Emorphis Jun 25, 2024
    1,152  Views

    How Integrated CRM Payment Solution can Help Nonprofits | Salesforce Solutions

    Blog in Salesforce integration

    If your fundraising team is spending hours monthly to manually import and export data from the increasing payment providers, then they are definitely doing it…

    Automatic Payment, cryptocurrency, Data Management, Data Transfer, Digital Donor Experience
    360 Degree May 11, 2022
    2,162  Views
    Ensuring Seamless Customer Experience with Option Constraints | Salesforce CPQ

    Ensuring a Seamless Customer Experience with Option Constraints | Salesforce CPQ

    Blog in Salesforce

    Salesforce CPQ provides an advanced and effective solution for companies looking to streamline their sales process and improve the customer experience. One of its key…

    CPQ, CPQ Analysis, CPQ Benefits, CPQ Consultation, CPQ CRM Software Integration
    Guilherme Feb 9, 2023
    2,074  Views

    Popular Salesforce Videos

    Develop a Basic Slack App For Salesforce

    Develop a Basic Slack App For Salesforce

    Video in Salesforce Stories

    Build Slack Apps integrated with Salesforce. Use Heroku to host Slack apps built using Slack Bolt Framework and get connected to Salesforce using Node.js library…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Video, Salesforce Learning
    Nauman Dec 7, 2021
    2,926  Views
    What is Salesforce APEX? How to use APEX? Salesforce Interview Questions and Answers

    What is Salesforce APEX? How to use APEX? Salesforce Interview Questions and Answers

    Video in Salesforce Apex

    Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls…

    Salesforce Training, Salesforce Tutorial, Salesforce Development, Salesforce Apex, Salesforce Online Training
    CRS Dec 20, 2021
    2,143  Views
    How to Learn Salesforce Development | Salesforce learning steps for beginners | Trailhead demo

    How to Learn Salesforce Development | Salesforce learning steps for beginners | Trailhead demo

    Video in Salesforce Training, Trailhead

    In this video, we shared the steps to learn salesforce lightning basics, salesforce apex development, CRM in online for free from scratch very quickly. Learn salesforce…

    Salesforce Training, Salesforce Tutorial, salesforce training videos for beginners, How To Learn Salesforce Development, Salesforce Learning Steps For Beginners
    Shiv Kumar Sharma Jan 18, 2018
    3,082  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.