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 › 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, 11 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]);

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

    Popular Salesforce Blogs

    FlexDeploy Loves Salesforce: Build and Deploy Salesforce DX Apps

    Blog in Salesforce integration

    FlexDeploy is an Enterprise DevOps platform with fully integrated support for Salesforce apps, metadata customizations, integrations and more. This is the fourth article in a blog…

    Best Practices, Business Org, CI/CD Automation, Created Packages, Customer Issues
    Flexagon Feb 9, 2022
    2,434  Views

    Pardot Audit: How and Why It is Essential to Conduct One Today | Salesforce

    Blog in Salesforce Products

    Pardot is a powerful B2B marketing tool that allows marketers to run and manage multiple campaigns with ease. It is capable of automating processes and driving…

    Automating Processes, B2B, B2B Marketing Tool, Best Practices, Bulk Data
    LevelShift (formerly DemandBlue) Dec 17, 2020
    3,639  Views
    controller extension

    Controller Extensions in Salesforce | The Developer Guide

    Blog in Others

    Controller Extension is an Apex Code that extends the functionality of a Standard or Custom Controller. When to Use Controller Extensions When we want to…

    Apex Pages, Controller Extension, Controller Extensions, Custom Controller, Developer Guide
    Kirandeep Aug 20, 2020
    3,956  Views

    Popular Salesforce Videos

    Reason to Switch the Job | Future of Salesforce Job Market | Christopher A Hopper

    Reason to Switch the Job | Future of Salesforce Job Market | Christopher A Hopper

    Video in Salesforce Stories

    Here is the Glimpse of Conversation with Christopher A Hopper on the topic "Reason to Switch the Job". This is the very first episode of…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Industry, Salesforce Video
    S2 Jul 1, 2022
    1,762  Views
    Salesforce Marketing Cloud: Journey Builder Demo

    Salesforce Marketing Cloud: Journey Builder Demo

    Video in Marketing, Salesforce Cloud Platform

    With Journey Builder from Marketing Cloud, send your customers personalized, connected experiences to build lasting relationships with your brand. Watch this video and learn.

    Salesforce Training, Salesforce Tutorial, Salesforce Marketing Cloud, Salesforce Video, Customers
    Jogender Dec 11, 2020
    3,211  Views
    How to Create Seamless Buying Experiences | Salesforce CPQ & B2B Commerce

    How to Create Seamless Buying Experiences | Salesforce CPQ & B2B Commerce

    Video in Salesforce Stories, Salesforce Training

    B2B buying is evolving as buyers want a better, more personalized experience when shopping for their businesses. But Salesforce has that covered. Here are 4…

    salesforce, Salesforce CPQ, Salesforce Video, Salesforce Learning, Business
    Jogender Jul 14, 2020
    1,680  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.