function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC16SFDC16 

How to use map


Hello Developer,

How to use map for below code 



global class DisplayDashboard
{
             
    public List <Dashboard>  fetchdashboard{get;set;}

    Public DisplayDashboard()
    {
       
        List<User> cuurentUser=[SELECT Id, Username,Title FROM User where username=:UserInfo.getUsername()];
        System.debug('Title-------->'+cuurentUser);
        
        System.debug(cuurentUser);
       
        List<String> str=new List<String>();    
        System.debug('str----------->'+str);
        for(User u:cuurentUser) 
        {
            str.add(cuurentUser[0].Title);
            
        }
       System.debug('str----------->'+str);

        for(Dashboard dash : [SELECT Id, Title FROM Dashboard])
        {
            for(User u:[SELECT Id, Username,Title FROM User where Title In:str] )
            {     
                if(dash.title.Contains(u.title))
                    {
                    fetchdashboard=[SELECT Id, Title FROM Dashboard];    
                    System.debug('fetchdashboard-------------->'+fetchdashboard);
                }
            }    
        }
    
    }     
 }


Regards,
SFDC16
Ajay K DubediAjay K Dubedi
Hi,

In this code, you don't need to use a map and you have to change code because of SOQL query in for loop, not the best practice.
So You can use this below code:

global class DisplayDashboard
{
    public List <Dashboard>  fetchdashboard{get;set;}

    Public static void Display_Dashboard()
    {
        List<User> cuurentUser=[SELECT Id, Username,Title FROM User where username=:UserInfo.getUsername()];
        System.debug('Title-------->'+cuurentUser);
        System.debug(cuurentUser);   
        String str = cuurentUser[0].Title;
        System.debug('str----------->'+str);
        if(!isBlank(str)){
            fetchdashboard=[SELECT Id, Title FROM Dashboard where Title =: str LIMIT 10000];
        }
    }     
 }
If you want to read then you can go throw the below link and below code. In this code, we create a map which keys as AccountId and values related contacts.
Code:
public class AccountIdVSContact {
    public static void accountIdVSContact_Method(){
        List<Contact> contactList = new List<Contact>();
        contactList = [SELECT Id, Name, AccountId FROM Contact WHERE AccountId != null LIMIT 10000];
        System.debug(contactList);
        Map<Id,List<Contact>> accIdVScontactList = new Map<Id,List<Contact>>();
        if(contactList.size()>0){
            for(Contact conObj:contactList){
                if(!accIdVScontactList.containsKey(conObj.AccountId)){
                    accIdVScontactList.put(conObj.AccountId,new  List<Contact>());
                }
                accIdVScontactList.get(conObj.AccountId).add(conObj);
            }
            System.debug('accIdVScontactList'+accIdVScontactList);
        }
    }
}

Link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi