1

I know from the error that it is looking for the id of the contentdocument and not the id of the contentversion that got inserted. But I am just confused after inserting the files how do I get the id of the contentdocument to link it to a record. Here is my full code. This code is invoked from a flow when a record is updated.

In the below code the creation of the file works but error out when linking to a record.

public with sharing class GenerateTxt {

    @InvocableMethod(label='Generate txt file' description='Test Description')
    public static void generateTxtFile(List<Requests> recordIds) {
        
        List<ContentVersion> files = new List<ContentVersion>();

        List<String> ids = new List<String>();
            

        for (Requests req: recordIds ) {
            ids.add(req.id);
        }
        
        List<WorkOrder> wos = [SELECT Id, WorkOrderNumber FROM WorkOrder WHERE Id IN :ids WITH SECURITY_ENFORCED];

        for (WorkOrder wo : wos) {
            String fileName = 'SCOOTAROUND_WR_SAR_20231211.txt';
            String fileContent = 'Testing TXT file creation';

            ContentVersion file = new ContentVersion();
            file.Title = fileName;
            file.PathOnClient = fileName;
            file.VersionData = Blob.valueOf(fileContent);
            file.ContentLocation = 'S';
            files.add(file);
        }

        try {
            Database.SaveResult[] insertFiles = Database.insert(files, true);
            List<ContentDocumentLink> contentDocumentLinks = new List<ContentDocumentLink>();
            Integer workOrderNumber = 0;
            for (Database.SaveResult result: insertFiles) {

                ContentDocumentLink cdl = new ContentDocumentLink();

                cdl.ContentDocumentId = result.getId();
                cdl.LinkedEntityId = wos[workOrderNumber].Id;
                cdl.ShareType = 'V';
                cdl.Visibility = 'InternalUsers';
                contentDocumentLinks.add(cdl);
                workOrderNumber++;
            }
            insert contentDocumentLinks;
        } catch(Exception e){
            System.debug('Error--'+e.getMessage());
        }

        }

        public class Requests {
            @InvocableVariable(label='Record Id' description='Id of the record' required=true)
            public String id;
        }
}

1 Answer 1

1

After creating the ContentVersion, you must query those records back to get the ContentDocumentId value.

Database.insert(files, true);
files = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :files ORDER BY Id ASC];

Because the Id values are created in the same order as insertion, this query will preserve the order of the files as they were created in the original list.

If you were to allow partial success, you'd then have to map the workorder to the file, probably something like:

Map<Id, ContentVersion> woToCV = new Map<Id, ContentVersion>();
for (WorkOrder wo : wos) {
            String fileName = 'SCOOTAROUND_WR_SAR_20231211.txt';
            String fileContent = 'Testing TXT file creation';

            ContentVersion file = new ContentVersion();
            file.Title = fileName;
            file.PathOnClient = fileName;
            file.VersionData = Blob.valueOf(fileContent);
            file.ContentLocation = 'S';
            woToCV.put(wo.Id, file);
        }
insert woToCV.values();
Map<Id, ContentVersion> versions = new Map<Id, ContentVersion>([
  SELECT ContentDocumentId FROM ContentVersion WHERE Id = :woToCV.values()
]);
for(WorkOrder wo: wos) {
  ContentVersion cv = versions.get(woToCV.get(wo.Id).Id);
  Id contentDocumentId = cv.ContentDocumentId;
  ...
}
2
  • Thanks @sfdcfox
    – sumchans
    Dec 11, 2023 at 20:49
  • I would also take a look at the FirstPublishLocationId on ContentVersion - if you set that on create to the WorkOrder Id, it will create the CDL record you I think Dec 11, 2023 at 22:55

You must log in to answer this question.

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