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:

how to insert line break to long text area field from api.. I have tried n, rn but nothing works.. Please help!
Don't use n or r from the API.
Use String.escapeSingleQuotes() in Apex before the field update in the class.
Is there a code available with a String.escapeSingleQuotes?
Yes in apex.
https://www.infallibletechie.com/2019/11/how-to-protect-against-soql-injection.html
how to do the same over the Rich text area ?
Use
tage.
Check this for example – https://www.infallibletechie.com/2021/05/how-to-add-new-line-in-richtext-area.html
I want to use a line break for a flow functionality. can i use a apex code for this on the long text field and if so, which code must i use for the linebreak?
Line Break is not supported in Flow – https://ideas.salesforce.com/s/idea/a0B8W00000GdbiIUAR/add-newline-carriage-return-in-flow. Check the workaround provided.