You need to sign in to do that
Don't have an account?
Chandu007
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
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
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
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
Thanks,
Chandra
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]