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 › How to return Map result from SOQL query in Salesforce Apex?

    Tagged: Account ID, Apex Map, Apex SelectList, Opportunity ID, Salesforce Accounts, Salesforce Apex, Salesforce Apex Code, SOQL Query

    • Salesforce® Discussions

      How to return Map result from SOQL query in Salesforce Apex?

      Posted by Aman on September 22, 2018 at 5:03 PM

      How to return Map result from SOQL query in Salesforce Apex?

      Parul replied 7 years, 2 months ago 3 Members · 2 Replies
      • Account ID
      • Apex Map
      • Apex SelectList
      • Opportunity ID
      • Salesforce Accounts
      • Salesforce Apex
      • Salesforce Apex Code
      • SOQL Query
    • 2 Replies
    • shariq

      Member
      September 22, 2018 at 5:03 PM

      Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);

    • Parul

      Member
      September 22, 2018 at 5:44 PM

      You can perform this operation by going through this code given below:

      //Creating List of all account Ids
      List<id> accIdsList = new List<id>() ;

      //Creating set of all account Ids
      Set<id> accIdsSet = new Set<id>() ;

      //Fetching all accounts
      List<account> accList = new List<Account>();

      //Creating Map with account id as key and account record as value
      Map<Id,Account> accountIdObjMap = new Map<Id,Account>([select Id,name,site,rating,AccountNumber from account limit 50000]);

      //getting list of account using map.values method
      accList = accountIdObjMap.values();

      //getting set of account Id’s using map.keySet method
      accIdsSet = accountIdObjMap.keySet();

      //getting list of account Id’s using list.addAll method
      accIdsList.addAll(accIdsSet);

      To clarify, this method can only be used to generate Maps using Id of the object you are querying as the key. If you want to use a different value as the key, you will have to iterate over the list returned by your query and put values into a Map. For instance, if you wanted to use AccountId as the key, you would need to do something like this: List<Opportunity> oppList = [Select Id, AccountId from Opportunity]; Map<Id,Opportunity> accOppMap = new Map<Id,Opportunity>(); for(Opportunity o : oppList){ accOppMap.put(o.AccountId,o); }

      Hope this may help

      • This reply was modified 7 years, 2 months ago by  Parul.

    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-service-cloud-customization-update

    8 Salesforce Service Cloud Enhancements to Deliver Exceptional Customer Experience

    Blog in Salesforce Products

    A customized Salesforce Service Cloud can go a long way in helping you deliver exceptional customer experiences. But what is it is that you can…

    Cloud Computing, Product Management Tools, Salesforce Customization, Salesforce Integration, Salesforce Service Cloud
    Ankur Gupta Oct 30, 2018
    6,959  Views

    Salesforce Advisory Services: Finally Explained in Detail

    Blog in Salesforce Consultant

    If Salesforce is out-of-the-box and ready to use, why does it even come to custom development? Implementing Salesforce CRM sounds like a perfect solution to…

    Assessment, Best Practices, Business Requirements, Business Strategy, Challenges
    Sparkybit Sep 21, 2023
    1,612  Views

    How to Choose the Right Salesforce Consulting Partner?

    Blog in Salesforce

    What is the secret to a successful Salesforce CRM implementation for an organization? Clear objectives, customization and configuration, seamless integration of third-party systems as well…

    budget, Cloud Analogy, Consulting Partner, CRM Implementation, Industry-specific Expertise
    Hennery cavil Nov 8, 2023
    1,439  Views

    Popular Salesforce Videos

    Journey To A Salesforce Solution Architect

    Journey To A Salesforce Solution Architect

    Video in Salesforce Training

    A Salesforce Solution Architect must master a number of skills to be able to solve complex business requirements. In this session, we will cover 10…

    Salesforce Training, Salesforce Tutorial, Salesforce Video, Salesforce Learning, Business Requirements
    Rupal Kakkar Jul 8, 2021
    1,604  Views
    Salesforce Full Course - Learn Salesforce in 9 Hours | Salesforce Training Video

    Salesforce Full Course - Learn Salesforce in 9 Hours | Salesforce Training Video

    Video in Salesforce Stories, Salesforce Training

    Salesforce Full Course video will help you understand and learn Salesforce Cloud services in detail. This Salesforce Tutorial is ideal for both beginners as well…

    Salesforce Training, Salesforce Tutorial, Salesforce Certification, salesforce, Salesforce Apps
    Jogender Jun 16, 2020
    3,625  Views
    Mission Mangal | Build a Mars Communication Uplink | Salesforce Tutorials | Mini Project

    Mission Mangal | Build a Mars Communication Uplink | Salesforce Tutorials | Mini Project

    Video in Salesforce Development

    In this video, you will learn to create a Mars Communication Uplink using Salesforce. We will create a communication system from Rovers on Mars. Interesting…

    Salesforce Training, Salesforce Developer, Salesforce Tutorials, Salesforce Video, Salesforce Learning
    Kapil Jun 22, 2020
    2,359  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.