Tuesday 10 December 2013

How to get a map from database.query()

In salesforce doc you may found that it is possible to get a map from a query instead of a list like this:

Map<ID, Contact> m = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);

But you can also achieve this if you are using database.query() to run the select.This can be achieved like follows :
 String query = 'SELECT Id, LastName FROM Contact';
Map<id,Contact> mapCon = new Map<id,Contact>((List<Contact>)Database.query(query));