How to add new line in Long Text Area field using Apex in Salesforce?

How to add new line in Long Text Area field using Apex in Salesforce?

Escape Sequence \n can be used to add new line in Long Text Area field using Apex in Salesforce.

Sample Text Area Long Field:

Sample Code:

trigger AccountTrigger on Account ( before update ) {

    for ( Account objAcc : trigger.new ) {
    
        Account objAccWithPriorValues = trigger.oldMap.get( objAcc.Id );
        
        if ( objAcc.Type != objAccWithPriorValues.Type ) {
            
            String strTypeTrackValue = 'Type value is changed from ' +
                    objAccWithPriorValues.Type + ' to ' + objAcc.Type + 
                    ' on ' + System.now();
        
            if ( String.isBlank( objAcc.Type_Change_Tracking__c ) ) {
                
                objAcc.Type_Change_Tracking__c = strTypeTrackValue;
                
            } else {
                
                objAcc.Type_Change_Tracking__c += '\n' + strTypeTrackValue;
                
            }
        
        }
    
    }

}

Output:

Leave a Reply