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

how to coverage for this method in test class
public with sharing class AttachController { 2 3 //private ApexPages.StandardController controller { get; set; } 4 public List<Deal_License__c> dp{get;set;} 5 6 public Deal__c D{get;set;} 7 public AttachController(ApexPages.StandardController controller) { 8 try 9 { 10 D = [Select Name,Id,Account__c,Account__r.Name,Rate__c,License_Value__c,Account__r.BillingStreet,Account__r.BillingCity,Account__r.Billingstate,Account__r.BillingPostalcode,Account__r.BillingCountry 11 from Deal__c where id=:controller.getRecord().id]; 12 system.debug('TestDeal'+D); 13 Account acc=[Select Id,BillingStreet,BillingCity,Billingstate,BillingPostalcode,BillingCountry from Account where id=:D.Account__c]; 14 system.debug('Account%%%%%%%%%%%%%%'+acc); 15 dp=[select id,Name,Deal__c,Months__c,List_Price__c,Unit_Price__c,Quantity__c,Total_Value__c from Deal_License__c where Deal__c=:D.Id]; 16 System.debug('DEALPRDODUCT&&&&&&&&&&'+dp); 17 } 18 catch(Exception e) 19 { 20 system.debug('Exception'+e); 21 } 22 } 23 24 public void attach() { 25 try{ 26 Attachment myAttach = new Attachment(); 27 myAttach.ParentId = ApexPages.currentPage().getParameters().get('id'); 28 System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id')); 29 List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId]; 30 Integer c=at.size()+1; 31 myAttach.name =D.Account__r.Name +''+ 'License Version' + c +'.pdf'; 32 PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id')); 33 myAttach.body = psPdf.getContentAsPdf(); 34 35 insert myAttach; 36 37 38 39 40 System.debug('After attach'); 41 System.debug(myAttach); 42 43 44 /* 45 ApexPages.addMessage(new ApexPages.Message 46 47 (ApexPages.Severity.CONFIRM,'Quotation has been attached.')); 48 */ 49 } 50 catch(Exception e) { 51 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'There was an error while attaching')); 52 System.debug('Exception'); 53 } 54 } 55 56 public PageReference QA() { 57 58 PageReference secondPage = new PageReference('/apex/EmailQuote?id=' + ApexPages.currentPage().getParameters().get('id')); 59 60 Attachment myAttach = new Attachment(); 61 myAttach.ParentId = ApexPages.currentPage().getParameters().get('id'); 62 System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id')); 63 List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId]; 64 Integer c=at.size()+1; 65 myAttach.name = D.Account__r.Name +''+ 'License Version' + c +'.pdf'; 66 PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id')); 67 myAttach.body = psPdf.getContentAsPdf(); 68 69 //insert myAttach; 70 71 secondPage.setRedirect(true); 72 return secondPage; 73 74 } 75 public PageReference back() { 76 attach(); 77 PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target'); 78 pr.setRedirect(true); 79 System.debug('Inside back'); 80 return pr; 81 } 82 public PageReference cancel() { 83 PageReference pr = new PageReference('/' + ApexPages.currentPage().getParameters().get('id') +'#'+ ApexPages.currentPage().getParameters().get('id') + '_RelatedNoteList_target'); 84 pr.setRedirect(true); 85 System.debug('Inside back'); 86 return pr; 87 } 88 89 90 91 /*public String attachmentName{get; set;} 92 public pagereference AttachQuote(){ 93 Pagereference pdf = page.Quotation; 94 blob b=pdf.getcontent(); 95 96 attachment att = new attachment(); 97 att.parentId = ApexPages.currentPage().getParameters().get('id') ; 98 att.body = b; 99 att.name = 'Quotation.pdf'; 100 insert att; 101 return null; 102 103 104 105 }*/ 106 /*test methods here*/ 107 public static testMethod void testAttachController() 108 { 109 Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 110 111 User U = new User(Alias = 'TUser1', Email='testItemaster1@im.com',EmailEncodingKey='UTF-8', 112 LastName='TestingAcc1', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId =p.Id, 113 TimeZoneSidKey='America/Los_Angeles', UserName='testacc1@test.com'); 114 insert U; 115 116 Account acc=new Account 117 118 (Name='rose',BillingCity='bang',Billingstate='kat',BillingPostalcode='560000',BillingCountry='india'); 119 insert acc; 120 system.debug('########555'+ acc); 121 Deal__c opp=new Deal__c(Name='test12',Account__c=acc.id); 122 insert opp; 123 system.debug('########7765'+ opp); 124 125 126 Attachment myAttach1 = new Attachment(); 127 myAttach1.ParentId =opp.id; 128 myAttach1.name = 'Quotation.pdf'; 129 myAttach1.body = blob.valueof('test'); 130 131 insert myAttach1; 132 133 134 system.debug('Attahment1'+ myAttach1); 135 136 AttachController atc = new AttachController(new ApexPages.StandardController(opp)); 137 system.debug('%%%%%'+atc); 138 PageReference pageRef = Page.Quotation; 139 pageRef.getParameters().put('id', String.valueOf(opp.Id)); 140 Test.setCurrentPage(pageRef); 141 Blob b; 142 143 144 AttachController atc1 = new AttachController(new ApexPages.StandardController(myAttach1)); 145 146 atc1.attach(); 147 atc.back(); 148 atc.cancel(); 149 //atc.QA(); 150 151 152 return; 153 /* public PageReference QA() { 154 PageReference secondPage = new PageReference('/apex/EmailQuote?id=' + 155 156 ApexPages.currentPage().getParameters().get('id')); 157 158 insert myAttach1; 159 secondPage.setRedirect(true); 160 return secondPage; 161 }*/ 162 163 //Attachment atchoo = [Select Id, From Attachment where Name like 'Quotation.pdf' 164 165 166 /* if (Test.isRunningTest()) 167 { 168 b=Blob.valueOf('Test Blob String'); 169 } 170 else 171 { 172 b= pageRef.getContent(); 173 }*/ 174 175 } 176 177 }
when i am trying to keep atc.QA();
Error message:Methods defined as TestMethod do not support getContent call, test skipped
can u pls anybody help in solving this
You can put getContentAsPdf() method in try block, then test case will not throw the Exception and the QA() method will also covered. Put the following code at the place of QA() method.
public PageReference QA() {
PageReference secondPage = new PageReference('/apex/EmailQuote?id=' + ApexPages.currentPage().getParameters().get('id'));
Attachment myAttach = new Attachment();
myAttach.ParentId = ApexPages.currentPage().getParameters().get('id');
System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id'));
List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId];
Integer c=at.size()+1;
myAttach.name = D.Account__r.Name +''+ 'License Version' + c +'.pdf';
PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id'));
try{
myAttach.body = psPdf.getContentAsPdf();
}
catch(Exception e){}
//insert myAttach;
secondPage.setRedirect(true);
return secondPage;
}
and also update the test case. Please put the following lines (at the place of code from Line 144 to 149):
ApexPages.currentPage().getParameters().put('id', opp.id);
AttachController atc1 = new AttachController(new ApexPages.StandardController(myAttach1));
atc1.attach();
atc.back();
atc.cancel();
atc.QA();
Thanks,
All Answers
You can put getContentAsPdf() method in try block, then test case will not throw the Exception and the QA() method will also covered. Put the following code at the place of QA() method.
public PageReference QA() {
PageReference secondPage = new PageReference('/apex/EmailQuote?id=' + ApexPages.currentPage().getParameters().get('id'));
Attachment myAttach = new Attachment();
myAttach.ParentId = ApexPages.currentPage().getParameters().get('id');
System.debug('Id: ' + ApexPages.currentPage().getParameters().get('id'));
List<Attachment> at=[Select id from Attachment where parentId=:myAttach.ParentId];
Integer c=at.size()+1;
myAttach.name = D.Account__r.Name +''+ 'License Version' + c +'.pdf';
PageReference psPdf = new PageReference('/apex/Quotation?id=' + ApexPages.currentPage().getParameters().get('id'));
try{
myAttach.body = psPdf.getContentAsPdf();
}
catch(Exception e){}
//insert myAttach;
secondPage.setRedirect(true);
return secondPage;
}
and also update the test case. Please put the following lines (at the place of code from Line 144 to 149):
ApexPages.currentPage().getParameters().put('id', opp.id);
AttachController atc1 = new AttachController(new ApexPages.StandardController(myAttach1));
atc1.attach();
atc.back();
atc.cancel();
atc.QA();
Thanks,
excellent! thank you somuch it is covering upto 97% only "insert myattach line 37" it is not covering
(from line 63 to line 125 it is not covered)
Please only put the getContent() method in try block. Please arrange the lines of code in the following way-
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
// Reference the attachment page and pass in the OpportunityID
PageReference pdf = Page.Quotation;
pdf.getParameters().put('id',(String)opp.id);
pdf.setRedirect(true);
Blob b;
try{
b = pdf.getContent();
}
catch(Exception e){}
Attachment myAttach = new Attachment();
myAttach.ParentId = ApexPages.currentPage().getParameters().get('id');
Remaining code.............
If this answers your query,please mark this as solution so that it would be useful for others. Thanks.
Hi Kamini,
its throwing ann error
Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]
Stack Trace Class.MyAccountListCntrlr.send: line 72, column 1
Class.MyAccountListCntrlr.testMyAccountListCntrlr: line 193, column 1
Please use the follwoing line when you have declared the blob variable:
blob b = blob.valueOf(' ');
Hi Kamini,
Now,its throwing different error
Error Message System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []
Stack Trace Class.MyAccountListCntrlr.send: line 122, column 1
Class.MyAccountListCntrlr.testMyAccountListCntrlr: line 206, column 1
I have used try,catch block before line 122
THANKS FOR ALL UR REPLIES