You need to sign in to do that
Don't have an account?
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
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
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
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;
}
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
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;
}
You may also need to change TODAY() to Date.Today().
Thanks for your Reply !!!