+ Start a Discussion
kj Smithkj Smith 

Trigger to update a long text field with available Notes bodies for that object record

I have a object called Incident__c and a custom field called Notes__c (Long Text Area).

Whenever a New Note is added to a Incident__c record, I want to populate Notes__c field with Note body.

If there are several Notes added to the Incident__c record, Notes__c field should be appended with each Note bodies.

Once a Note body is updates Note__c field should also be updated accordingly.

I'm currently struggling to make progress on this as i'm new to this scenario! How can I do this ?
sfdc Beginnersfdc Beginner

Hi Smith,

Try with below trigger your functionality will be achieved.
trigger PopulateNotesonIncidentTrg on Note (before insert, before update) {
    
    if(Trigger.isInsert || Trigger.isUpdate){
        
       Set<Id> incid = new Set<Id>();
        
        for(Note n : Trigger.new){
            incid.add(n.ParentId);
        }
        
       Map<Id,Incident__c> Incidents = new Map<Id,Incident__c>([Select Id,Notes__c from Incident__c where Id IN :incid]);
        
        
        for(Note n :Trigger.new){
            
            if( Incidents.size() > 0 && Incidents.containsKey(n.ParentId)){
                Incidents.get(n.ParentId).Notes__c = n.Body;
            }
        }
        
        update Incidents.values();
        
   }

}


If this solves your Problem, Mark it as the Best Answer.


Thanks,
SFDC Beginner.

 
kj Smithkj Smith
@sfdc Beginner thanks for your answer.

I want to seperate out the trigger and use a helper class, so I modified the code as below,

-----------------Helper Class--------------------------
public with sharing class NoteTriggerHelper {

public static void insertNotesBodyToTextField(){
Set<Id> incid = new Set<Id>();
     List<Note> notes = [SELECT Body FROM Note WHERE ParentId IN(SELECT Id FROM grc__Incident__c)];
        for(Note n : notes){
            incid.add(n.ParentId);
        }

        Map<Id, grc__Incident__c> incidents = new Map<Id, grc__Incident__c>([Select Id,Notes__c from grc__Incident__c where Id IN :incid]);
            for(Note n : notes){
                if(incidents.size() > 0 && incidents.containsKey(n.ParentId)){
                    incidents.get(n.ParentId).Notes__c = n.Body;
                }
            }
            update incidents.values();
    }
}

-----------------------Trigger-----------------------
rigger NoteTrigger on Note (after insert, after update) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            NoteTriggerHelper.insertNotesBodyToTextField(Trigger.newMap);
        }

        if(Trigger.isUpdate){
            NoteTriggerHelper.insertNotesBodyToTextField(Trigger.newMap);
        }
    }
}

but i'm getting an error when saving the trigger which says "NoteTrigger.trigger: Method does not exist or incorrect signature: NoteTriggerHelper.insertNotesBodyToTextField(Map<Id,Note>) (Line: 4, Column: 4)"
kj Smithkj Smith
@sfdc Beginner please ignore my previous comments I managed to get the above issue fixed.
Now the only issue is, when a new Note is added to the record my field updates the value without appending Note bodies. i'm trying to work around, how can I fix it ?
sfdc Beginnersfdc Beginner
trigger PopulateNotesonIncidentTrg on Note (before insert, before update) {
    
    if(Trigger.isInsert || Trigger.isUpdate){
        
       Set<Id> incid = new Set<Id>();
        
        for(Note n : Trigger.new){
            incid.add(n.ParentId);
        }
        
       Map<Id,Incident__c> Incidents = new Map<Id,Incident__c>([Select Id,Notes__c from Incident__c where Id IN :incid]);
        
        
        for(Note n :Trigger.new){
            
            if( Incidents.size() > 0 && Incidents.containsKey(n.ParentId)){
                Incidents.get(n.ParentId).Notes__c  += n.Body; // Change this Line
            }
        }
        
        update Incidents.values();
        
   }

}

Try to Modify the line which I Have Underlined your functionality willl be achieved.

Thanks,
SFDC Beginner