Hello friends, In this post you will see an example that helps you to do dynamic soql query inside map. Let's understand this with an example.

Dynamic soql query in map

Most of us well known, how to do normal soql query inside map. That looks like below example.

Normal SOQL Query in Map

 String sRecordId='0010m00000kOpcVAAS';//Replace with your org account id
MAP<Id,Account> mapAccount=new MAP<Id,Account>([SELECT Id,Name FROM Account WHERE Id=:sRecordId]);
System.debug('Account^^*'+mapAccount);
Dynamic SOQL Query in Map
 String sRecordId='0010m00000kOpcVAAS';//Replace with your org account id
MAP<Id,Account> mapAccount=new MAP<Id,Account>((List<Account>)Database.query('SELECT Id,Name FROM Account WHERE Id = \'' + sRecordId + '\''));
System.debug('Account^^*'+mapAccount);

Output:

  

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.