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
Vadivel MuruganVadivel Murugan 

Test Class for Blob

public PageReference sendEmail(){

//Creating the Contact
        Contact tempContact  = new Contact();
            tempContact.LastName ='test';
            tempContact.firstName = 'testmerfantz';
            tempContact.email = 'noreply@salesforce.com';
            tempContact.Account = [select id, name from Account limit 1];
        Insert tempContact;
        
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
emailTo=emailadd;
mail.setToAddresses(new String[] {emailTo});

PageReference ref = Page.PricelistFAEL;
Blob b = ref.getContentAsPDF();
Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
efa1.setFileName('Uplift Request.pdf');
efa1.setBody(b);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});

mail.setTemplateId([select id, name from EmailTemplate where Name='Calculate Amount'].id);
    mail.setTargetObjectId(tempContact.id);
    mail.setSaveAsActivity(false);
    mail.setWhatId(apexpages.currentpage().getparameters().get('id'));
    
try{
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(resultMail[0].isSuccess()) {     
response = 'Your Mail Successfully sent!';
flag=false;
}
else{
response = resultMail[0].getErrors().get(0).getMessage();
}
}catch(System.EmailException ex){
response = ex.getMessage();
}  

// Delete the tempContact
       Delete tempContact; 
       
return null;
}

public PageReference SaveasPdf(){
system.debug('CurrentId>>>'+currentid);
 Attachment newAttach = new Attachment();
 PageReference ref = Page.PricelistFAEL;
 Blob b = ref.getContentAsPDF();
 
 newAttach.Name = 'Uplift Request';
 newAttach.ParentId = currentid;
 newAttach.Body = b;
 
 Insert newAttach;
 system.debug('CurrentId>>>'+currentid);
 PageReference pageRef = new PageReference('/'+currentid);
 pageRef.setRedirect(true);
 return pageRef;
}

Hi, I want the test class for these two methods. If anyone know reply......
Ray KaushikRay Kaushik
Rather than getting the code directly, I would suggest you can go through this link for details.

This will help you in any further test classes that you write. Link - https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

For Blobs, we generally check for not null asserts to verify it matched correctly

Do let me know if it helps

Thanks.
Ray
Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Can not use the getContent() or getContentAsPDF() in Apex test class.
One idea is posted on same
https://success.salesforce.com/ideaview?id=08730000000HzknAAC
Please modify your class method like below
Solution 1:-
if (!Test.isRunningTest()){
    body = thePage.getContentAsPDF(); 
}
else{
    body = Blob.valueof('Some random String');
}

Solution 2:-
http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/

Please let us know if this will help you

Thanks
Amit Chaudhary