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 8 years, 1 month 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 8 years, 1 month 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

    3 Key Marketing Challenges and how you can overcome them with Salesforce Marketing Cloud Benefits

    Blog in Salesforce Products

    Salesforce Marketing Cloud Benefits – In the digital age, customers are invariably inundated with marketing messages everywhere. Digital Marketing experts estimate that most Americans are exposed…

    Customer Experience, Marketing Cloud, Marketing Cloud Benefits, Marketing Cloud Features, Salesforce Cloud Platform
    LevelShift (formerly DemandBlue) Jun 20, 2019
    5,114  Views
    Integrate QuickBooks

    How to Integrate QuickBooks and Salesforce?

    Blog in Salesforce integration

    Introduction QuickBooks, a leading accounting software platform developed and marketed by Intuit, a financial software development company helping businesses develop and sell financial, accounting, and…

    Apex Setting, App, AppExchnage, Applications in Appexchange, automated process
    Deepika Feb 17, 2021
    4,455  Views

    Salesforce Marketing Cloud: A Beacon in the 2025 Digital Marketing Landscape

    Blog in Others

    Salesforce Marketing Cloud has emerged as a dominant force in the digital marketing landscape. Its comprehensive suite of tools empowers businesses to deliver personalized, omnichannel…

    Cloudy Aug 1, 2025
    408  Views

    Popular Salesforce Videos

    What is Salesforce Development? | Salesforce Developer Course

    What is Salesforce Development? | Salesforce Developer Course

    Video in Salesforce Development

    In this video, Shrey has given a detailed overview of the Salesforce Development Course. You will learn the following topics from this course: Apex basics…

    Salesforce Training, Salesforce Development, Visualforce, SOQL, Apex Basics
    Prafull Jun 15, 2021
    1,575  Views
    Create a New Marker Layer in Maps | Salesforce Maps

    Create a New Marker Layer in Maps | Salesforce Maps

    Video in Salesforce Training

    Salesforce Maps is a location intelligence and map visualization tool that integrates with your Salesforce CRM, simplifying territory management. With Salesforce Maps you can create territories…

    Salesforce Training, Salesforce Tutorial, salesforce, Salesforce CRM, Salesforce Video
    Prafull May 10, 2022
    2,221  Views
    Salesforce Data Management - Data Loader - Insert, Update, Upsert, Export, Delete

    Salesforce Data Management - Data Loader - Insert, Update, Upsert, Export, Delete

    Video in Data

    Use the Data Loader wizards to add, modify, or delete records. The upsert wizard combines inserting and updating a record. If a record in your…

    Salesforce Training, Salesforce Tutorial, Salesforce Data Management, Salesforce Video, Salesforce Learning
    Prafull Aug 24, 2021
    3,496  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.