apex-codevisualforceapexsalesforce-service-cloudsalesforce-chatter

Need to access the name of field from map of Sobject


I have a map of sobjects like this:

Map<String,list<sobject>> recordIdsMap = new Map<String,list<sobject>>();

Now my requirement is to iterate over this map, access the field and assign some value to it.

Code that I am currently trying for this:

for(Sobject target: recordIdsMap.values()){

  target.BR_District__c = recorddestinationId;
  obj.add(target);

} 

But this isn't able to access the field name BR_District__c because it can't identify the object type.


Solution

  • I suggest you use put(fieldName, value) method on your sObject (target in your case).

    Should be smth like

    for(Sobject target : recordIdsMap.values()) {    
      target.put('BR_District__c', recorddestinationId);
      obj.add(target);    
    }