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
JMcD1362JMcD1362 

Test Class for Creating a Case from Salesforce Site

Hi,

I am new to Apex and I create a way to create a case from a site, which references the custom billing account number field.  But, I can't figure out how to write the test class.  I basically followed this article https://developer.salesforce.com/page/Creating_Custom_Web-To-Case_Forms_Using_Visualforce_and_Sites for the page and class.  But, what would my test class be?

My Class -
@isTest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
     // Create the required test data needed for the test scenario.
     // In this case, I need to update an Account to have a Billing Account Number'controller'
     // So I create that Account in my test method itself.
    
     Account testAccount = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     insert testAccount;

     // Verify that the billingState field was updated in the database.
     Account updatedAccount = [SELECT Billing_Account_Number__c FROM Account WHERE Id = :testAccount.Id];
     System.assertEquals('CONTROLLER', updatedAccount.Billing_Account_Number__c);

     // In this case, I need to update a Case to have a BAN ='CONTROLLER'
     // So I create that Case record in my test method itself.
     Case TestC = new Case(Subject='Test Controller Acct Case',Billing_Acct_Number__c = 'CONTROLLER',
     Has_SIM_card_been_activated__c='Yes',What_Colors_are_Displayed__c = 'Black');
     insert TestC;
    
     // Verify that the case field was updated in the database.
     Case updatedCases = [SELECT Subject FROM Case WHERE Id = :TestC.Id];
     System.assertEquals('Test Controller Acct Case', updatedcases.Subject);
    
     confirm() {
     PageReference q = test.confirm();
     PageReference s = Page.SubmitCaseThanks;
     system.assertEquals(q.getURL(), s.getURL());
}
}

Any help is appreciated.
Best Answer chosen by JMcD1362
rohitsfdcrohitsfdc
@istest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
     // Create the required test data needed for the test scenario.
     // In this case, I need to update an Case to have a Billing Acct number
     // So I create that Account in my test method itself.
    
     Account testAccount = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     insert testAccount;

   Account testAccount2 = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     
	 
	 SubmitCaseController sc = new SubmitCaseController();
	 sc.acctNum='CONTROLLER';
	 
	 Case ca = new Case(Subject='Test Controller Acct Case');
	 ca.Is_Device_Installed__c='No';
	 ca.Has_SIM_card_been_activated__c='No';
	 
	 sc.c = ca;
	 
	 sc.submitcase();
	 
	 sc.c.Has_SIM_card_been_activated__c='Yes';
	 sc.submitcase();

insert testAccount2;
 sc.submitcase();


	 
}
}

All Answers

rohitsfdcrohitsfdc
Jackie,
Are you getting any error while running the code above? or is it the coverage where you need help. PLease advise
JMcD1362JMcD1362
I don't have code coverage.  So, it is like it isn't testing anything.  I do have an error with the page reference items.  I can post the error in a bit, but I think the whole thing is just bad.  Thanks for your help.
JMcD1362JMcD1362
Here is my Class; 

public class SubmitCaseController {
    public Case c { get; set; }

    public String acctNum { get; set; }
   

    public SubmitCaseController() {
        c = new Case();
    }
 
    public PageReference submitCase() {
        List<Account> accts = [SELECT Id FROM Account WHERE Billing_Account_Number__c = :acctNum];
        if (accts.size() != 1) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
            ApexPages.addMessage(msg);
            return null;
        }
       
        if (c.Is_Device_Installed__c =='No') {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please install device');
            ApexPages.addMessage(msg);
            return null;
        }
       
         if (c.Has_SIM_card_been_activated__c=='No') {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please activate SIM');
            ApexPages.addMessage(msg);
            return null;
        }
       
       
       
        else {
            try {
                c.AccountId = accts.get(0).Id;
              
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                dmlOpts.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmlOpts);
                // Insert the case
                INSERT c;
                String param = 'RecordType=012G0000001IbQk';
               
                return new PageReference('/SubmitCaseThanks');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}


Here is my Test Class - that doesn't have errors, but I don't have any code coverage.

@isTest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
     // Create the required test data needed for the test scenario.
     // In this case, I need to update an Case to have a Billing Acct number
     // So I create that Account in my test method itself.
    
     Account testAccount = new Account(name='Test Company Name');
     insert testAccount;
       
     testAccount.Billing_Account_Number__c='CONTROLLER';
     update testAccount;

     // Verify that the billing account number field was updated in the database.
     Account updatedAccount = [SELECT Billing_Account_Number__c FROM Account WHERE Id = :testAccount.Id];
     System.assertEquals('CONTROLLER', updatedAccount.Billing_Account_Number__c);

     // In this case, I need to update a Case to have a BAN ='CONTROLLER'
     // So I create that Case record in my test method itself.
     Case TestC = new Case(Subject='Test Controller Acct Case');
     insert TestC;
    
     TestC.Subject='Test Controller Acct Case';
     TestC.Billing_Acct_Number__c = 'CONTROLLER';
     update TestC;
     // Verify that the case field was updated in the database.
     Case updatedCases = [SELECT Subject FROM Case WHERE Id = :TestC.Id];
     System.assertEquals('Test Controller Acct Case', updatedcases.Subject);
}
}
rohitsfdcrohitsfdc
Try the test class as stated below

@istest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
     // Create the required test data needed for the test scenario.
     // In this case, I need to update an Case to have a Billing Acct number
     // So I create that Account in my test method itself.
    
     Account testAccount = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     insert testAccount;
	 
	 SubmitCaseController sc = new SubmitCaseController();
	 sc.acctNum='CONTROLLER';
	 
	 Case ca = new Case(Subject='Test Controller Acct Case');
	 ca.Is_Device_Installed__c='No';
	 ca.Has_SIM_card_been_activated__c='No';
	 
	 sc.c = ca;
	 
	 sc.submitcase();
	 
	 sc.c.Has_SIM_card_been_activated__c='Yes';
	 sc.submitcase();

	 
}
}


JMcD1362JMcD1362
Thank you!  My hero!  That got me 68% code coverage for the class, 75% overall.  But, when I went to deply the change set, I received a new error.  Have you seen this?  Thanks for all your help.  Sorry I am so novice.User-added image
rohitsfdcrohitsfdc
jackie,
Go to SETUP-> Apex classes and locate the class "submitCaseControllerTest". 
Click on edit and click on version tab. 

Change the version of the apex class to 30. Save it.

Now try adding the component to change set.

If that answer solves your problem, please mark this as Best Answer
JMcD1362JMcD1362
Is there anything else I can do to obtian a higher code coverage?  When I deployed it to Prod, I apparently only have 71% code coverage there with this code and it needs to be 75.
rohitsfdcrohitsfdc
@istest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
     // Create the required test data needed for the test scenario.
     // In this case, I need to update an Case to have a Billing Acct number
     // So I create that Account in my test method itself.
    
     Account testAccount = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     insert testAccount;

   Account testAccount2 = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     
	 
	 SubmitCaseController sc = new SubmitCaseController();
	 sc.acctNum='CONTROLLER';
	 
	 Case ca = new Case(Subject='Test Controller Acct Case');
	 ca.Is_Device_Installed__c='No';
	 ca.Has_SIM_card_been_activated__c='No';
	 
	 sc.c = ca;
	 
	 sc.submitcase();
	 
	 sc.c.Has_SIM_card_been_activated__c='Yes';
	 sc.submitcase();

insert testAccount2;
 sc.submitcase();


	 
}
}

This was selected as the best answer