You need to sign in to do that
Don't have an account?
Jos Vervoorn 2
Trigger on ContentVersion issue
I wrote a fairly simple trigger on the ContentVersion object which ultimately would allow tracking opportunities without a signed contract.
So a document is added or changed and the picklist value 'Signed Contract' is selected. This would flip the 'HasSignedQuote__c' bollean on true on the opportunity.
However I noticed that the LinkedEntityId from the ContentDocumentLink is not always populated and I can't get my finger behind the root cause.
So a document is added or changed and the picklist value 'Signed Contract' is selected. This would flip the 'HasSignedQuote__c' bollean on true on the opportunity.
However I noticed that the LinkedEntityId from the ContentDocumentLink is not always populated and I can't get my finger behind the root cause.
trigger ContentVersion_TRIGGER on ContentVersion (After insert, After update) { Public static Boolean SignedContract = false; System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER]. - TRIGGER '); Set<Id> contentDocumentIdSet = new Set<Id>(); for(ContentVersion cv:trigger.new){ if(cv.ContentDocumentId != null){ contentDocumentIdSet.add(cv.ContentDocumentId); If(cv.File_Type__c == 'Signed Contract'){ //** Based on Content version picklist ... System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER]. - Set SignedContract = true'); SignedContract = true; } } } System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER].contentDocumentIdSet'+contentDocumentIdSet); ContentDocumentLink cdl = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:contentDocumentIdSet Limit 1]; System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER].ContentDocumentLink :'+cdl.LinkedEntityId); List<Opportunity> OppList = [SELECT Id, HasSignedQuote__c FROM Opportunity where Id =:cdl.LinkedEntityId]; System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER].OppList.size() :'+OppList.size()); For (Opportunity Opp:OppList){ If (SignedContract == true){ System.debug(LoggingLevel.Info,'[ContentVersion_TRIGGER]. - Flag opportunity HasSignedQuote to true ...'); Opp.HasSignedQuote__c = true; } } Update OppList; } //** end of Class
if you are dealing with Parent Data you need to write the trigger on ContentDocumnetLink object, this object having LinkedEntityId(ParentId).
When you are inserting the file the order of trigger execution of content documents objects
First ContentVersion object trigger will fire at the ContentDocumentLink not fire means you don't have parentId, next ContentDocument object trigger will fire last ContentDocumentLink object trigger will fire.
I updated your code check it working or not(not tested), modify trigger as per your requirement. Kindly mark this as solved if the reply was helpful.
Thanks
Shaik
All Answers
if you are dealing with Parent Data you need to write the trigger on ContentDocumnetLink object, this object having LinkedEntityId(ParentId).
When you are inserting the file the order of trigger execution of content documents objects
First ContentVersion object trigger will fire at the ContentDocumentLink not fire means you don't have parentId, next ContentDocument object trigger will fire last ContentDocumentLink object trigger will fire.
I updated your code check it working or not(not tested), modify trigger as per your requirement. Kindly mark this as solved if the reply was helpful.
Thanks
Shaik