I need to update all of the text fields in bulk so cant specify all the fields, looking for an answer in which I use map to extract all the text fields and put it.
Hi, execute below code to update the string field values to Uppercase dynamically. In Trigger code avoiding nested loop is not possible to update dynamically.
//get object related all string datatype fields in map format by using schema method.
public class AccountHandler {
public static Map<String, String> getDataType() {
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType accountSchema = schemaMap.get('Account');
Map<String, Schema.SObjectField> fieldMap = accountSchema.getDescribe().fields.getMap();
Map<String, String> strMap = new Map<String, String>();
for(String fieldName: fieldMap.keySet()) {
Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
boolean isUpdateable= fieldMap.get(fieldName).getDescribe().isUpdateable(); // exclude formula fields.
if(fielddataType == Schema.DisplayType.TextArea && isUpdateable) {
strMap.put(fieldName, fieldName);
} else if(fielddataType == Schema.DisplayType.String && isUpdateable) {
strMap.put(fieldName, fieldName);
}
}
return strMap;
}
}
//Trigger code to update the value to Uppercase dynamically for all string fields.
Map<String, String> strMap = AccountHandler.getDataType();
for(Account acc : Trigger.new) {
for(String str : strMap.keySet()) {
if(String.isNotBlank((String)acc.get(str))) {
String val = (String)acc.get(str);
acc.put(str, val.toUpperCase());
}
}
}
I dont think we need map In this scenerio. It can be simple trigger as below.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
execute below code to update the string field values to Uppercase dynamically. In Trigger code avoiding nested loop is not possible to update dynamically. Hope this should work.