I'm trying to write a test class to validate a VF page and apex class that only performs a SOQL query to find all accounts matching a city or state. However, I can't figure out what i'm testing for and where i'm testing despite all of the documentation i am reading because it's not just creating a record to test a deletion on but rather just querying a bunch of records.
Here's the original code that you nice folks helped me with but I never put a test class in so it's failing code coverage now...
I have a Place__c
record called "Massachusetts" with a State_Code__c
field (e.g. "MA"). I'm trying to embed a Visualforce Page
on the Place__c
Page Layout to show all Account
records that have a BillingState
that matches the State_Code__c
field value of the current record (in this case, "MA"), and have it preferably look like a Related List
in LEX.
There is no relationship between place and account. We just want to be able to look at all accounts that have a match to the State Code.
VF Page AccountsinPlaces
<apex:page standardController="Place__c" extensions="AccountsinPlacesExt">
<apex:stylesheet value="{!URLFOR($Resource.SLDS0122, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />
<!-- Rest of your page -->
<script>
function navSObject(reciord)
{
sforce.one.navigateToSObject(reciord,"detail");
};
</script>
<div class="slds" style=".slds-scrollable--x">
<h1>Accounts with Services located in {!Place__c.Name}, and the contacts for those Accounts.</h1>
<br />NOTE: The specific contacts might not actually be affiliated with the Service shown, this is a list of all contacts associated with the Account.<br />
<apex:dataTable value="{!inPlaceAccounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="slds-table slds-table--bordered">
<apex:Column >
<apex:facet name="header" >Account Name</apex:facet>
<apex:outputLink onclick="navSObject('{!Account.Id}')">{!Account.name}</apex:outputLink>
</apex:Column>
<apex:Column >
<apex:facet name="header">Account JFF Role Name</apex:facet>
<apex:repeat value="{!account.Our_Role_Service_Affiliation_for_Account__r}" var="junction">
<apex:outputLink onclick="navSObject('{!junction.id}')">
{!junction.Our_Role__r.name}</apex:outputLink><br />
</apex:repeat>
</apex:Column>
<apex:Column >
<apex:facet name="header">Service Name</apex:facet>
<apex:repeat value="{!account.Our_Role_Service_Affiliation_for_Account__r}" var="junction">
<apex:outputLink onclick="navSObject('{!junction.Service__r.id}')">
{!junction.Service__r.name}</apex:outputLink><br />
</apex:repeat>
</apex:Column>
<apex:Column >
<apex:facet name="header">Contact Name</apex:facet>
<apex:repeat value="{!account.Contacts}" var="contact"><apex:outputLink onclick="navSObject('{!contact.id}')">{!contact.name}</apex:outputLink><br /></apex:repeat>
</apex:Column>
</apex:dataTable>
</div>
</apex:page>
Apex Class AccountsinPlacesExt
public with sharing class AccountsinPlacesExt {
@AuraEnabled
public Place__c place { get; private set; }
public List<Account> inPlaceAccounts { get; private set; }
public AccountsinPlacesExt(ApexPages.StandardController controller)
{
// add the Place__c field that tells you which state
List<String> neededFields = new List<String> { 'City__c','State_Code__c' };
if (!Test.isRunningTest()) controller.addFields(neededFields);
place = (Place__c)controller.getRecord();
if (place.City__c != null) {
// search for city and state match
inPlaceAccounts = [
SELECT Id,Name,BillingState,(SELECT Name FROM Contacts),(SELECT Id,Service__r.Name,Our_Role__r.Name FROM Our_Role_Service_Affiliation_for_Account__r) FROM Account WHERE Id IN (SELECT Account__c FROM Our_Role_Service_Affiliation_for_Account__c WHERE Our_Role__r.Name != 'Outreach') AND Our_Role_Count__c > 0 AND BillingCity = :place.City__c AND BillingState = :place.State_Code__c
];
} else {
//search for only state match
inPlaceAccounts = [
SELECT Id,Name,BillingState,(SELECT Name FROM Contacts),(SELECT Id,Service__r.Name,Our_Role__r.Name FROM Our_Role_Service_Affiliation_for_Account__r) FROM Account WHERE Id IN (SELECT Account__c FROM Our_Role_Service_Affiliation_for_Account__c WHERE Our_Role__r.Name != 'Outreach') AND Our_Role_Count__c > 0 AND BillingState = :place.State_Code__c
];
}
// end else
}
}