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, 4 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, 4 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

    Navigating Salesforce Test Automation: A Comprehensive Guide for Engineers

    Blog in Salesforce, Salesforce Apex

    Organizations increasingly rely on advanced tools to keep pace with frequent releases in the rapidly evolving software development world. This blog provides engineers, testers, product…

    Apex Testing Framework, Automated Salesforce Testing, Cross-browser Compatibility, Custom Developments, Data Consistency
    Abhay Jan 24, 2024
    1,679  Views
    Batch Apex

    Using Batch Apex | Apex Developer Guide | Salesforce

    Blog in Salesforce Apex

    Cluster Apex is utilized to run huge positions (think thousands or millions of records!) that would surpass ordinary preparing limits. Utilizing Batch Apex, you can…

    Apex Work Line, API Call, Batch Apex, Batch Apex Work, Batchable Interface
    Sumit kumar Dec 16, 2020
    4,288  Views

    Solving Challenges that Come with the Traditional Salesforce Chatbot Model

    Blog in Salesforce

    Digitization is the name of the game and customers are getting used to this. Chatbots are part of almost every organization’s digital journey.  They offer…

    Chatbot Development Challenges, Chatbot Development Time, Chatbot Maintenance Costs, Chatbot Pricing, Cold AI Chatbot
    360 SMS APP Oct 16, 2023
    1,224  Views

    Popular Salesforce Videos

    How To Use Data Loader In Salesforce - Learn Salesforce Series By Algoworks

    How To Use Data Loader In Salesforce - Learn Salesforce Series By Algoworks

    Video in Salesforce Products, Salesforce Training

    Learn how to use Data Loader in Salesforce. In this video, the following points will be covered - 1. Introduction Here you will get a…

    salesforce, Testing, support, Data Loader, Salesforce Series
    Algoworks Aug 31, 2020
    1,502  Views
    Salesforce Customer 360 - It Brings Your Company & Customers Together

    Salesforce Customer 360 - It Brings Your Company & Customers Together

    Video in Salesforce Products

    Q: With a dispersed workforce, how do you bring all departments together with a single shared view of your customer? A: With Salesforce Customer 360.…

    Salesforce Training, salesforce, CRM, Salesforce Video, Customer 360
    Pooja Jul 29, 2020
    2,450  Views
    Get Started with Salesforce for Recruitment and Admissions: Admissions and Onbaording

    Get Started with Salesforce for Recruitment and Admissions: Admissions and Onbaording

    Video in Salesforce Stories

    This webinar is designed for Salesforce admins and users who work in Recruitment and Admissions. We will cover all you need to know about using…

    salesforce, Salesforce Admins, Salesforce Video, Salesforce Learning, Saleforce Training
    Prafull Aug 17, 2020
    1,821  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.