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
TerminusbotTerminusbot 

Apex Trigger: Update a field on Opportunity record when a new note is inserted.

Hello, 

I have created a field on the Opportunity object called Latest Note. When a user adds a new note to the Opportunitues Notes section I want to copy the Body and insert it to the field "Latest Note". Can a trigger be created on the Note object to handle this?

Thanks for all your help,
Ricky 
 
Best Answer chosen by Terminusbot
ManojjenaManojjena
Hi Terminusbot,

Try with below code it will help ,Check the Api name of opportunity field .
 
trigger UpdateOppoetunity on Note (After insert,After update) {
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            if(String.valueOf(nt.parentId).startsWith('006')){
                Opportunity opp=new Opportunity(Id=nt.parentId,Lastest_Note__c=nt.Body); 
                oppLstToUpdate.add(opp);
            }   
        }
    }if(Trigger.isUpdate){
        for(Note nt : Trigger.new){
		    if(String.valueOf(nt.parentId).startsWith('006')){
			    if(nt.Body != Trigger.oldMap.get(nt.Id).Body){
				   Opportunity opp=new Opportunity(Id=nt.parentId,Lastest_Note__c=nt.Body); 
					oppLstToUpdate.add(opp);
			    }
		    }
        }
    }
    if(!oppLstToUpdate.isEmpty()){
        try{
            update oppLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    }
}

Go to developer console from there you can create trigger file>new >apex triggger .

Let us know if it helps !!
Thanks
Manoj

All Answers

Pramodh KumarPramodh Kumar
yeah this can be implemented by writing the trigger on the note object.

Here is the sample code for the scenario.
trigger noteAfterTrigger on note__c (after insert, after update) {
    
    list<Opportunity> OpportunityToupdate = new list<Opportunity>(); 
    list<id> OpportunityID = new list<id>(); 
    
    if(Trigger.isInsert){ //opportunity id
        for(note__c tca : Trigger.new){
            OpportunityID.add(tca.Opportunity__c);
        }
    }
    map<id, Opportunity> OpportunityMap = new map<id,Opportunity>([select id,Latest_Note__c
                                                               from  Opportunity where id IN: OpportunityID]);
    
    
    for(note__c tca : Trigger.new)
    {
		OpportunityMap.get(tca.Opportunity__c).Latest_Note__c = tca.note__c
        OpportunityToupdate.add(OpportunityMap.get(tca.Opportunity__c));
    }
    if(!OpportunityToupdate.isEmpty()){update OpportunityToupdate;} 
}

Thanks,
pRAMODH.
ManojjenaManojjena
Hi Terminusbot,

Try with below code it will help ,Check the Api name of opportunity field .
 
trigger UpdateOppoetunity on Note (After insert,After update) {
    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            if(String.valueOf(nt.parentId).startsWith('006')){
                Opportunity opp=new Opportunity(Id=nt.parentId,Lastest_Note__c=nt.Body); 
                oppLstToUpdate.add(opp);
            }   
        }
    }if(Trigger.isUpdate){
        for(Note nt : Trigger.new){
		    if(String.valueOf(nt.parentId).startsWith('006')){
			    if(nt.Body != Trigger.oldMap.get(nt.Id).Body){
				   Opportunity opp=new Opportunity(Id=nt.parentId,Lastest_Note__c=nt.Body); 
					oppLstToUpdate.add(opp);
			    }
		    }
        }
    }
    if(!oppLstToUpdate.isEmpty()){
        try{
            update oppLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    }
}

Go to developer console from there you can create trigger file>new >apex triggger .

Let us know if it helps !!
Thanks
Manoj
This was selected as the best answer
TerminusbotTerminusbot
Thank you!

I created a trigger and updated the API name on the Latest Note field. As a test I created a new note and updated the same note on an Opportunity. The Latest Note field was empty on both accounts. Looks like it's not updating the Latest Note field on creation or update. 
 
trigger UpdateOppLastNote on Note (after insert, after update) {


List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            if(String.valueOf(nt.parentId).startsWith('006')){
                Opportunity opp=new Opportunity(Id=nt.parentId,Latest_Note__c=nt.Body); 
                oppLstToUpdate.add(opp);
            }   
        }
    }if(Trigger.isUpdate){
        for(Note nt : Trigger.new){
		    if(String.valueOf(nt.parentId).startsWith('006')){
			    if(nt.Body != Trigger.oldMap.get(nt.Id).Body){
				   Opportunity opp=new Opportunity(Id=nt.parentId,Latest_Note__c=nt.Body); 
					oppLstToUpdate.add(opp);
			    }
		    }
        }
    }
    if(!oppLstToUpdate.isEmpty()){
        try{
            update oppLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    }


}

 
TerminusbotTerminusbot
I was having an issue with the new notes feature. When I switched to classic and added a note from the Notes and Attachments section it worked. 
Chantinique BakerChantinique Baker
So, in this code how do you enable it to work only for a specific Opportunity Record Type?? For example:
Record Type Name: OEM Programs
or Record Type Id?
June PowerJune Power
I added a trigger on Note with this scenario which works.  I also need a test class.  Can anyone assist?
 
trigger UpdateOppLastNote on Note (after insert, after update) {


List<Jr_Processing_Tasks__c> oppLstToUpdate=new List<Jr_Processing_Tasks__c>();
    if(Trigger.isInsert){
        for(Note nt : Trigger.new){
            {
                Jr_Processing_Tasks__c opp=new Jr_Processing_Tasks__c(Id=nt.parentId,Latest_Note__c=nt.Body); 
                oppLstToUpdate.add(opp);
            }   
        }
    }if(Trigger.isUpdate){
        for(Note nt : Trigger.new){
            {
                if(nt.Body != Trigger.oldMap.get(nt.Id).Body){
                   Jr_Processing_Tasks__c opp=new Jr_Processing_Tasks__c(Id=nt.parentId,Latest_Note__c=nt.Body); 
                    oppLstToUpdate.add(opp);
                }
            }
        }
    }
    if(!oppLstToUpdate.isEmpty()){
        try{
            update oppLstToUpdate;
        }catch(DmlException de ){
            System.debug(de);
        }
    }


}