Davide Gammone
How to create a Map in Salesforce with Keys = Fields Name and Values​ = Query Result

A Map is a collection of Key-Value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

Example:
this table represents a map of countries and currencies:

Country (Key) Japan France England Italy
Currency (Value) Yen Euro Pound Euro

An SObject record has a very map-like structure by default.

Account record = [SELECT Name, Phone FROM Account LIMIT 1];
system.debug(record.get('Name'));
system.debug(record.get('Phone'));

Execution Log:

USER_DEBUG [5]|DEBUG|Mario Rossi
USER_DEBUG [6]|DEBUG|null

It even supports put:

record.put('Phone', '0811234567');

However, if you absolutely must have a map for some reason, you can use serialization (CPU intensive process) as follows:

String stringAcc = JSON.serialize(record);
Map<String, Object> mapAcc = (Map<String, Object>)JSON.deserializeUntyped(stringAcc);
mapAcc.remove('attributes');

Apex code and query for many records:

List<Account> record = [SELECT Name, Phone FROM Account LIMIT 2];
List<Map<String, Object>> listTotal = new List<Map<String, Object>>();
for(Account forAcc : record)
{
    system.debug(forAcc.get('Name'));
    system.debug(forAcc.get('Phone'));
    forAcc.put('Phone', '0811234567');
    String stringAcc = JSON.serialize(forAcc);
    system.debug(stringAcc);
    Map<String, Object> mapAcc = (Map<String, Object>)JSON.deserializeUntyped(stringAcc);
    mapAcc.remove('attributes');
    system.debug(mapAcc);
    listTotal.add(mapAcc);
}
system.debug(listTotal);

Execution Log:

USER_DEBUG|[5]|DEBUG|Mario Rossi
USER_DEBUG|[6]|DEBUG|null
USER_DEBUG|[9]|DEBUG|{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0011w000004rocHAAQ"},"Name":"Mario Rossi","Id":"0011w000004rocHAAQ","RecordTypeId":"0121w0000004OmvAAE","Phone":"0811234567"}
USER_DEBUG|[12]|DEBUG|{Id=0011w000004rocHAAQ, Name=Mario Rossi, Phone=0811234567, RecordTypeId=0121w0000004OmvAAE}
USER_DEBUG|[5]|DEBUG|Luca Verdi
USER_DEBUG|[6]|DEBUG|null
USER_DEBUG|[9]|DEBUG|{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0011w000004ronHAAQ"},"Name":"Luca Verdi","Id":"0011w000004ronHAAQ","RecordTypeId":"0121w0000004OmvAAE","Phone":"0811234567"}
USER_DEBUG|[12]|DEBUG|{Id=0011w000004ronHAAQ, Name=Luca Verdi, Phone=0811234567, RecordTypeId=0121w0000004OmvAAE}
USER_DEBUG|[15]|DEBUG|({Id=0011w000004rocHAAQ, Name=Mario Rossi, Phone=0811234567, RecordTypeId=0121w0000004OmvAAE}, {Id=0011w000004ronHAAQ, Name=Luca Verdi, Phone=0811234567, RecordTypeId=0121w0000004OmvAAE})

Generic code to query all fields and put them into a Map<String,String> is:

SObjectType sobType = Account.SObjectType;
List<Map<String, Object>> listTotal = new List<Map<String, Object>>();

List<String> fields = new List<String>(sobType.getDescribe().fields.getMap().keySet());
for(SObject sob : Database.query(
       ' select ' + String.join(fields, ',')
    + ' from ' + sobType
    + ' limit 2'))
{
    Map<String,String> mapObj = new Map<String,String>();
    for (String field : fields)
    {
        Object value = sob.get(field);
        if(value != null){
            mapObj.put(field, String.valueOf(value));
        }
    }
    listTotal.add(mapObj);
}
System.debug(JSON.serialize(listTotal));

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Read about our Cookie Policy.