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?

  • Turning on history tracking for that notes__c field isn't sufficient? – Keith C May 26 '15 at 11:06
  • what will happen if i turn it on. – Vrushabh Jain May 26 '15 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 '15 at 11:16
  • Yes I got it. but it'll not show me the changes made to notes__c field. – Vrushabh Jain May 26 '15 at 11:19
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.