function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Racheal Dales 20Racheal Dales 20 

Trigger & Test Class Help Needed

I am not familiar on writing code, could anyone help me on by looking over my Trigger & Test Class?

I'm trying to create a Trigger to check a checkbox (the checkbox field name is Has_Attachment__c) on the Case object ( (specifically the Legal Help Desk record type = 0122M000001QR8xQAG)  every time an attachment is added, then I will use this to build a workflow to send an email to corresponding users every time the checkbox is true.

Within the Trigger where do I specify the Case Record Type?
trigger LegalCaseFileAttached on Attachment (before insert, before delete) 
{
if(trigger.isinsert){
List<Case> co = [select id from Case where id =: Trigger.New[0].ParentId];
If(co.size()>0)        
{            
co[0].Has_New_Case_Comment__c = True;            
update co;        
}
}


if(trigger.isdelete){

List<Case> co = [select id from Case where id =: Trigger.old[0].ParentId];        
If(co.size()>0)        
{            
co[0].Has_New_Case_Comment__c = false;            
update co;        
}
}
}
Also, I am stuck on trying to write the Test Class. I would greatly appreciate any help with this!
@isTest
public class LegalCaseFileAttachedTEST {
{
    static testMethod void attTriggerTest1()
    {
        test.startTest();      
        Case = new Case(Subject = 'Test Case', Status__c ='New');
        insert Case;   
        Attachment att = new Attachment(Name='textAtt', ParentId = Case.Id, body = Blob.valueOf('Some Text'), Description='TestAttachment');
        insert att;
        delete att;        
        test.stopTest();
    }
}
}
Also, I need to make sure this will work in Lightning.  Thanks so very much in advance for any help you can provide.
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Racheal,

Can you try checking the below code once:

trigger AttachmentonCDL on ContentDocumentLink (after insert) 

    String tempParentId;
    Set<Id> setParentId = new Set<Id>();
    List<Case> Attachmentlst = new List<Case>();
    
 for (ContentDocumentLink cdl : trigger.new ) {
            tempParentId = cdl.ParentId;
     
            if (tempParentId.left(3) =='a0X') {
                System.debug('Debug : found a0X');
                System.debug('Debug : content document id ' + cdl.ContentDocumentId );
                setParentId.add(cdl.ParentId);
            }
        }
    Attachmentlst = [select Id , Has_New_Case_Comment__c  from Case where Id IN :setParentId];
     
     For(Case e : Attachmentlst)
     {
        e.Has_New_Case_Comment__c  = True;
     }

     update Attachmentlst;
}

I used the code from this link [ https://developer.salesforce.com/forums/?id=9062I000000IEtOQAW ] that has a similar implementation to the one you are trying to implement but on different object.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Racheal Dales 20Racheal Dales 20
Anutej,

Thank you for the help but I'm receiving an error on both Line 8 & Line 13 - Variable does not exist: ParentID.  Should I be referencing the Case ID there? Also within the Trigger where do I specify the Case Record Type?  One more question - would you know how to write the Test Class for this Trigger, I am stuck on that piece and can't seem to move forward.

Thanks again!