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
m23m23 

How to write a trigger to convert all the text fields to uppercase in account object using map and avoid using nested for loop.

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I dont think we need map In this scenerio. It can be simple trigger as below.
 
trigger AccountUpperCaseTrigger on Account (before insert, before update) {
    

    for(Account acc : Trigger.new){
            if(acc.name!=null )
            acc.Name = acc.Name.toUpperCase();
        if(acc.BillingStreet!=null)
            acc.BillingStreet = acc.BillingStreet.toUpperCase();
            //acc.BillingCity = acc.BillingCity.toUpperCase();


        }
    }

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
m23m23
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. 
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
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());
                }
            }
        }
Hope this should work.