function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ryan GreeneRyan Greene 

Populating list from Set Id in Trigger

Hello,
I thought I had a simple straight forward code......never the case for me! Basically what I'm looking to do is if the ContentNote is updated or created, then push that note to a field in the Lead. Since ContentNote uses ContentDocumentLink as a sort of junction the Trigger is based of the ContentDocumentLink. Also, a trigger is not possible from the ContentNote itself (at least I couldn't find it). My code fails on line 9 showing error in the log of "FATAL_ERROR System.QueryException: List has no rows for assignment to SObject". Even though the log also shows the SOQL pulls 1 record. Any advice on getting this to work properly?
Trigger:
trigger Note on ContentDocumentLink (after insert, after update){
	Set<Id> cnote = new Set<Id>();
    Set<Id> leed = new Set<Id>();
    for(ContentDocumentLink cl : Trigger.new){
        cnote.add(cl.ContentDocumentId);
        leed.add(cl.LinkedEntityId);
    }
    ContentNote cn = [SELECT Id,Content FROM ContentNote WHERE Id IN :cnote];
    Lead ld = [SELECT Id,Last_Note__c FROM LEAD WHERE Id IN :leed LIMIT 1];
    for(ContentDocumentLink clnk : Trigger.new){
        ld.Last_Note__c = cn.Content.toString();
        update ld;
    }
}

 
Best Answer chosen by Ryan Greene
Dev_AryaDev_Arya
Instead of  getting result in to Lead ld,  you could  try using List<Lead> ld. And then change the for loop accordingly. :-)