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
Vivek KidambiVivek Kidambi 

covert Map<String,Object> to Map<String,String>

I have a method which creates Map<String,Object> by querying a object. I need to pass this value to a future method which has callout. Since callout method does not accept Sobject type as parameter, i need to convert this Map<String,Object> to Map<String,String>. 

Appreciate any insights on this issue

List<SObject> listObjectRecords = database.query('select ' + cusFields + ' from '+ objName + ' WHERE ID = \'' + objId + '\'');
 for(SObject sObj : listForMap) {
Map<String,Object> mapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(sObj));
}



 
James LoghryJames Loghry

What is the key of the map? The record Id?  What is the value of the map? a JSON representation of the sobject? Think we need a few more details to help you out.  That being said, going off my previous assumptions, here's how you might do it:

Map<String,String> mapObj = new Map<String,String>();
for(sObject sobj : Database.query(...)){
    mapObj.put(sobj.Id,JSON.serialize(sobj));
​}
Vivek KidambiVivek Kidambi
Please see below code 
List<SObject> listObjectRecords = database.query('select ' + cusFields + ' from '+ objName + ' WHERE ID = \'' + objId + '\'');
 for(SObject sObj : listObjectRecords ) {
Map<String,Object> mapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(sObj));
}

listObjectRecords returns me a list which is used to build map<string,object>. Here String is name of the field and Object is value of the field. But i need this as Map<String,String> instead of Map<String,Object>
Virendra ShekhawatVirendra Shekhawat
Hi @Vivek, did you get any solution? I also have same requirement to use Object as string. 
Mahesh DMahesh D
You can try the below solution:
 
Map<String, Object> mapToSerialize = new Map<String, Object>();
mapToSerialize.put('type', 'TEST');
mapToSerialize.put('subtypes', new List<String>{'TEST'});
String jsonString = JSON.serialize(mapToSerialize);

Map<String, String> finalMap = new Map<String, String>();
finalMap.put('name', jsonString);

Regards,
Mahesh​