You need to sign in to do that
Don't have an account?
Deanna Aaron 11
Trigger to update a custom field in the Account object when a Note is being created?
I want to have a field on the account object called “most recent note”. Basically, when a new note is created, the “note” create date populates here.
BONUS: If this is possible— I want to have a field on the account object called “Recent Note Content”. Basically, the most recent note’s content goes here.
How would I accomplish this? I sincerely appreciate your help!
BONUS: If this is possible— I want to have a field on the account object called “Recent Note Content”. Basically, the most recent note’s content goes here.
How would I accomplish this? I sincerely appreciate your help!
Please try this and let me know if you still face any issue.
trigger updateMost_Recent_Note on Note (after insert) {
List<Account> accLstToUpdate = new List<Account>();
for(Note nt : Trigger.new){
if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
Account acc=new Account(Id=nt.parentId)
acc.Most_Recent_Note__c =nt.Body;
acc.Most_Recent_Note_Date__c= nt.createdDate;
accLstToUpdate.add(acc);
}
}
if(!accLstToUpdate.isEmpty()){
update accLstToUpdate;
}
}
All Answers
Could you please try this ?
trigger updateMost_Recent_Note on Note (after insert) {
List<Account> accLstToUpdate = new List<Account>();
for(Note nt : Trigger.new){
if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
Account acc=new Account(Id=nt.parentId,Recent_Note_Content__c=nt.Body,most_recent_note__c = nt.createdDate);
accLstToUpdate.add(acc);
}
}
if(!accLstToUpdate.isEmpty()){
update accLstToUpdate;
}
}
Note: This is the API name for the field created on the account object: Most_Recent_Note__c
Please use this
Account acc=new Account(Id=nt.parentId,Most_Recent_Note__c=nt.Body);
and if you are going to store the most recent note's createddate data, created a new datetime field and use it in the above code.
In inserted the API name below
trigger updateMost_Recent_Note on Note (after insert) {
List<Account> accLstToUpdate = new List<Account>();
for(Note nt : Trigger.new){
if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
Account acc=new Account(Id=nt.parentId,Recent_Note_Content__c=nt.Body,most_recent_note__c = nt.createdDate);
accLstToUpdate.add(acc);
}
}
if(!accLstToUpdate.isEmpty()){
update accLstToUpdate;
}
}
RECEIVED THIS ERROR
Error: Compile Error: Illegal assignment from String to Datetime at line 5 column 25
I'm not sure where to insert this?
Account acc=new Account(Id=nt.parentId,Most_Recent_Note__c=nt.Body);
Thank you for being so patient.
Please try this and let me know if you still face any issue.
trigger updateMost_Recent_Note on Note (after insert) {
List<Account> accLstToUpdate = new List<Account>();
for(Note nt : Trigger.new){
if(nt.ParentId != null && nt.ParentId.getSObjectType() == Account.sObjectType){
Account acc=new Account(Id=nt.parentId)
acc.Most_Recent_Note__c =nt.Body;
acc.Most_Recent_Note_Date__c= nt.createdDate;
accLstToUpdate.add(acc);
}
}
if(!accLstToUpdate.isEmpty()){
update accLstToUpdate;
}
}
Thank you SO very much.