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

    The Ultimate Guide To Making The Name Field Unique In Salesforce

    The Ultimate Guide To Making The Name Field Unique In Salesforce

    Blog in Salesforce

    The Salesforce platform has a unique function that allows users to create a custom page layout. This is a powerful tool since you are able…

    Augmenting Marketing Campaigns, Automated Marketing Campaigns, B2B Marketing Campaigns, Cloud Based Customer Relationship Management Software, Customer Relationship Management
    Ashley Jan 11, 2023
    3,078  Views
    Percentage Calculator

    Standard and Custom Percentage Calculator in Salesforce Lightning Component

    Blog in Others, Salesforce

    Requirement We are going to calculate some standard percentage and custom percentage i.e. any percent you want to calculate any input number. On click of…

    Component Configuration, Custom Percentage, Developer Console, Lightning Component, Lightning Experience
    Deepak Oct 19, 2020
    6,448  Views
    Unravel the Advantages of Salesforce Marketing Cloud

    Unravel the Advantages of Salesforce Marketing Cloud

    Blog in Salesforce Products

    Salesforce has continued to be a market leader in offering Customer Relationship Management (CRM). Presently, it holds over 19.5% share of the market, defeating all…

    Automation, Behaviors, Building Customer Relationships, Campaign Management, Campaign Performance
    Melonleaf Apr 11, 2023
    1,460  Views

    Popular Salesforce Videos

    What is Billing - Salesforce Revenue Cloud

    What is Billing - Salesforce Revenue Cloud

    Video in Salesforce Cloud Platform

    How can you transform your billing system for subscription and usage models? By dropping your legacy systems and streamlining your processes. In this video, learn…

    Salesforce Training, Salesforce Video, Customers, Salesforce Learning, Subscription
    Jogender Jan 27, 2021
    2,193  Views
    How to Learn Salesforce Development | Salesforce learning steps for beginners | Trailhead demo

    How to Learn Salesforce Development | Salesforce learning steps for beginners | Trailhead demo

    Video in Salesforce Training, Trailhead

    In this video, we shared the steps to learn salesforce lightning basics, salesforce apex development, CRM in online for free from scratch very quickly. Learn salesforce…

    Salesforce Training, Salesforce Tutorial, salesforce training videos for beginners, How To Learn Salesforce Development, Salesforce Learning Steps For Beginners
    Shiv Kumar Sharma Jan 18, 2018
    2,848  Views
    How To Set Up DKIM Key In Salesforce?

    How To Set Up DKIM Key In Salesforce?

    Video in Salesforce Admin

    If you use Salesforce for sending emails, we highly recommend setting up DKIM authentication to sign your outgoing messages and improve their deliverability and trustworthiness.…

    Salesforce Admin, Salesforce VIdeos, Videos, DKIM, Salesforce DKIM
    ENWAY Jan 22, 2024
    1,134  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.