You need to sign in to do that
Don't have an account?

hello friendsstuck with 40 % cocerage of apex class, need help thanks
Test class:-
@istest
public class AddAttachmentOnQuotes_Test{
Static testMethod Void testMethod2(){
Test.starttest();
Account acc = new Account();
acc.Name = 'Test Account';
acc.Website ='www.test.com';
acc.Type='Banking';
insert acc;
opportunity opp = new opportunity();
opp.name ='Test Opp';
opp.stagename='Closed Won';
opp.Type= 'New Business';
opp.CloseDate=system.today().addmonths(2);
opp.Upsell_Potential__c ='No';
opp.RenewalDate__c = system.today();
opp.accountid=acc.id;
insert opp;
SBQQ__Quote__c oldquote = new SBQQ__Quote__c();
oldquote.SBQQ__Primary__c = true;
oldquote.SBQQ__Opportunity2__c = opp.id;
oldquote.SBQQ__StartDate__c = system.today();
oldquote.SBQQ__EndDate__c= system.today().addmonths(3);
oldquote.SBQQ__SubscriptionTerm__c=1;
oldquote.Status_Of_Approval__c='Pending';
oldquote.Document_Template__c= 'Auto Renew';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
// oldquote.Contract_End_Date__c = system.today().addmonths(3);
insert oldquote;
oldquote.Document_Template__c= 'Auto Renew 3';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
update oldquote;
oldquote.Document_Template__c= 'Basic Template';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
update oldquote;
}
}
Apex handler:-
Public class AddAttachmentOnQuotes {
public static void createAttachment(List<SBQQ__Quote__c> quotes){
List<Attachment> attls = [select id, Name, body from Attachment where name like '%Recurring Payment Agreement Form_2%' limit 1];
List<Attachment> newAttls = new List<Attachment>();
if(attls.size() > 0) {
for(SBQQ__Quote__c q1 :quotes) {
Attachment newFile = attls[0].clone();
newFile.ParentId = q1.id;
newAttls.add(newFile);
}
}
if(newAttls.size() > 0) {
insert newAttls;
}
List<SBQQ__RelatedContent__c> relConls = new List<SBQQ__RelatedContent__c>();
for(Attachment newAtt :newAttls) {
SBQQ__RelatedContent__c relCon = new SBQQ__RelatedContent__c();
relCon.Name = newAtt.Name;
relCon.SBQQ__Quote__c = newAtt.parentId;
relCon.SBQQ__ExternalId__c = newAtt.Id;
relCon.SBQQ__Required__c = true;
relConls.add(relCon);
}
if(relConls.size()>0) {
insert relConls;
}
}
}
Apex trigger :-
trigger QuoteTrigger_PayTerms_GenerateDoc on SBQQ__Quote__c (after insert, after update) {
if(Trigger.Isinsert) {
pt.Documenttemplate(Trigger.new);
AddAttachmentOnQuotes.createAttachment(Trigger.new);
}
// for quote creation...
if(Trigger.IsUpdate) {
List<SBQQ__Quote__c> quotels = new List<SBQQ__Quote__c>();
Set<Id> quoteIds = new Set<Id>();
for(SBQQ__Quote__c qt : trigger.new) {
quoteIds.add(qt.id);
}
set<Id> existingQuoteIds = new Set<Id>();
List<Attachment> attls = [select id, Name,parentId from Attachment where parentId = :quoteIds];
for(Attachment att :attls) {
existingQuoteIds.add(att.id);
}
for(SBQQ__Quote__c qt : trigger.new) {
if((qt.Document_Template__c == 'Auto Renew' && ( qt.SBQQ__PaymentTerms__c=='Monthly' || qt.SBQQ__PaymentTerms__c=='Quarterly'))
||(qt.Document_Template__c=='Auto Renew 3' && ( qt.SBQQ__PaymentTerms__c=='Quarterly' || qt.SBQQ__PaymentTerms__c=='Monthly'))
||(qt.Document_Template__c=='Basic Template' && ( qt.SBQQ__PaymentTerms__c=='Quarterly' || qt.SBQQ__PaymentTerms__c=='Monthly'))
&& qt.Document_Template__c != trigger.oldMap.get(qt.id).Document_Template__c
&& !existingQuoteIds.contains(qt.Id)) {
quotels.add(qt);
}
}
if(quotels.size() > 0) {
AddAttachmentOnQuotes.createAttachment(Trigger.new);
}
}
}
@istest
public class AddAttachmentOnQuotes_Test{
Static testMethod Void testMethod2(){
Test.starttest();
Account acc = new Account();
acc.Name = 'Test Account';
acc.Website ='www.test.com';
acc.Type='Banking';
insert acc;
opportunity opp = new opportunity();
opp.name ='Test Opp';
opp.stagename='Closed Won';
opp.Type= 'New Business';
opp.CloseDate=system.today().addmonths(2);
opp.Upsell_Potential__c ='No';
opp.RenewalDate__c = system.today();
opp.accountid=acc.id;
insert opp;
SBQQ__Quote__c oldquote = new SBQQ__Quote__c();
oldquote.SBQQ__Primary__c = true;
oldquote.SBQQ__Opportunity2__c = opp.id;
oldquote.SBQQ__StartDate__c = system.today();
oldquote.SBQQ__EndDate__c= system.today().addmonths(3);
oldquote.SBQQ__SubscriptionTerm__c=1;
oldquote.Status_Of_Approval__c='Pending';
oldquote.Document_Template__c= 'Auto Renew';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
// oldquote.Contract_End_Date__c = system.today().addmonths(3);
insert oldquote;
oldquote.Document_Template__c= 'Auto Renew 3';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
update oldquote;
oldquote.Document_Template__c= 'Basic Template';
oldquote.SBQQ__PaymentTerms__c= 'monthly';
update oldquote;
}
}
Apex handler:-
Public class AddAttachmentOnQuotes {
public static void createAttachment(List<SBQQ__Quote__c> quotes){
List<Attachment> attls = [select id, Name, body from Attachment where name like '%Recurring Payment Agreement Form_2%' limit 1];
List<Attachment> newAttls = new List<Attachment>();
if(attls.size() > 0) {
for(SBQQ__Quote__c q1 :quotes) {
Attachment newFile = attls[0].clone();
newFile.ParentId = q1.id;
newAttls.add(newFile);
}
}
if(newAttls.size() > 0) {
insert newAttls;
}
List<SBQQ__RelatedContent__c> relConls = new List<SBQQ__RelatedContent__c>();
for(Attachment newAtt :newAttls) {
SBQQ__RelatedContent__c relCon = new SBQQ__RelatedContent__c();
relCon.Name = newAtt.Name;
relCon.SBQQ__Quote__c = newAtt.parentId;
relCon.SBQQ__ExternalId__c = newAtt.Id;
relCon.SBQQ__Required__c = true;
relConls.add(relCon);
}
if(relConls.size()>0) {
insert relConls;
}
}
}
Apex trigger :-
trigger QuoteTrigger_PayTerms_GenerateDoc on SBQQ__Quote__c (after insert, after update) {
if(Trigger.Isinsert) {
pt.Documenttemplate(Trigger.new);
AddAttachmentOnQuotes.createAttachment(Trigger.new);
}
// for quote creation...
if(Trigger.IsUpdate) {
List<SBQQ__Quote__c> quotels = new List<SBQQ__Quote__c>();
Set<Id> quoteIds = new Set<Id>();
for(SBQQ__Quote__c qt : trigger.new) {
quoteIds.add(qt.id);
}
set<Id> existingQuoteIds = new Set<Id>();
List<Attachment> attls = [select id, Name,parentId from Attachment where parentId = :quoteIds];
for(Attachment att :attls) {
existingQuoteIds.add(att.id);
}
for(SBQQ__Quote__c qt : trigger.new) {
if((qt.Document_Template__c == 'Auto Renew' && ( qt.SBQQ__PaymentTerms__c=='Monthly' || qt.SBQQ__PaymentTerms__c=='Quarterly'))
||(qt.Document_Template__c=='Auto Renew 3' && ( qt.SBQQ__PaymentTerms__c=='Quarterly' || qt.SBQQ__PaymentTerms__c=='Monthly'))
||(qt.Document_Template__c=='Basic Template' && ( qt.SBQQ__PaymentTerms__c=='Quarterly' || qt.SBQQ__PaymentTerms__c=='Monthly'))
&& qt.Document_Template__c != trigger.oldMap.get(qt.id).Document_Template__c
&& !existingQuoteIds.contains(qt.Id)) {
quotels.add(qt);
}
}
if(quotels.size() > 0) {
AddAttachmentOnQuotes.createAttachment(Trigger.new);
}
}
}
- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)
----------------Test Class-------------------
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.