You need to sign in to do that
Don't have an account?
Ryan Greene
add note with apex trigger
Hello,
I created a very simple trigger to add a Note when a Lead is created. The Note is not getting added, what am I doing wrong? (Code below) Is it not working because the Lead is not yet created? With an After Insert this shouldn't matter though. Notes are turned on and are working to manually create them.
Thanks,
Ryan
I created a very simple trigger to add a Note when a Lead is created. The Note is not getting added, what am I doing wrong? (Code below) Is it not working because the Lead is not yet created? With an After Insert this shouldn't matter though. Notes are turned on and are working to manually create them.
Thanks,
Ryan
trigger AddNote on Lead (after insert) { List<Note> addnte = new List<Note>(); for(Lead lds : Trigger.new){ Note addedntes = new Note(); addedntes.ParentId = lds.Id; addedntes.Body = lds.Sales_Notes__c; addedntes.Title = 'Creation Note'; addnte.add(addedntes); } if(addnte.size()>0){ insert addnte; } }
Basically you need to create the ContentDocumentLink to link it to both the ContentNote and the sObject.
All Answers
Your code is working fine.Please check once the trigger is active or not.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
This code is working fine. When you add new Lead record then it will add equal number of Note record.
Thanks
Anyway I can add a Note to just the Notes list?
Basically you need to create the ContentDocumentLink to link it to both the ContentNote and the sObject.
I am trying to use your code with slight change to create notes only when a field chnage on the object. But its creating 3 entries for the notes. please can you help me why its creating duplicates?
trigger AddNote_Workorder on WorkOrder (Before update) {
List<ContentNote> nte = new List<ContentNote>();
List<ContentDocumentLink> lnk = new List<ContentDocumentLink>();
for(WorkOrder lds : Trigger.new){
if(Trigger.oldmap.get(lds.id).Next_Action_Notes__c!= Trigger.newmap.get(lds .id).Next_Action_Notes__c){
ContentNote cnt = new ContentNote();
cnt.Content = Blob.valueof(lds.Next_Action_Notes__c);
cnt.Title =lds.CreatedDate + ' Creation Note';
nte.add(cnt);
}
}
if(nte.size()>0){
insert nte;
}
for(WorkOrder lds : Trigger.new){
if(Trigger.oldmap.get(lds.id).Next_Action_Notes__c!= Trigger.newmap.get(lds .id).Next_Action_Notes__c){
ContentDocumentLink clnk = new ContentDocumentLink();
clnk.LinkedEntityId = lds.Id;
clnk.ContentDocumentId = nte[0].Id;
clnk.ShareType = 'I';
lnk.add(clnk);
}
}
if(nte.size()>0){
insert lnk;
}
}
List<ContentVersion> addnotes = new List<ContentVersion>();
for(Lead lds : Trigger.new){
ContentVersion objCntNote = new ContentVersion();
objNote.Title = 'Creation Note';
objNote.PathOnClient = objNote.Title + '.snote';
objNote.VersionData = Blob.valueOf(lds.Sales_Notes__c);
objNote.FirstPublishLocationId = lds.Id; // ParentId
addnotes.add(objNote);
}
if(addnotes.size()>0){
insert addnotes;
}
facing issues when wrote a test class for the below code ! let me know if anyone can resolve the issue
Task Trigger :
trigger cga_trigger_Task on Task (after insert,after update) {
if(Trigger.isAfter){
if(Trigger.isInsert){
cm_ctrl_utilities.createNotes(Trigger.new);
}
List<Task> lstTskDespChange = new List<Task>();
for(Task tsk : Trigger.new){
if(Trigger.isUpdate && (Trigger.oldMap.get(tsk.Id).Description != Trigger.newMap.get(tsk.Id).Description)){
lstTskDespChange.add(tsk);
}
}
if(!lstTskDespChange.isEmpty()){
cm_ctrl_utilities.createNotes(lstTskDespChange);
}
}
}
cm_ctrl_utilities (Apex class):
public without sharing class cm_ctrl_utilities {
public static void createNotes(List<Task> taskLst){
List<ContentNote> cntNteLst = new List<ContentNote>();
List<ContentDocumentLink> cdLnkLst = new List<ContentDocumentLink>();
for(Task tsk : taskLst){
ContentNote cntNte = new ContentNote();
cntNte.Content = Blob.valueof(tsk.Description);
cntNte.Title = tsk.Subject;
cntNteLst.add(cntNte);
}
if(cntNteLst.size()>0){
insert cntNteLst;
}
// to create a note related to case & task, when task is created.
for(Task task : taskLst){
for(ContentNote cntNte : cntNteLst){
ContentDocumentLink clnk = new ContentDocumentLink();
ContentDocumentLink clnk1 = new ContentDocumentLink();
clnk.LinkedEntityId = task.WhatId;
clnk.ContentDocumentId = cntNte.Id;
clnk1.LinkedEntityId = task.Id;
clnk1.ContentDocumentId = cntNte.Id;
cdLnkLst.add(clnk);
cdLnkLst.add(clnk1);
}
}
if(cdLnkLst.size()>0){
insert cdLnkLst;
}
}
}
}
Testclass :
@isTest
public class cm_ctrl_utilities_TEST {
@isTest
static void test1CreateNotes(){
Test.startTest();
Case obj = new Case();
obj.Type = 'Case Management';
obj.Origin = 'Fax';
obj.Subject = 'cm_ctrl_utilities_TEST2';
obj.Description = 'cm_ctrl_utilities_TEST2';
insert obj;
List<Task> lstTask = new List<Task>();
for(Integer i=0;i<200;i++){
Task tsk = new Task();
tsk.Subject = 'testTask1';
tsk.Description = 'cm_ctrl_utilities_TEST';
tsk.WhatId = obj.Id;
lstTask.add(tsk);
}
insert lstTask;
List<ContentDocumentLink> cntNtelst = new List<ContentDocumentLink>();
cntNtelst = [SELECT Id,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId=:obj.Id];
Test.stopTest();
System.assertEquals(cntNtelst.size(), 200, 'Notes not equal to 200');
}
}
}
Not able insert multiple task at a time
Task tsk1 = new Task();
tsk.Subject = 'testTask1';
tsk.Description = 'cm_ctrl_utilities_TEST';
tsk.WhatId = obj.Id;
insert tsk1;
Task tsk2 = new Task();
tsk.Subject = 'testTask2';
tsk.Description = 'cm_ctrl_utilities_TEST';
tsk.WhatId = obj.Id;
insert tsk2;
Task tsk3 = new Task();
tsk.Subject = 'testTask3';
tsk.Description = 'cm_ctrl_utilities_TEST';
tsk.WhatId = obj.Id;
insert tsk3;
If you do need 200 Tasks for testing, you maybe be able to put the entire Task creator in a loop like this: (I havent tried it, not sure if it works)
for(Integer i=0;i<200;i++){
Task tsk = new Task();
tsk.Subject = 'testTask'+i;
tsk.Description = 'cm_ctrl_utilities_TEST';
tsk.WhatId = obj.Id;
insert tsk;
}