You need to sign in to do that
Don't have an account?
George Laird 55
Trigger to add task when note (contentNote) is inserted
Hello. I'm trying to write a trigger that will insert a complted task whenever a Note is inserted. This is using the newer notes feature, not the old school one. The problem I'm having is that the new task is not being related to the same record that the note is. The note has a "related to" field when you are typing the note. I think that that field is actually "LinkedEntityId" field. The problem is that when the trigger runs, that field is referencing a user. I was told that the trigger may run multiple times and after you actually save the note, "LinkedEntityId" becomes the related record, but I'm not seeing that happen. Here is my trigger:
I've also attached a screen shot of the code for better readability.
trigger NewNoteCreatesTaskTrigger on ContentVersion (after insert) {
List<Task> tasks = new List<Task>();
string userId = [SELECT Id FROM User WHERE Id = : UserInfo.getUserId()][0].Id;
ContentDocumentLink rr;
Id cId;
for(ContentVersion n : Trigger.New){
if(n.FileType == 'SNOTE'){
cId = n.ContentDocumentId;
}
}
List<ContentDocumentLink> cCont = [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId, ShareType, Visibility FROM ContentDocumentLink where ContentDocumentId =: cId];
system.debug(cCont);
for(ContentDocumentLink z : cCont){
String myIdPrefix = String.valueOf(z.LinkedEntityId).substring(0,3);
if(myIdPrefix != '005'){
rr = z;
}
}
if(rr != null){
task t = new task();
t.WhatId = rr.LinkedEntityId;
t.ActivityDate = system.today();
t.OwnerId = userId;
t.Priority = 'Low';
t.Status = 'Completed';
t.Subject = 'New Note Created';
try {
insert t;
} catch (DmlException e) {
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'george.laird@amtrustgroup.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemailgoeshere');
mail.setSenderDisplayName('Apex error message');
mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
mail.setPlainTextBody(e.getMessage());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
I've also attached a screen shot of the code for better readability.
trigger NewNoteCreatesTaskTrigger on ContentVersion (after insert) {
List<Task> tasks = new List<Task>();
string userId = [SELECT Id FROM User WHERE Id = : UserInfo.getUserId()][0].Id;
ContentDocumentLink rr;
Id cId;
for(ContentVersion n : Trigger.New){
if(n.FileType == 'SNOTE'){
cId = n.ContentDocumentId;
}
}
List<ContentDocumentLink> cCont = [SELECT Id, ContentDocumentId, ContentDocument.Title, ContentDocument.FileType, LinkedEntityId, ShareType, Visibility FROM ContentDocumentLink where ContentDocumentId =: cId];
system.debug(cCont);
for(ContentDocumentLink z : cCont){
String myIdPrefix = String.valueOf(z.LinkedEntityId).substring(0,3);
if(myIdPrefix != '005'){
rr = z;
}
}
if(rr != null){
task t = new task();
t.WhatId = rr.LinkedEntityId;
t.ActivityDate = system.today();
t.OwnerId = userId;
t.Priority = 'Low';
t.Status = 'Completed';
t.Subject = 'New Note Created';
try {
insert t;
} catch (DmlException e) {
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'george.laird@amtrustgroup.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('myemailgoeshere');
mail.setSenderDisplayName('Apex error message');
mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
mail.setPlainTextBody(e.getMessage());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}