You need to sign in to do that
Don't have an account?
Jingyuan Xie
why my test code not coverage 75%
hi everyone, i had an after trigger to help on copy specific attachment from proposal to its parent opportunity, the trigger code is like this
trigger CopyAttachmentsToOpps on Attachment (after insert) {
// collect a set of Proposal 'parent' IDs from the attachments inserted
Set<Id> proposalIds = new Set<Id>();
for(Attachment file : Trigger.new) {
// only collect those that are for the proposal object (others can be ignored)
if(file.ParentId.getSObjectType() == Apttus_Proposal__Proposal__c.getSObjectType() && file.Name.containsIgnoreCase('quote English 20')) {
proposalIds.add(file.ParentId);
}
}
if(!proposalIds.isEmpty()) {
// find the Opportunity to which the Proposal relates
Map<Id,Apttus_Proposal__Proposal__c> proposalMap = new Map<Id,Apttus_Proposal__Proposal__c>([Select Apttus_Proposal__Opportunity__c From Apttus_Proposal__Proposal__c Where Id IN :proposalIds ]);
List<Attachment> attachments = new List<Attachment>();
for(Attachment file : Trigger.new)
{
Attachment newFile = file.clone();
newFile.ParentId = proposalMap.get(file.ParentId).Apttus_Proposal__Opportunity__c;
attachments.add(newFile);
}
// insert the cloned attachments
insert attachments;
}
}
i want to write an test class to deploy it into produciton, here is so far i did ,it only coverage 30% how can i improve that?
@isTest
public class TestCopyAttachmentsToOpps {
static testMethod void testCopyattachs() {
// create quote attachment from scratch
Attachment att = new attachment();
att.body = Blob.valueOf('Unit Test Attachment Body 1');
att.name = 'quote english hahahah';
att.ParentId= 'a5no0000000GxEz';
insert att;
Map<Id,Apttus_Proposal__Proposal__c> proposalMap = new Map<Id,Apttus_Proposal__Proposal__c>([Select Apttus_Proposal__Opportunity__c From Apttus_Proposal__Proposal__c Where Id ='a5no0000000GxEz' ]);
List<Attachment> attachments = new List<Attachment>();
attachment newfile= new attachment();
newFile.ParentId = '006o000000NEmIB';
newfile.body=att.body;
newfile.name=att.name;
insert attachments;
}
}
trigger CopyAttachmentsToOpps on Attachment (after insert) {
// collect a set of Proposal 'parent' IDs from the attachments inserted
Set<Id> proposalIds = new Set<Id>();
for(Attachment file : Trigger.new) {
// only collect those that are for the proposal object (others can be ignored)
if(file.ParentId.getSObjectType() == Apttus_Proposal__Proposal__c.getSObjectType() && file.Name.containsIgnoreCase('quote English 20')) {
proposalIds.add(file.ParentId);
}
}
if(!proposalIds.isEmpty()) {
// find the Opportunity to which the Proposal relates
Map<Id,Apttus_Proposal__Proposal__c> proposalMap = new Map<Id,Apttus_Proposal__Proposal__c>([Select Apttus_Proposal__Opportunity__c From Apttus_Proposal__Proposal__c Where Id IN :proposalIds ]);
List<Attachment> attachments = new List<Attachment>();
for(Attachment file : Trigger.new)
{
Attachment newFile = file.clone();
newFile.ParentId = proposalMap.get(file.ParentId).Apttus_Proposal__Opportunity__c;
attachments.add(newFile);
}
// insert the cloned attachments
insert attachments;
}
}
i want to write an test class to deploy it into produciton, here is so far i did ,it only coverage 30% how can i improve that?
@isTest
public class TestCopyAttachmentsToOpps {
static testMethod void testCopyattachs() {
// create quote attachment from scratch
Attachment att = new attachment();
att.body = Blob.valueOf('Unit Test Attachment Body 1');
att.name = 'quote english hahahah';
att.ParentId= 'a5no0000000GxEz';
insert att;
Map<Id,Apttus_Proposal__Proposal__c> proposalMap = new Map<Id,Apttus_Proposal__Proposal__c>([Select Apttus_Proposal__Opportunity__c From Apttus_Proposal__Proposal__c Where Id ='a5no0000000GxEz' ]);
List<Attachment> attachments = new List<Attachment>();
attachment newfile= new attachment();
newFile.ParentId = '006o000000NEmIB';
newfile.body=att.body;
newfile.name=att.name;
insert attachments;
}
}
- May I request you to please check for Test Class Generator App from APP Exchange.Please refer the below link.
- https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
I hope it will be helpful.Please mark it as best answer if the information is informative.
Best Regards
Rahul Kumar