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

    Salesforce Company USA

    Choose An Experienced Company For Salesforce Development In The USA

    Blog in Salesforce Consultant

    When you wish to achieve unparalleled customer service, it is essential to choose an experienced and professional Salesforce Development company. The reason to select an…

    Saleforce Consulting Services in USA, Saleforce CRM, Saleforce Development Company, Salesforce Developer, Salesforce Development Services in USA
    Advanz101 Marketing Nov 6, 2018
    3,126  Views

    Salesforce Marketing Cloud Personalization Overview

    Blog in Salesforce Cloud Platform

    Suppose you've attracted leads to your website or app. Some of them converted into prospects. You carry on gathering data, but they keep unsubscribing or…

    Audience Segments, Behavioral Data, Cloud Personalization, Cross-Selling Tools, Customer Data Platform
    Twistellar Jun 17, 2024
    836  Views

    No Code ScheduleOnce and Salesforce Integration

    Blog in Salesforce integration

    ScheduleOnce is a cloud-based scheduling service that enables businesses of all sizes to schedule and manage user engagement with their users and prospects. Users can…

    API requests, API User, Booking Data, Cloud-based Scheduling Service, CRM
    Apphienz Apr 26, 2022
    2,295  Views

    Popular Salesforce Videos

    Salesforce Tutorial | Data Extensions in Marketing Cloud

    Salesforce Tutorial | Data Extensions in Marketing Cloud

    Video in Data, Salesforce Training

    In this video, we are going to show you something so simple, yet so powerful, you may not even know it existed. For more Salesforce…

    Salesforce Training, Salesforce Tutorial, Salesforce VIdeos, Salesforce Learning, Marketing Cloud
    Stimulus Consulting Jan 6, 2021
    3,063  Views
    Methods in Salesforce - All You Need to Know

    Methods in Salesforce - All You Need to Know

    Video in Salesforce Apex

    Learn All About Methods in Salesforce. Watch this video and do let us know if you have any doubts or concerns. What is a Method…

    Salesforce Training, Salesforce Tutorial, Salesforce Apex, salesforce, Apex
    SFDC Sep 16, 2022
    3,273  Views
    Get Started with Salesforce for Recruitment and Admissions: Recruitment and Marketing

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

    Video in Others, Marketing

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

    salesforce, Salesforce Tools, Salesforce Admins, Salesforce Webinar, Salesforce Video
    Nauman Jul 28, 2020
    1,438  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.