I want to create notes (not attachment) using trigger on standard contact object. I have 1 custom field called as notes__c, which i want to be reflected in notes once trigger gets fired. Is it possible to create notes on every time contact is created and updated? If yes then how can i achieve that?
1 Answer
Try this trigger code :
trigger insertNote on Contact (after insert, after update){
List<Note> noteList = new List<Note>();
if(Trigger.isAfter && Trigger.isInsert){
for(Contact c : trigger.new){
if(c.notes__c != null){
Note newNote = new Note();
newNote.Title = 'Custom Title';
newNote.Body = c.notes__c;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(Trigger.isAfter && Trigger.isUpdate){
for(Contact c : trigger.new){
if(trigger.oldMap.get(c.id).notes__c != c.notes__c){
Note newNote = new Note();
newNote.Title = 'Custom Title';
newNote.Body = c.notes__c;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(noteList.size() > 0){
insert noteList;
}
}
Hope this may help.
notes__c
field isn't sufficient?