0

On Lead conversion I also create a new object to which I map several fields of the lead trough processbuilder. I also want to pass the files from the lead to the new custom object. I am trying to do a query on lead and pass this to the new record Id but I am stuck on linking the content document id which I want to use as a trigger.

See:

List<ContentDocumentLink> ContDocLinksLead = [SELECT ContentDocumentId  FROM ContentDocumentLink WHERE  LinkedEntityId = '00Q1X000001BxVmUAK'];
system.debug('ContDocLinksLead: '+ContDocLinksLead);

ContentDocumentLink LinkList = new ContentDocumentLink();
for(ContentDocumentLink Link: ContDocLinksLead){
    Link.ContentDocumentId = ContDocLinksLead.ContentDocumentId;
    Link.LinkedEntityId = 'a001X000000k7oSQAQ';
    link.ShareType = 'V';
    link.Visibility = 'AllUsers';
    LinkList.add(Link);
}
Insert LinkList;
  • There are a number of syntax errors here because you've used the wrong variable name in several places. What are you asking for help with? The core principle looks valid to me. – David Reed Feb 1 at 13:38
  • @DavidReed I want to pass the lead files on conversion to a newly created object. I am trying to grasp how the ContentDocumentLink works to do this as a trigger. At the moment if I convert the lead I lose my files on the new object record. – Thomas Feb 1 at 14:07
1

Your basic principle here is valid, but you've got a number of mistakes in your code that aren't related to ContentDocument management as such. I'll try to help here by annotating your code, rather than rewriting it.

// This is good - to query ContentDocumentLink, you must filter on
// LinkedEntityId or ContentDocumentId. In real use, you'd be querying
// against a Set<Id>, I would assume.
List<ContentDocumentLink> ContDocLinksLead = [
    SELECT ContentDocumentId  
    FROM ContentDocumentLink 
    WHERE LinkedEntityId = '00Q1X000001BxVmUAK'
];   
// The type of this variable is wrong. You want List<ContentDocumentLink>.
ContentDocumentLink LinkList = new ContentDocumentLink();

for(ContentDocumentLink Link: ContDocLinksLead){
    // Here you need to be creating a *new* ContentDocumentLink.
    // ContentDocumentLink newLink = new ContentDocumentLink();
    //
    // ContDocLinksLead is your list variable. You need to be
    // looking at `Link` here.
    Link.ContentDocumentId = ContDocLinksLead.ContentDocumentId;

    // In real code, you'd be mapping between the Id of your original Lead
    // and the Id of the custom object here, to get the new LinkedEntityId.
    Link.LinkedEntityId = 'a001X000000k7oSQAQ';
    // You'll want ShareType 'I' for Files attached to records.
    // This infers access level from the parent record.
    // See https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm
    link.ShareType = 'V';
    link.Visibility = 'AllUsers';
    LinkList.add(Link);
}
Insert LinkList;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.