0

I have a legacy code which is uploading document as attachment. I am trying to change them to ContentVersion.

for (case c : ARCases) {
// Do something
Blob pdf = Blob.toPdf(emailbody);
//create content version record
.
.
//Add to contentversion List to do an insert after loop
pdfemails.add(cver);
}

Question is how do we associate the contentversion to the correct case? Would it be safe to assume the contentversion list would be in the same order of how they were generated? I am pretty sure these would be pretty common use case and what would be the best way associate the parent record with the contentversion when dealing with multiple files and multiple parent records

1 Answer 1

2

Yes, the List object is an ordered list. Therefore, the relationship between your case and the PDF is 1:1. You'll never get a situation where the order changes arbitrarily.

If you had an "optional" update, then you'd want to use a map:

Map<Id, Blob> pdfs = new Map<Id, Blob>();
for(Case record: cases) {
  if(shouldDoPdf(record)) {
    pdfs.put(record.Id, Blob.toPdf(emailBody));
  }
}
insert pdfs.values();
for(Case record: cases) {
  if(pdfs.containsKey(record.Id)) {
    // You can associate the pdfs to the cases here
  }
}

You must log in to answer this question.

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