You need to sign in to do that
Don't have an account?
This is my code,and it's mot working can anyone help me
trigger CountNoOfFiles on ContentVersion (after insert,before update,after delete,after undelete) {
Set<Id> contentDocumentIdSet = new Set<Id>();
list<Contract> ConToUpdate = new list<Contract>();
set<id> LinkedEntityId1=new set<id>();
if(Trigger.isInsert || Trigger.isUpdate|| Trigger.isUndelete){
for(ContentVersion cv:trigger.new){
if(cv.ContentDocumentId != null){
contentDocumentIdSet.add(cv.ContentDocumentId);
}
}
}
if(Trigger.isDelete){
for(ContentVersion cv:trigger.old){
if(cv.ContentDocumentId != null){
contentDocumentIdSet.add(cv.ContentDocumentId);
}
}
}
system.debug('===contentDocumentIdSet'+contentDocumentIdSet);
List<ContentDocumentLink> cdlForEvent= [SELECT ContentDocumentID, LinkedEntityId FROM ContentDocumentLink
WHERE ContentDocumentID IN:contentDocumentIdSet];
if(cdlForEvent!=null && cdlForEvent.size()>0){
for(ContentDocumentLink contlink:cdlForEvent){
String qtId = String.valueOf(contlink.LinkedEntityId);
qtId = qtId.substring(0,3);
if(qtId=='800'){
LinkedEntityId1.add(contlink.LinkedEntityId);
}
}
system.debug('===LinkedEntityId1'+LinkedEntityId1);
if(LinkedEntityId1!=null && LinkedEntityId1.size()>0){
for(Contract con : [SELECT Id,No_of_Files__c FROM Contract WHERE ID in:LinkedEntityId1]){
con.No_of_Files__c+=LinkedEntityId1.size();
ConToUpdate.add(con);
}
}
}
if(ConToUpdate!=NULL && ConToUpdate.size()>0)
system.debug('===ConToUpdate'+ConToUpdate);
update ConToUpdate;
}
Set<Id> contentDocumentIdSet = new Set<Id>();
list<Contract> ConToUpdate = new list<Contract>();
set<id> LinkedEntityId1=new set<id>();
if(Trigger.isInsert || Trigger.isUpdate|| Trigger.isUndelete){
for(ContentVersion cv:trigger.new){
if(cv.ContentDocumentId != null){
contentDocumentIdSet.add(cv.ContentDocumentId);
}
}
}
if(Trigger.isDelete){
for(ContentVersion cv:trigger.old){
if(cv.ContentDocumentId != null){
contentDocumentIdSet.add(cv.ContentDocumentId);
}
}
}
system.debug('===contentDocumentIdSet'+contentDocumentIdSet);
List<ContentDocumentLink> cdlForEvent= [SELECT ContentDocumentID, LinkedEntityId FROM ContentDocumentLink
WHERE ContentDocumentID IN:contentDocumentIdSet];
if(cdlForEvent!=null && cdlForEvent.size()>0){
for(ContentDocumentLink contlink:cdlForEvent){
String qtId = String.valueOf(contlink.LinkedEntityId);
qtId = qtId.substring(0,3);
if(qtId=='800'){
LinkedEntityId1.add(contlink.LinkedEntityId);
}
}
system.debug('===LinkedEntityId1'+LinkedEntityId1);
if(LinkedEntityId1!=null && LinkedEntityId1.size()>0){
for(Contract con : [SELECT Id,No_of_Files__c FROM Contract WHERE ID in:LinkedEntityId1]){
con.No_of_Files__c+=LinkedEntityId1.size();
ConToUpdate.add(con);
}
}
}
if(ConToUpdate!=NULL && ConToUpdate.size()>0)
system.debug('===ConToUpdate'+ConToUpdate);
update ConToUpdate;
}