I'm writting a trigger that have to create a new note (ContentNote) when a field is filled (toNote__c ). The trigger had to occurs when the record is updated.
Here is the trigger :
trigger CreateNoteLeadTrigger on Lead (after update) {
Set<id> ownerIds = new Set<id>();
for (Lead l : Trigger.new){
ownerIds.add(l.OwnerId);
}
Lead leadToUpdate = [SELECT Id, toNote__c FROM Lead WHERE Id IN :ownerIds];
ContentNote cn = new ContentNote();
String body = leadToUpdate.toNote__c;
cn.Title = 'test ' + leadToUpdate.Name;
cn.Content = Blob.valueOf(body.escapeHTML4());
insert cn;
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cn.Id;
cdl.LinkedEntityId = leadToUpdate.Id;
cdl.ShareType = 'I';
cdl.Visibility = 'InternalUsers';
insert cdl;
}
When I try to update a record I got that error :
System.QueryException: List has no rows for assignment to SObject: Trigger.myTrigger
leadToUpdate
variable is not array. Your query is supposed to return an array.