0

I need to check if the org has middle names enabled for field copying from one record to another. A solution I came up with is checking if the sObject fields contains MiddleName.

However is there a way to modify the org settings via Apex so I can configure the setting for testing?

1 Answer 1

2

Changing organization settings cannot happen in a unit test. Nor should be necessary to do so. You can do a generic copy of selected fields with a Set and some Maps:

Set<String> fieldsToCopy = new Set<String> { 'FirstName','MiddleName','LastName'};
Map<String, Object> valuesFromRecord = record.getPopulatedFieldsAsMap();
valuesFromRecord.keySet().retainAll(fieldsToCopy);
for(String field: valuesFromRecord) {
  record2.put(field, valuesFromRecord.get(field));
}

If the field names would differ, you can also create a mapping for that as well:

Map<String, String> fieldMap = new Map<String, String> {
  'FirstName' => 'MiddleName__c'
  'MiddleName' => 'MiddleName__c'
  'LastName' => 'MiddleName__c'
};

And:

for(String field: valuesFromRecord) {
  record2.put(fieldMap.get(field), valuesFromRecord.get(field));
}

You must log in to answer this question.

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