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
Michael MMichael M 

Before-insert trigger to divide long text area to paragraphs

We import a csv file daily to new records in a custom object. One of those fields is a long-text area, which I will call "notes". When the notes are imported, they come in as one very long string of text, and each note is separated by "*** yyyy-dd-mm***" (three asterics surrounding the date of the note). I want my "before insert" trigger to organize the long text area, such that a new paragraph begins with each new note. Can someone please assist with writing this trigger?  Here is what I have so far but it is not working:

trigger DischargeAfterInsert on Discharge__c (after insert) {

    for (discharge__c disch : trigger.new){
        string progressnotes = disch.progress_notes__c;
        progressnotes = progressnotes.replace('. ***', '. <br> ***');
        update disch;
            }
}
Best Answer chosen by Michael M
Andrew GAndrew G
A few things.

1.  You are doing an Update inside a FOR loop - bad practice, you will hit limits.
2.  Whilst you are manipulating the string, you are not writing it back to field that you wish to modify.
3.  You are doing an After Insert on the record you are updating. Before Insert would be better practice.
4.  I'm not sure is <br> works nicely in Long Text Area fields, so I would try \n

With all that in mind, try...
trigger SplitText on Discharge__c (before update) {

    for (Discharge__c disch : trigger.new){
        String progressnotes = disch.Long_Text__c;
        progressnotes = progressnotes.replace('***','\n');
        disch.Long_Text__c = progressnotes ;
    }
}

As it is Before Insert, there is no need to invoke the Update feature (or the insert either) as we are ...Before Insert.
So basically,
for each record, 
grab the string value from the field
manipulate the string
pass the manipulated string back to the field

HTH
Andrew
 

All Answers

Andrew GAndrew G
A few things.

1.  You are doing an Update inside a FOR loop - bad practice, you will hit limits.
2.  Whilst you are manipulating the string, you are not writing it back to field that you wish to modify.
3.  You are doing an After Insert on the record you are updating. Before Insert would be better practice.
4.  I'm not sure is <br> works nicely in Long Text Area fields, so I would try \n

With all that in mind, try...
trigger SplitText on Discharge__c (before update) {

    for (Discharge__c disch : trigger.new){
        String progressnotes = disch.Long_Text__c;
        progressnotes = progressnotes.replace('***','\n');
        disch.Long_Text__c = progressnotes ;
    }
}

As it is Before Insert, there is no need to invoke the Update feature (or the insert either) as we are ...Before Insert.
So basically,
for each record, 
grab the string value from the field
manipulate the string
pass the manipulated string back to the field

HTH
Andrew
 
This was selected as the best answer
Michael MMichael M
Excellent, thank you very much.