• Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce Consultants
      • Marketing Cloud Consultants
      • Service Cloud Consultants
      • Experience Cloud Consultants
      • Analytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud Consultants
      • Financial Service Cloud Consultants
      • Health Cloud Consultants
      • Commerce Cloud Consultants
      • Manufacturing Cloud Consultants
  • Jobs
  • Marketplace
  • Stories
  • Top Bloggers 2022
  • Advertise With Us
  • Quizzes
  • Discussions
  • Groups
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce Consultants
        • Marketing Cloud Consultants
        • Service Cloud Consultants
        • Experience Cloud Consultants
        • Analytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud Consultants
        • Financial Service Cloud Consultants
        • Health Cloud Consultants
        • Commerce Cloud Consultants
        • Manufacturing Cloud Consultants
    • Jobs
    • Marketplace
    • Stories
    • Top Bloggers 2022
    • Advertise With Us
    • Quizzes
    • Discussions
    • Groups

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

    Reply

    Subscribe

    airslate

    SALESFORCE PRODUCT EXPERTISE

    • Top Salesforce Consultants
    • Top Salesforce Experience Cloud Consultants
    • Top Salesforce Analytics Cloud Consultants
    • Top Salesforce Service Cloud Consultants
    • Top Salesforce Marketing Cloud Consultants

    SALESFORCE INDUSTRY EXPERTISE

    • Top Salesforce Consultants
    • Top Salesforce Manufacturing Cloud Consultants
    • Top Salesforce Non-Profit Cloud Consultants
    • Top Salesforce Health Cloud Consultants
    • Top Salesforce Financial Service Cloud Consultants
    • Top Salesforce Commerce Cloud Consultants

    Popular Salesforce Blogs

    Process Builder in Salesforce

    Learn All About Process Builder in Salesforce and Its Features

    Blog in Salesforce Apex

    Would you like to make a record for any item from a work process? Would you like to call your Apex class from a work…

    Add Objects, Apex Class, Business Measures, Canvas, Catch Bar
    rajesh andani Sep 30, 2021
    1,839  Views

    How Salesforce is Better Than Other CRM?

    Blog in Salesforce

    Salesforce is renowned for its CRM software, which is widely used by businesses and industries. Their CRM has proven to be quite effective throughout different…

    Analytics and Reporting, Automated Marketing Campaigns, Business, Business Operations, chrome
    Cymetrix Nov 22, 2022
    1,301  Views
    Financial Services Cloud

    Relationships in Salesforce Financial Services Cloud

    Blog in Salesforce Admin

    Relationships. One might think that building a good relationship with Salesforce’s Financial Services Cloud would be harder than maintaining a personal relationship, but that is…

    Account and Contacts, Account and Contacts Fields, Account Fields, Additional Configurations, AppExchange
    rajesh andani Jun 1, 2022
    3,593  Views

    Popular Salesforce Videos

    Salesforce Pardot: 2020 Update

    Salesforce Pardot: 2020 Update

    Video in Salesforce Stories, Salesforce Products

    If you’re a current Salesforce user, you may be hearing a lot about Pardot these days. Perhaps you are using MailChimp, Campaign Monitor, or Constant…

    salesforce, Marketing Automation, Salesforce Video, Trailblazer, Leads
    Algoworks Oct 15, 2020
    583  Views
    5 Things Salesforce Lightning Does Better

    5 Things Salesforce Lightning Does Better

    Video in Lightning

    In this 30-minute recorded webinar, Jamie Grettum talked about her favorite Lightning features, how to set them up, and how to get hands-on experience with…

    Salesforce Lightning, How To Learn Salesforce Lightning, Lightning App Builder, How to Salesforce Lightning Component, Building Lightning Components in Salesforce
    Prafull May 18, 2018
    1,030  Views
    Salesforce Customer 360 Data Manager

    Salesforce Customer 360 Data Manager

    Video in Others

    Salesforce Customer 360 Data Manager is a cross-cloud integration capability that allows Salesforce admins and developers to combine and reconcile customer data from their Salesforce…

    Salesforce Products, Salesforce Developer, Salesforce Integration, Salesforce Admin, Salesforce Data Storage
    Parul Feb 27, 2020
    1,310  Views
    Footer Forcetalks logo

    [email protected]

    Forcetalks Android App
    Forcetalks IOS App

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2022

    Top Salesforce® Bloggers 2021

    Tips to choose Best Salesforce® Consulting Company

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    Submissions

    Write a Blog

    Start a New Discussion

    Post a Video

    Post an Infographic

    © 2023 - 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®.

    Forum Description

    Report

    There was a problem reporting this post.

    Harassment or bullying behavior
    Harassment or bullying behavior
    Contains mature or sensitive content
    Contains misleading or false information
    Mobile App Reported Contents
    Contains abusive or derogatory content
    Contains spam, fake content or potential malware

    Block Member?

    Please confirm you want to block this member.

    You will no longer be able to:

    • See blocked member's posts
    • Mention this member in posts
    • Invite this member to groups
    • Message this member
    • Add this member as a connection

    Please note: This action will also remove this member from your connections and send a report to the site admin. Please allow a few minutes for this process to complete.

    Report

    You have already reported this .
    Clear Clear All
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.