0

I have a requirement that users should not be able to remove document from the closed cases. I have achieved this by writing a trigger on before delete on contentdocumentlink.

My old code was like as below (just the logic of handler class I am pasting):

public void restrictDeleteAttachment(set<id>parentidset,list<ContentDocumentLink> oldcontents){

    ClosedCasesList1=[select id,status from case where status=:label.ClosedCaseStatus];
    for(case cas: ClosedCasesList1){
        closedcasesset1.add(cas.id);
    }
    for(ContentDocumentLink cdl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId in: closedcasesset1]){
       contentdoclinkcd.add(cdl.ContentDocumentId);
    }
    for(ContentDocumentLink cv:oldcontents){
        if(contentdoclinkcd.contains(cv.ContentDocumentId)){
           cv.adderror(system.label.CCR_RemoveFile);
        }
    }
}

This one I know surely it's not properly bulkified, but it just runs perfectly in one sandbox1 and addError was firing. When I deployed the same code to other sandbox2 and try to remove document for closed case, the message simply didn't fire. My trigger was active and code was just the same.

I thought may be my code is not much streamlined, so I just modified the code as below:

public void restrictDeleteAttachment(set<id>parentidset,list<ContentDocumentLink> oldcontents){

    for(ContentDocumentLink cdl : [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId in ( SELECT Id FROM Case ) and LinkedEntity.Type=:label.Case]){
        linkedid.add(cdl.LinkedEntityId);
        contentdoclinkcd.add(cdl.ContentDocumentId);
        MapOfCdNCdl.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
    }
    
    caselist=[select id, status from case where id in:linkedid];
    for(case cass:caselist){
        MapOfCaseStatus.put(cass.Id, cass.status);
    }
    
    for(ContentDocumentLink cv:oldcontents){
        Id cdlId = MapOfCdNCdl.get(cv.ContentDocumentId);
        if(MapOfCaseStatus.get(cdlId) == system.label.CaseStatus){
            cv.adderror(system.label.CCR_RemoveFile);
        }
    }
}

I use maps. Now in sandbox1 it's working fine and addError message is firing. The new code change is not yet implemented in sandbox2.

Can someone please help me to know why for my first code, it's working in sandbox1 and not in sandbox2?
Also if someone can guide me whether my changed new code is fine?

1 Answer 1

0

I have optimized your code, it should work. One thing I'm sure is you are querying contentDocumentLink for all cases, which you should not do.

public void restrictDeleteAttachment(set<id>parentidset,list<ContentDocumentLink> oldcontents){
    
    List<Id> caseIdList = new List<Id>();
    for(ContentDocumentLink a :oldcontents){
            string cdlentityId = a.LinkedEntityId;
            if (cdlentityId.substring(0,3) == '500') {
                caseIdList.add(a.LinkedEntityId);
            }
    }
    
    Map<Id,string> caseStatusPerIdMap = new Map<Id,string>();
    for(Case cs : [Select Id, Status FROM Case Where Id IN: caseIdList AND Status = 'Closed']){
        caseStatusPerIdMap.put(cs.Id,cs.Status);
    }
    
    for(ContentDocumentLink a :oldcontents){
        string cdlentityId = a.LinkedEntityId;
        if(cdlentityId.substring(0,3) == '500' && caseStatusPerIdMap.containsKey(cdlentityId)) {
             a.addError(system.label.CCR_RemoveFile);
        }
    }
}

You must log in to answer this question.

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