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
Jarvis HowardJarvis Howard 

How to write a test class with 100% coverage

Hey everyone, I'm not a developer, but have managed to write this test class which works well in Sandbox, but don't know how to write a test class. 
Also - is there any way I can get this to save to 'files' rather than just 'attachments'?

Code is below:

public class OrderAttachPDFExt{
    ApexPages.StandardController controller;
    public Order order {get;set;}
    public PageReference rtn;
    public OrderAttachPDFExt(ApexPages.StandardController c){
        order = (Order)c.getRecord();
        rtn = c.view();
    }
    public PageReference attachOrderGeneratePDF() {
        /* Get the page definition */
        PageReference pdfPage = Page.OrderGeneratePDF;
        pdfPage.getParameters().put('id',order.id);
        /* generate the pdf blob */
        Blob pdfBlob = pdfPage.getContent();
        /* create the attachment against the offer */
        Attachment a = new Attachment(parentId = order.id, name=order.OrderNumber +'.pdf', body = pdfBlob);
        /* insert the attachment */
        insert a;
        /* send the user back to the offer detail page */
        return rtn;
    }
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jarvis,

For increasing the code coverage you will have to implement different cases so that all the scenarios are tested in the test class.

I hope this was helpful.

Regards,
Anutej 
 
Jarvis HowardJarvis Howard
Hi Anutej,

Thanks for that - are you able to give me some pointers on how to do that? 
I've not written a test class before!

Regards, 
Jarvis
ANUTEJANUTEJ (Salesforce Developers) 
I found the below links that could help you in writing test class.

>> https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

>> https://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/

Do let me know if this helps and in case if you need any help let me know.