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
Srinivas223Srinivas223 

carrying files to another record

I am using a custom contract object which have the option to amend. The new amendment will carry all the contract info but not carrying its files attached to it. How to carry over the files when we are amending existing contract(amending a contract means it creates a new contract with the old contract info).

I am trying this 
public static void carryFilestoAmendments(list<Contract__c> contracts){
            list<id> amended = new list<id>();
            for(Contract__c con : contracts){
                amended.add(con.REVVY__AmendedFrom__c);
                }
           list<ContentDocumentLink> files = [SELECT ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where 
                                           LinkedEntityId in: amended and LinkedEntity.Type='MnContract__c'];
            for(Contract__c con : contracts)
            for(ContentDocumentLink doc : files){
                doc.LinkedEntityId = con.id;
                }
        }

The error I got on this is
Field is not writable. ContentDocumentLink.LinkedEntityId
I appreciate your help.
Thank you.
 
Best Answer chosen by Srinivas223
Srinivas223Srinivas223
Here is the solution I found. 
As the linkedentity of the existing is not writable, clone the contentdocument link and attach it to the record with linkedEntityId.

Thanks!
for(ContentDocumentLink file: files){
  ContentDocumentLink newclnk = file.clone();
  newclnk.linkedentityid = NewContractId[0]; //This ID is the New Contract Id.
                    insertLinks.add(newclnk);
                    deleteLinks.add(file);
           
                    }
                  }
               
 insert insertLinks; //Insert the new ContentDocumentLink with new Contract.
  Delete deleteLinks; //Delete the Old ContentDocumentLink with old Contract.

All Answers

bhanu_prakashbhanu_prakash
Hi Srinvas,

ContentDocumentLink.LinkedEntityId is autofield, you can't able to update values on that field . So your are getting error as " Field is not writable "  
doc.LinkedEntityId = con.id;  
you can't perform operation on LinkedEntityId .
let us know if it helps you and mark it best if it helps you

Thanks, 
Bhanu Prakash

visit ForceLearn (https://www.forcelearn.com)for salesforce Latest Updates and development tips
Srinivas223Srinivas223
Hello Bhanu,

Thank you for your time.
Do we have anyway to carry files from one record to its amendment?
Srinivas223Srinivas223
Here is the solution I found. 
As the linkedentity of the existing is not writable, clone the contentdocument link and attach it to the record with linkedEntityId.

Thanks!
for(ContentDocumentLink file: files){
  ContentDocumentLink newclnk = file.clone();
  newclnk.linkedentityid = NewContractId[0]; //This ID is the New Contract Id.
                    insertLinks.add(newclnk);
                    deleteLinks.add(file);
           
                    }
                  }
               
 insert insertLinks; //Insert the new ContentDocumentLink with new Contract.
  Delete deleteLinks; //Delete the Old ContentDocumentLink with old Contract.
This was selected as the best answer