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
labaulabau 

Do a good deed - help a small nonprofit with testing

Hello All:

 

I work for Rebuild Resources, a small nonprofit in St. Paul, MN. We have been using salesforce.com for a couple of years, and are just starting to push the limits by using VisualForce. I have been trying to learn VF and Apex, and have been able to cobble together what will be a very useful (even if simple) VF page and Controller - it allows a public guest to create a new Lead record and attach a file, save the record, and get redirected to a "Thank You" page.

 

However, while I have been able to piece together the necessary code and make it work in our sandbox, I am completely lost when it comes to testing. I have read over the documentation several times and it is just not clicking. I was hoping an experienced dev here might be able to help us out. It uses only standard objects, so it should be pretty easy to replicate. The code is below:

 

 

public class SaveRedirect {

public Blob artfile {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}

Lead lead;
public Lead getLead() {
if(lead == null) lead = new Lead();
return lead;
}
public PageReference save() {
// Add the lead to the database.
insert lead;

if (resume != null) {
Attachment attach = new Attachment();
attach.Body = artfile;
attach.Name = fileName;
attach.ContentType = contentType;
attach.ParentId = lead.id;
try {
insert(attach);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
}

// Send the user to a custom VF Thank You page on save.
PageReference acctPage = page.ThankYou;
acctPage.setRedirect(true);
return acctPage;
}
}

 

<apex:page Controller="SaveRedirect" sidebar="false" showHeader="false" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!Save}" />
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1" >
<apex:inputField value="{!Lead.FirstName}" required="true"/>
<apex:inputField value="{!Lead.LastName}"/>
<apex:inputField value="{!Lead.Product_Quantity__c}" required="true"/>
<apex:inputField value="{!Lead.Company}"/>
<apex:inputFile accept="doc, txt, pdf" filename="{!fileName}" contentType="{!contentType}"filesize="1000" size="50" value="{!artfile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 Your assistance would do a world of good for our organization - you can read more about what we do at rebuildresources.com.

 

Cheers,

 

Josh LaBau
Rebuild Resources, Inc.

 

 

Message Edited by labau on 07-02-2009 02:10 PM
labaulabau
So I have been scouring Java/Apex guides in an attempt to understand this, but I'm still coming up short. I have written a test meth that gets a small amount of code coverage. The relationships in this are a bit beyond my skill level. Am I even getting close?

@isTest private class SaveRedirectTest { static testMethod void myUnitTest() { PageReference pageRef = Page.leadForm; Test.setCurrentPage(pageRef); SaveRedirect sr = new SaveRedirect(); Lead lead = new Lead(LastName='Johnson', Company='Johnson Corp'); insert lead; Attachment attachment = new Attachment(); attachment.body = Blob.valueOf( 'this is an attachment test' ); attachment.name = 'fake attachment'; attachment.ParentId = lead.id; insert attachment; Lead[] leads = [select id, LastName from lead where Company = 'Johnson Corp']; System.assertEquals('Johnson', leads[0].LastName); } }