You need to sign in to do that
Don't have an account?
JMcD1362
Pass parameters from URL into Site Visualforce page
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();
}
}
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();
}
}
Hope this helps. Thank you
ApexPages.currentPage().getParameters().put('acctNum',sc.acctNum);
<apex:page controller="SubmitCaseController" sidebar="false" showheader="false">
<apex:sectionHeader title="Request Support Assistance"/>
<apex:form >
<apex:pageBlock title="Your Information" id="YourInformation"
mode="edit">
<apex:pageMessages />
<apex:pageBlockButtons >
<apex:commandButton value="Submit Case" action="{!submitCase}"/>
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:pageBlockSection title="Basic Information"
columns="1">
<apex:inputField value="{!c.SuppliedName}" label="Your Name"/>
<apex:inputField value="{!c.SuppliedEmail}" label="Your Email" required="true"/>
<apex:inputText value="{!acctNum}" label="Billing Account Number" required="true"/>
<apex:inputField value="{!c.Subject}" label="Subject"/>
<apex:inputTextarea value="{!c.Description}" label="Description"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Is Device Installed"/>
<apex:outputPanel >
<apex:inputField value="{!c.Is_Device_Installed__c}">
<apex:actionSupport event="onchange"
rerender="thePageBlock"/>
</apex:inputField>
</apex:outputPanel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:actionRegion>
<apex:actionRegion >
<apex:pageBlockSection title="Please help us further isolate the issue."
columns="1"
rendered="{!c.Is_Device_Installed__c == 'Yes'}">
<apex:inputField value="{!c.Has_SIM_card_been_activated__c}"
required="true"/>
<apex:actionsupport event="onchange" rerender="thePageBlock"/>
<apex:inputField value="{!c.What_Colors_are_Displayed__c}"/>
<apex:actionsupport event="onchange" rerender="thePageBlock"/>
</apex:pageblocksection>
</apex:actionRegion>
</apex:pageblock>
</apex:form>
</apex:page>