• JMcD1362
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies
HI,

I'm trying to create a controller where I can pass values from a URL into my site.  The controller appears to work as I expected, but the test class doesn't have enough code coverage.  I can't get it to actually set my account number.  I have 19% code coverage, which looks like it's the first lines of my test class, but nothing after where it's supposed to set the account.

Any help is appreciated.


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 = :
        ApexPages.currentPage().getParameters().get('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';
               
               // c = [SELECT Id, CaseNumber, Is_Device_Installed__c, Has_SIM_card_been_activated__c
                //FROM Case WHERE Id = :c.Id];
                return new PageReference('/SubmitCaseThanks');
               
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}


Test Class:
@istest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
   
  SubmitCaseController sc = new SubmitCaseController();  
   
  system.currentPageReference().getparameters().put('Id',sc.acctNum);
     
     Case ca = new Case(Subject='Test Controller Acct Case');

     ca.SuppliedName='JMcD';
     ca.Billing_Acct_Number__c=sc.acctNum;
     ca.SuppliedEmail='test@test.com';
     ca.Subject='test subject controller';
     ca.Is_Device_Installed__c='Yes';
     ca.Has_SIM_card_been_activated__c='Yes';
     ca.Type='012G0000001IbQk';
    
     sc.c = ca;
    
     sc.c.Is_Device_Installed__c='Yes';
     sc.c.Has_SIM_card_been_activated__c='Yes';
     sc.c.Type='012G0000001IbQk';
    
     if (sc.c.Is_Device_Installed__c =='No')
        {
        System.assertEquals('No', sc.c.Is_Device_Installed__c, 'Please install device');
        }
               
     sc.submitcase();  
}
}
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.
HI,

I'm trying to create a controller where I can pass values from a URL into my site.  The controller appears to work as I expected, but the test class doesn't have enough code coverage.  I can't get it to actually set my account number.  I have 19% code coverage, which looks like it's the first lines of my test class, but nothing after where it's supposed to set the account.

Any help is appreciated.


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 = :
        ApexPages.currentPage().getParameters().get('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';
               
               // c = [SELECT Id, CaseNumber, Is_Device_Installed__c, Has_SIM_card_been_activated__c
                //FROM Case WHERE Id = :c.Id];
                return new PageReference('/SubmitCaseThanks');
               
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}


Test Class:
@istest
public class SubmitCaseControllerTest{
static testMethod void SubmitCaseController(){
   
  SubmitCaseController sc = new SubmitCaseController();  
   
  system.currentPageReference().getparameters().put('Id',sc.acctNum);
     
     Case ca = new Case(Subject='Test Controller Acct Case');

     ca.SuppliedName='JMcD';
     ca.Billing_Acct_Number__c=sc.acctNum;
     ca.SuppliedEmail='test@test.com';
     ca.Subject='test subject controller';
     ca.Is_Device_Installed__c='Yes';
     ca.Has_SIM_card_been_activated__c='Yes';
     ca.Type='012G0000001IbQk';
    
     sc.c = ca;
    
     sc.c.Is_Device_Installed__c='Yes';
     sc.c.Has_SIM_card_been_activated__c='Yes';
     sc.c.Type='012G0000001IbQk';
    
     if (sc.c.Is_Device_Installed__c =='No')
        {
        System.assertEquals('No', sc.c.Is_Device_Installed__c, 'Please install device');
        }
               
     sc.submitcase();  
}
}
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.