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
shan razshan raz 

Need Help Writing Test class for Attachment

Hi,
Need Help writing test class for attachments when cloning I tried and only get to 54%... the clone() is what I need help with.. Please see below:
Trigger:
trigger ZCQAttachment on Attachment (after insert) {
    Set<Id> Parents = new Set<Id>();

    List<Attachment> newFiles = new List<Attachment>();


    for (attachment a : Trigger.New) {
        Parents.add(a.parentId);
    }
System.Debug('NewTriggerParentID+++++' + Parents);

    for (EmailMessage e : [SELECT Id, ParentId,Community_Quote__c FROM EmailMessage WHERE Id in :Parents]) { //loop through unique parents
    System.Debug('NewTriggere+++++' + e);
        for (Attachment a : Trigger.New) {
            if (e.Id == a.ParentId) {
                Attachment newFile = a.clone();
                newFile.ParentId = e.Community_Quote__c;
                newFiles.add(newFile);
            }
        }
    }

    Insert newFiles;
    System.Debug('++++++newFiles' +newFiles);
}

Part of TestClass in regards to that part:
Quote quoterec = [Select Case__c, Id, OpportunityId,AccountId,Case_Thread__c,QuoteNumber From Quote where id=:qt];
        //Insert emailmessage for case
        EmailMessage email = new EmailMessage();
            email.FromAddress = 'test@abc.org';
            email.Incoming = True;
            email.ToAddress= 'test@xyz.org';
            email.Subject = 'Test email';
            email.HtmlBody = 'Test email body';
            email.ParentId = quoterec.Case__c; 
        insert email;
        Attachment attach = new Attachment();

        attach.Name='Unit Test Attachment';
            Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
            attach.body=bodyBlob;
            attach.parentId=quoterec.Case__c;
        insert attach;

                   Community_Quote__c ins = new Community_Quote__c ( 
                        Case__c = quoteRec.Case__c, 
                        Quote__c = quoteRec.Id, 
                        Opportunity__c = quoteRec.OpportunityId, 
                        Account__c = quoteRec.AccountId,
                        ThreadID__c = quoteRec.Case_Thread__c,
                        Name = quoteRec.QuoteNumber,
                        Related_Email_Message__c = email.id );
                insert ins;

                EmailMessage emg = [Select Community_Quote__c from EmailMessage Where Id=:email.id];
                    emg.Community_Quote__c=ins.Id;
                update emg;
        
        Attachment newFile = new Attachment();

        newFile.Name='Unit Test Attachment';
            Blob bodyBlob2=Blob.valueOf('Unit Test Attachment Body');
            newFile.body=bodyBlob;
            newFile.parentId=emg.Community_Quote__c;
        insert newFile;

Code Coverage
Please help thank you
Z​​​​​​​
ShirishaShirisha (Salesforce Developers) 
Hi Shan,

Greetings!

As per the code,I can see that the inserting the attachment part is not covering with the above class.

So,I would suggest you to go through the below documents which will give you the sample code to insert the attachment on the respective Object.

https://developer.salesforce.com/forums/?id=9060G000000Bg7BQAS

https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri