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 › What is the SOQL query to get all accounts related to a contact?

    Tagged: Contacts Data, Multiple Account Relationship, Rollup, Salesforce Account, SOQL Query

    • Salesforce® Discussions

      What is the SOQL query to get all accounts related to a contact?

      Posted by Delano on August 17, 2018 at 5:36 PM

      I need to roll-up to contacts data from related accounts. As a first step, how would the query look like to get for example the number of accounts related to a contact, or better, total AnnualRevenue of all related accounts?

      David Levi replied 2 years, 7 months ago 3 Members · 3 Replies
      • Contacts Data
      • Multiple Account Relationship
      • Rollup
      • Salesforce Account
      • SOQL Query
    • 3 Replies
    • Avnish Yadav

      Member
      August 20, 2018 at 6:52 AM

      Hi Delango.

      Here is sample code:-

      List<Account> acc=[Select Id,Name from Account];
      for(Account a:acc){
      List<Contact> con=[Select accountId, LastName from contact where AccountId=:a.id];

      Integer TotalAnnualRevenue = 0;
      for(Contact c:con){
      TotalAnnualRevenue = TotalAnnualRevenue+c.AnnualRevenue
      System.debug('Total revenue'+ TotalAnnualRevenue);
      }

      }

      Hope this will help you.

      Thanks.

    • Delano

      Member
      August 22, 2018 at 10:09 PM

      I should have clarified that I have enable the Contact to multiple Accounts relationship and what I'm trying to solve for is to roll-up data to the contact from all related accounts.

    • David Levi

      Member
      June 26, 2023 at 1:39 PM

      You do a SOQL query in LOOP - Its the best practice to fail on Governor Limits

    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

    A Comprehensive Comparison of Salesforce Standard Quote and Salesforce CPQ for Manufacturing Companies

    Blog in Salesforce

    As a seasoned Salesforce consultant, I have spent countless hours assisting all kinds of companies, including Tech, Sanitary, Service and... Manufacturing in optimizing their quoting…

    Automatic Discounts, Block Price, Built-in Module, contract-management, Customer Experience
    Guilherme May 5, 2023
    1,625  Views

    Integrating Single Salesforce Org With Multiple Jira Instances with Sinergify

    Blog in Salesforce integration

    Why the need for different Jira instances within the same organization? As we all know, Salesforce is a CRM platform and organizations use it for…

    Atlassian Jira, Communication and Collaboration, CRM Platform, Customer Data-related Activities, Integrate Multiple Jira Instances
    Shubham Mar 22, 2021
    3,128  Views

    Salesforce Marketing Cloud and its Benefits - Learn Here

    Blog in Salesforce Cloud Platform

    Marketing Cloud is an automation platform praised around the globe and is the most feature-rich in its class. It provides incredible insight into customer journeys…

    Automation Platform, Cloud Computing, Connected Customer Experience, Contact Data, Current Data
    Innovadel Aug 19, 2022
    3,914  Views

    Popular Salesforce Videos

    How to Execute a SOQL Query in POSTMAN?

    How to Execute a SOQL Query in POSTMAN?

    Video in Salesforce Training

    Watch this video to learn how to execute a SOQL Query in POSTMAN. If you have any doubts or questions, let us know in the…

    Salesforce Training, Salesforce Tutorial, Salesforce Video, Salesforce Learning, Postman
    Rupal Kakkar Jun 28, 2021
    2,999  Views
    Similarity and Difference Between SFRA and PWA | Which One to Choose? | Salesforce

    Similarity and Difference Between SFRA and PWA | Which One to Choose? | Salesforce

    Video in Salesforce Stories

    Choosing SFCC as the platform necessitates a major decision: on what architecture will your channels be based? When learning about Salesforce B2C Commerce Cloud, you'll…

    Salesforce Training, salesforce, Salesforce Learning, Webinar, Commerce Cloud
    Cyntexa Aug 29, 2022
    1,581  Views
    Exploring Salesforce composite Resources | Composite API

    Exploring Salesforce composite Resources | Composite API

    Video in Salesforce Training

    Exploring Salesforce composite Resources-- 0:00 Introduction 1:30 What is Composite Resources? 2:00 Composite Resources consists of 2:25 Composite request body 5:30 Composite request response 6:33…

    Salesforce Training, Salesforce Tutorial, Salesforce Video, Salesforce Learning, sObject
    Prafull Sep 2, 2021
    1,715  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.