+ Start a Discussion
Katjag8Katjag8 

Use trigger to create a Note - possible?

Is it possible to use a trigger to create a note on a object? have a text field value that I need to populate within Notes & Attachments on custom object. Any help is greatly appreciated.

Chris760Chris760

Sure is.  You'd just create the trigger on the custom object where the "note" field is and save it as a new record on the "Note" object.  Might be tricky logic though... like for example, when you click save, would the value you wrote in the note field stay in that field, as well as create the note on the attachments object?  If so, would modifying it create a new note with all the same information populated in yet a new note record?  Or would you want the note field on the custom object to delete as soon as the note record is created, thereby making the note field *always* blank in View mode on the custom object?

 

Once you figure out the logic that you want to apply, it shouldn't be too hard at all, assuming you're familair with writing triggers.

Katjag8Katjag8

New to writing triggers and from that couldn't it be only on Insert, Edit that would create the Note within Note & Attachments?

Dhaval PanchalDhaval Panchal

Try below trigger

 

trigger trgCreateNote on CustomObject1__c (after insert, after update) {
    List<Note> lstNote = new List<Note>();
    Set<ID> setParentToRemove = new Set<ID>();
    Map<Id, String> mapUpdate = new Map<Id, String>();
    for(CustomObject1__c obj:Trigger.New){
        if(Trigger.IsInsert || (Trigger.IsUpdate && NVL(obj.Description__c) <> NVL(Trigger.oldMap.get(obj.Id).Description__c) && NVL(Trigger.oldMap.get(obj.Id).Description__c)=='')){
            Note note = new Note();
            note.Body = obj.Description__c;
            note.ParentId = obj.Id;
            note.Title = obj.Name;
            note.IsPrivate = true;
            lstNote.add(note);
        }
        if(Trigger.IsUpdate && NVL(obj.Description__c) == '' && NVL(Trigger.oldMap.get(obj.Id).Description__c) <> ''){
            setParentToRemove.add(obj.Id);
        }
        if(Trigger.IsUpdate && NVL(obj.Description__c) <> '' && NVL(Trigger.oldMap.get(obj.Id).Description__c) <> '' && obj.Description__c <> Trigger.oldMap.get(obj.Id).Description__c){
            mapUpdate.put(obj.Id, obj.Description__c);
        }
    }
    if(lstNote.size()>0){
        insert lstNote;
    }
    if(setParentToRemove.size()>0){
        List<Note> lstNotesToRemove = [Select Id from Note Where ParentId in:setParentToRemove];
        if(lstNotesToRemove.size()>0){
            delete lstNotesToRemove;
        }
    }
    if(mapUpdate.size()>0){
        List<Note> lstNotesToUpdate = [Select Id, ParentId, Body from Note Where ParentId in:mapUpdate.KeySet()];
        if(lstNotesToUpdate.size()>0){
            for(Note note:lstNotesToUpdate){
                note.Body = mapUpdate.get(note.ParentId);
            }
            Update lstNotesToUpdate;
        }
    }
    String NVL(String str){
        if(str == null)
            return '';
        else 
            return str.trim();
    }
}