I have a solution for my particular problem, Quote PDFs for Quotes with names containing a /
I have done this with a trigger:
trigger ContentDocumentLinkTrigger on ContentDocumentLink(after insert) {
ContentDocumentLinkTriggerHandler.ProcessOnInsert(Trigger.new);
}
and of course its handler:
public with sharing class ContentDocumentLinkTriggerHandler {
public static void ProcessOnInsert(List<ContentDocumentLink> newList) {
Set<Id> quoteIds = new Set<Id>();
Map<Id, Id> mapQuoteIdToCDId = new Map<Id, Id>();
for (ContentDocumentLink cdl : newList) {
String lEId = cdl.LinkedEntityId;
String objectPrefix = lEId == null ? '' : lEId.left(3);
if (objectPrefix == '0Q0') {
quoteIds.add(lEId);
mapQuoteIdToCDId.put(lEId, cdl.ContentDocumentId);
}
}
Map<Id, Quote> mapQuotes = new Map<Id, Quote>([SELECT Id, Name FROM Quote WHERE Id IN :quoteIds]);
Set<Id> contDocIds = new Set<Id>();
Map<Id, String> cdIdToQuoteName = new Map<Id, String>();
for (Id quoteId : quoteIds) {
if (mapQuotes.containsKey(quoteId)) {
Quote q = mapQuotes.get(quoteId);
if (q.Name.contains('/')) {
Id contentDocumentId = mapQuoteIdToCDId.get(quoteId);
contDocIds.add(contentDocumentId);
cdIdToQuoteName.put(contentDocumentId, q.Name);
}
}
}
// Modify document/file names if necessary.
Map<Id, ContentDocument> mapContentDocuments = new Map<Id, ContentDocument>([SELECT Id, Title, FileExtension FROM ContentDocument WHERE Id IN :contDocIds]);
List<ContentDocument> cdsToUpdate = new List<ContentDocument>();
for (ContentDocumentLink cdl : newList) {
if (mapContentDocuments.containsKey(cdl.ContentDocumentId)) {
ContentDocument objCntDoc = mapContentDocuments.get(cdl.ContentDocumentId);
// Apply full Quote name with / replaced by -.
// objCntDoc.Title contains part of the Quote name + a version.
// For example Quote "abc/offerte" will have a file with Title "offerte_V1".
Integer startOfVersionInfo = objCntDoc.Title.lastIndexOf('_V');
String versionInfo = startOfVersionInfo >= 0 ? objCntDoc.Title.substring(startOfVersionInfo) : '';
String strFilename = cdIdToQuoteName.get(cdl.ContentDocumentId).replace('/', '-') + versionInfo + '.' + objCntDoc.FileExtension;
objCntDoc.Title = strFilename;
cdsToUpdate.add(objCntDoc);
}
}
if (cdsToUpdate.size() > 0) {
update cdsToUpdate;
}
}
}
I wish it could be done in a simpler way, but this works for me.
Kudos to tushar who came up with the basis for this solution here.