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 and Gmail Integration

    Salesforce and Gmail Integration | The Perfect Integration Guide

    Blog in Salesforce integration

    Salesforce Gmail Integration Gmail is a free Web-based Mail service that provides users with gigabytes of storage for emails and information and provides the ability…

    Chrome Browser, Chrome Web Store, Customer Relationship Management, Customers and Partners, Email Platforms
    Nikhil Kumar Sep 2, 2022
    2,797  Views
    insert record in salesforce

    Insert Record by Email Service Attachments in Salesforce

    Blog in Others, Salesforce, Salesforce Apex

    Email Service Email service is the process that fulfills the business requirement by automated process service by using apex classes to process the content, attachment,…

    Attached CSV Files, Attachment, Attachments in Salesforce, Automated Process Service, Business Requirement
    Aditya Aug 7, 2020
    11,492  Views

    Everything You Need To Know About Salesforce Admin Certification Exam

    Blog in Salesforce, Salesforce Certifications

    Salesforce is a game-changer, ever-evolving, always innovating, #1 CRM platform globally. It is a huge company growing leap and bounds every year. It welcomes individuals…

    App Developer, AppExchange, Approval Process, Automation Solution, Business Process
    Apphienz Apr 12, 2022
    3,020  Views

    Popular Salesforce Videos

    Salesforce Customer 360 Audiences Enhancements

    Salesforce Customer 360 Audiences Enhancements

    Video in Salesforce Stories, Salesforce Products

    Salesforce Customer 360 is a tool that allows companies to connect Salesforce apps and create a unified customer ID to build a single view of…

    Salesforce Demo, Salesforce Apps, Salesforce Video, Salesforce Customer 360, Salesforce Learning
    Jogender Dec 9, 2020
    1,867  Views
    Schedule Batch Apex | Asynchronous Apex in Salesforce | Learn Salesforce Development

    Schedule Batch Apex | Asynchronous Apex in Salesforce | Learn Salesforce Development

    Video in Salesforce Apex

    Watch this video tutorial to learn how to schedule batch apex in Salesforce. If you have any doubts or concerns do let us know in…

    Salesforce Training, Salesforce Tutorial, Salesforce Apex, Learn Salesforce Development, Salesforce Video
    Prafull Mar 9, 2022
    2,195  Views
    Why Are Some of The World’s Biggest Brands Transitioning Towards Headless? | Salesforce

    Why Are Some of The World’s Biggest Brands Transitioning Towards Headless? | Salesforce

    Video in

    With the advancement of headless architecture, consumers can now enjoy an omnichannel shopping experience. But that's not all that headless commerce has to offer. Leading…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce Webinar, Salesforce Learning
    Cyntexa Aug 31, 2022
    1,310  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.