You need to sign in to do that
Don't have an account?
Michael 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;
}
}
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;
}
}
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...
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
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...
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