0

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

1
  • There's a problem with your lead query. You're using a list of Ids and an IN clause but you your leadToUpdate variable is not array. Your query is supposed to return an array. Commented Apr 3, 2017 at 12:15

2 Answers 2

3

Well you have two problems here:

  • You're trying to assign to a single Lead record.
    • Right now, you're getting an error because the query returns zero records.
    • If you fix just the query, you will likely get multiple records, also an error.
  • You're trying to query where a Lead record has an Id matching OwnerId on another record. That will never happen.

So at the very least you should switch to a List<Lead>. It's hard to tell what you're trying to accomplish, but it seems like you may be looking for records owned by the same user, which would then change your query code to:

List<Lead> leadsToUpdate = [SELECT Id FROM Lead WHERE OwnerId IN :ownerIds];

This change won't likely make the code do what you laid out in the spec, but you didn't share that with us, and it solves the problem at hand.

3
  • Ok thank you for the precision, but with a list the rest of my code isn't working . I'm new to triggers and I don't see how I can make it works. I had put all my code in my question
    – dou
    Commented Apr 3, 2017 at 12:22
  • @dou All of your code but none of your requirement.
    – Adrian Larson
    Commented Apr 3, 2017 at 12:23
  • Yes I just had to create a new ContentNote filled with the content of the field toNote__c and so I have to write a trigger after update for that. The note is linked to a record
    – dou
    Commented Apr 3, 2017 at 12:26
1

In your query you have the wrong where clause and that's why there is no result. Do you mean: OwnerId instead of Id?

Lead leadToUpdate = [SELECT Id, toNote__c FROM Lead WHERE OwnerId IN :ownerIds];

your query is expected to return list and not a single record, that's why Apex threw an Exception if there is no result. Try using List<Lead> in the result:

List<Lead> leadToUpdate = [SELECT Id, toNote__c FROM Lead WHERE OwnerId IN :ownerIds];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .