1

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?

4
  • Turning on history tracking for that notes__c field isn't sufficient?
    – Keith C
    May 26, 2015 at 11:06
  • what will happen if i turn it on. May 26, 2015 at 11:13
  • All changes to the field will be listed providing you have added the "History" related list to the layout.
    – Keith C
    May 26, 2015 at 11:16
  • Yes I got it. but it'll not show me the changes made to notes__c field. May 26, 2015 at 11:19

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .