You need to sign in to do that
Don't have an account?
Connor Small 5
Test Class for VF Custom Controller that updates related records
How can I write a test class for a Controller that updates 2 related records?
Controller:
Any help is greatly appreciated
Controller:
public class NASController { public Account Household { get; set; } public New_Account_Setup__c CurNAS { get; set; } public Account PrimCont { get; set; } Public String StatusSet {get;set;} Public String BilledSet {get;set;} Public String CTSet {get;set;} Public String ReportSet {get;set;} Public String AssignmentSet {get;set;} Public String AllocationSet {get;set;} Public String ThirtyDaySet {get;set;} Public String RestrictedSet {get;set;} Public String AdditionalSet {get;set;} public NASController() { CurNAS = [SELECT Id,Account_Status__c,Additional_Cash_Reserve__c,ContactAcctID__c,Centralized_Trading_Picklist__c,Report_on_Account_Picklist__c,Account_Billed_Picklist__c,Model_Assignment__c,Model_Allocation__c,List_any_restricted_securities_to_keep__c,Assets_in_Next_30_Days__c,Additional_Cash_Reserve_Picklist__c,Account__c,Contact__c,Primary_Contact__c FROM New_Account_Setup__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; Household = [SELECT Id,Classification__c,Billing_Tier__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('Account__c')]; PrimCont = [SELECT Id,AccountSource,Onboarding_Offering__c FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('ContactAcctID__c')]; } public PageReference save() { update Household; update PrimCont; return null; } Public Void ValueSet(){ IF(CurNAS.Account_Status__c != Null){ StatusSet = 'none'; } IF(CurNAS.Account_Billed_Picklist__c != Null){ BilledSet = 'none'; } IF(CurNAS.Centralized_Trading_Picklist__c != Null){ CTSet = 'none'; } IF(CurNAS.Model_Assignment__c != Null){ AssignmentSet = 'none'; } IF(CurNAS.Model_Allocation__c != Null){ AllocationSet = 'none'; } IF(CurNAS.Report_on_Account_Picklist__c != Null){ ReportSet = 'none'; } IF(CurNAS.List_any_restricted_securities_to_keep__c != Null){ RestrictedSet = 'none'; } IF(CurNAS.Assets_in_next_30_days__c != Null){ ThirtyDaySet = 'none'; } IF(CurNAS.Additional_Cash_Reserve__c != Null){ AdditionalSet = 'none'; } } }VF Page;
<apex:page controller="NASController" lightningStylesheets="true" action="{!ValueSet}"> <apex:form style="overflow:auto;"> <div style="overflow:auto;"> <apex:pageBlock title="Needed Household/Contact Fields"> <apex:pageBlockSection columns="1" showHeader="false" > <apex:inputField value="{!Household.Billing_Tier__c}" rendered="{!Household.Billing_Tier__c = null }"/> <apex:inputField value="{!Household.Classification__c}" rendered="{!Household.Classification__c = null}"/> <apex:inputField value="{!PrimCont.AccountSource}" rendered="{!PrimCont.AccountSource = null }"/> <apex:inputField value="{!PrimCont.Onboarding_Offering__c}" rendered="{!CurNAS.Primary_Contact__c = true && PrimCont.Onboarding_Offering__c =null}"/> <apex:outputText value="All fields needed on Household/Contact are complete" rendered="{!Household.Billing_Tier__c != null && Household.Classification__c != null && PrimCont.AccountSource != null && OR(CurNAS.Primary_Contact__c = true && PrimCont.Onboarding_Offering__c !=null,CurNAS.Primary_Contact__c = false)}"></apex:outputText> </apex:pageBlockSection> <div align="center" draggable="false" > <apex:commandButton value="Save" action="{!save}"/> </div> </apex:pageBlock> </div> <apex:pageBlock > <apex:pageBlockSection > <h1 style="font-size:16px"><u>Missing Fields on New Account Setup</u></h1> <ul> <li style="Display:{!StatusSet};">Account Status</li> <li style="Display:{!CTSet};">Centralized Trading</li> <li style="Display:{!ReportSet};">Report on Account</li> <li style="Display:{!BilledSet};">Account Billed</li> <li style="Display:{!AssignmentSet};">Model Assinment</li> <li style="Display:{!AllocationSet};">Model Allocation</li> <li style="Display:{!RestrictedSet};">List any Restricted Securities</li> <li style="Display:{!ThirtyDaySet};">Assets in next 30 Days</li> <li style="Display:{!AdditionalSet};">Additional Cash Reserve</li> </ul> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>Attempt at Test
@isTest public class NASControllerTest { public static TestMethod void AccTest() { Account Acc = New Account(Name='Test', Billing_Tier__c=null, Classification__c='Gold'); Insert Acc; Account Acc2 = New Account(Name='Test', AccountSource=null, Onboarding_Offering__c=Null); Insert Acc2; New_Account_Setup__c NAS = new New_Account_Setup__c(Account__c = Acc.Id, Contact__c = Acc2.PersonContactID, Account_Billed_Picklist__c = 'Yes', Centralized_trading_Picklist__c = 'Yes', Report_on_Account_Picklist__c = 'Yes', Account_Status__c = 'Active', List_any_restricted_securities_to_keep__c = 'NA', Assets_in_next_30_days__c = 0, Additional_Cash_Reserve_picklist__c = 'No'); Insert NAS; Test.startTest(); system.assertEquals(null, acc.Billing_Tier__c); PageReference pageRef = Page.NewAccountSetup; pageRef.getParameters().put('Id',NAS.id); Test.setCurrentPage(pageRef); NASController AccController = new NASController(); Acc2.AccountSource = 'Unknown'; Acc.Billing_Tier__c = 'TRPG Standard'; AccController.ValueSet(); AccController.Save(); Test.stopTest(); AccController.Household = [Select Name, Billing_Tier__c FROM Account WHERE Id =:Acc.Id]; AccController.PrimCont = [Select Name,AccountSource FROM Account WHERE Id =:Acc2.Id]; system.assertEquals('TRPG Standard', AccController.Household.Billing_Tier__c); system.assertEquals('Unknown', AccController.PrimCont.AccountSource); } }
Any help is greatly appreciated