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
Rocks_SFDCRocks_SFDC 

How to update Opportunity whenever a new Note is Added by Apex Trigger

Hi Team,

We have Notes & Attachments related lists to Opportunity. And i have Date__c field on Opportunity.

I want to update Date__c field on Opportunity with recently note created date.


Please let me know if you have any suggestions.

Thanks,
Anil
Best Answer chosen by Rocks_SFDC
CheyneCheyne
You should be able to write a trigger on Note to accomplish this. It's a little tricky to create a trigger on Note through the UI, since Salesforce doesn't provide a link to create Note triggers the way it does for other objects. However, if you open up a new trigger page for any other object, you'll notice somewhere in the URL the parameter

entity=<ObjectName>

Replace <ObjectName> with Note and it should give you a page to create a trigger on Note. You should also be able to create the trigger using the Force.com IDE.

The actual trigger might look like this:

trigger SetOppDate on Note (after insert) {
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for (Note n : trigger.new) {
        //Check if the note is related to an opportunity
        if (n.ParentId.startsWith('006')) {
            //Set the Date__c field on the opportunity
            Opportunity opp = new Opportunity(Id = n.ParentId, Date__c = Date.Today());
            oppsToUpdate.add(opp);
        }
    }
    update oppsToUpdate;
}

All Answers

CheyneCheyne
You should be able to write a trigger on Note to accomplish this. It's a little tricky to create a trigger on Note through the UI, since Salesforce doesn't provide a link to create Note triggers the way it does for other objects. However, if you open up a new trigger page for any other object, you'll notice somewhere in the URL the parameter

entity=<ObjectName>

Replace <ObjectName> with Note and it should give you a page to create a trigger on Note. You should also be able to create the trigger using the Force.com IDE.

The actual trigger might look like this:

trigger SetOppDate on Note (after insert) {
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for (Note n : trigger.new) {
        //Check if the note is related to an opportunity
        if (n.ParentId.startsWith('006')) {
            //Set the Date__c field on the opportunity
            Opportunity opp = new Opportunity(Id = n.ParentId, Date__c = Date.Today());
            oppsToUpdate.add(opp);
        }
    }
    update oppsToUpdate;
}
This was selected as the best answer
Rocks_SFDCRocks_SFDC
Hi Cheyne,

Thank you for your reply.

When i executed the above, i am getting the following error

Error: Compile Error: Method does not exist or incorrect signature: [Id].startsWith(String) at line 6 column 13

Please help!!

Thanks,
Anil
Rocks_SFDCRocks_SFDC
Got the Solution. Here it is:

trigger SetOppDate on Note (after insert) {

List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for (Note nRecord : trigger.new) {
        //Check if the note is related to an opportunity
        String parentIdString = String.valueof(nRecord.parentid);
        if (parentIdString.substring(0,3) == '006'){
            //Set the Date__c field on the opportunity
            Opportunity opp = new Opportunity(Id = nRecord.ParentId, Date__c=TODAY());
            oppsToUpdate.add(opp);
       }
    }
    update oppsToUpdate;

}

CheyneCheyne
Ah, my apologies, I forgot that startsWith only works with strings, not Ids. 

You may also need to change TODAY() to Date.Today().
Rocks_SFDCRocks_SFDC
yeah Cheyne.

Thanks for your Reply !!!