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
Chandu007Chandu007 

Not able to get 100% code coverage for this trigger

Trigger:- 

 trigger CreateAttachment on Attachment (after insert, after update) {
        for(integer i =0; i<= Trigger.new.size();i++){
            try{
                ID refid = id.valueof(trigger.new[i].parentid);
                system.debug('xxxxxxxxxxxxxxxxxxxxxxxxx' + refid);
                Id Opportunity15 = string.valueof(trigger.new[i].parentid).substring(0,15);
                if (string.valueof(trigger.new[i].parentid).substring(0,3) == '006'){
                ID contractid = [SELECT id FROM CMC_Contract__c where OpportunityId__c =:  string.valueof(trigger.new[i].parentid).substring(0,15)].id;        
                    if(contractid != null){
                        Attachment ConAttachment = New Attachment(
                        Body = trigger.new[i].Body,
                        IsPrivate = trigger.new[i].IsPrivate,
                        ContentType = trigger.new[i].ContentType,
                        OwnerId = trigger.new[i].OwnerId,
                        Name = trigger.new[i].name,
                        parentid = contractid);
                        insert ConAttachment;

                    }
                }    
        
            } 
            catch(exception ex){
                system.debug('Exception' + ex);
            }
        }
    }

Test Class:- 

@isTest
public class CreateAttachmentTest {

    public static testMethod void Attachmentcreatemethod(){
        
        Test.startTest();
        Account act = new Account(Name='Test Acc');
        insert act;
        MHC2__Project__c proj = new MHC2__Project__c(Name='Test Project3');
        insert proj;
        List<CMC_Contract__c> conList = new List<CMC_Contract__c>();
        CMC_Contract__c c1 = new CMC_Contract__c();
        conList.add(c1);
        insert conList;       
        
        
        Opportunity op = new Opportunity(Name='My Test Opportunity5');
        op.Bid_Date_Time__c = System.Now().addDays(60);
        op.CloseDate = System.Now().addDays(30).date();
        op.StageName = 'Bidding';
        op.AccountId = act.Id;
        op.Bid_Type__c = 'Lump Sum';
        op.Project__c = proj.Id;
        insert op;
        
        Attachment attach=new Attachment();
        attach.Name='Unit Test Attachmnt';
        Blob bodyBlob1=Blob.valueOf('Unit Test Attachment Body1');
        attach.body=bodyBlob1;
        attach.parentId=op.id;
        insert attach;
        
        Test.stopTest();
        
    }
}

Lines highlighted in bold are not covered. i think i am missing to add some oppty id but not sure where it went wrong. Please comment your suggestions


Thanks,
Chandu
Best Answer chosen by Chandu007
Alex Bondarenko 1Alex Bondarenko 1
Hello, have you checked if  "if(contractid != null)" condition is executed during the test run? 
I think it always equals to NULL because you are not make a relation between CMC_Contact__c and OpportunityId__c in your test method

All Answers

Alex Bondarenko 1Alex Bondarenko 1
Hello, have you checked if  "if(contractid != null)" condition is executed during the test run? 
I think it always equals to NULL because you are not make a relation between CMC_Contact__c and OpportunityId__c in your test method
This was selected as the best answer
Chandu007Chandu007
Yes. i missed it but i am not understanding output of string.valueof(trigger.new[i].parentid).substring(0,15)].id in the query
Chandu007Chandu007
i have created relation by adding one line c1.Opportunity_Contract__c = op.id;  and it solved the issue. Thanks Alex.

Thanks,
Chandra
Alex Bondarenko 1Alex Bondarenko 1

Can you post here what the values are stored in the OpportunityId__c fields from CMC_Contract__c object

just show a few recods from [SELECT id, OpportunityId__c FROM CMC_Contract__c]