0

I am trying to get all record fields and transfer them to another org. But getPopulatedFieldsAsMap doesn't give all fields. How can i retrieve all fields?

conList is trigger.new

for(List<Contact> conList : contact){
        Map<String, Object> contactMap = new Map<String, Object>(contact.getPopulatedFieldsAsMap());
...
}
0

2 Answers 2

1

The data in the map returned by SObject.getPopulatedFieldsAsMap is, as suggested by the name, only those fields that have been "populated".

Fields are populated on an SObject by ensuring they are queried, or by assigning values to them using static or dynamic schema mechanisms.

An example of setting a field using static schema access is:

Account myAccount = ...;

myAccount.Name = 'Example of static schema setting of the Name field';

An example of doing the same thing with dynamic access is:

String fieldName = 'Name';
myAccount.put(fieldName, 'Example of dynamic setting of the Name field');

There are other mechanisms too.

However, in your case I suspect you want to simply query all the fields for all the Contact records so you actually need to build an appropriate query to handle this. (Triggers automatically populate all fields, but outside triggers this must be done explicitly.)

You'll find plenty of examples, using dynamic SOQL and the Schema API, if you do a simple internet search like "salesforce query all fields from object" which includes this previous Q&A. Note, however, that you'll need to do this in something like a batch implementation to avoid hitting heap, query or callout limits when transferring the data to the other org.

Using a batch with a limit of 200 records per chunk may allow you to use the new FIELDS feature in SOQL, but I'm not 100% sure.

1

Try code below

Map<String, Object> contactMap = contact.getPopulatedFieldsAsMap();

getPopulatedFieldsAsMap() return a specific instance of the map, you don't need to initialize this map. Also, you can try to serialize and then deserialize using JSON

1
  • says that collecion is read only. I think cause trigger return fields like id . So i must initialize map.
    – Solmetirka
    Aug 19, 2021 at 9:05

Not the answer you're looking for? Browse other questions tagged .