• Connor Small 5
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 0
    Replies
How can I write a test class for a Controller that updates 2 related records?
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

 
I have a fairly simple controller that queries a record and references values from the record in order to render other VF pages.

How can I write a test class for this Controller?
public class MyController {
    
 public Account account  { get; set; }
        
        public MyController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

	}
}

 
public class MyController {
    
 public Account account  { get; set; }
        
        public MyController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

	}
}

This controller works for a VF page I have that renders other pages if they meet criteria. 

This Controller also achieves for the same purpose 
public class MyPageController {
 
    Public final Account account;
 
    	public MyPageController() {
        account = [SELECT Id, Name, Billing_Tier__c, Primary_Contact_Full_Name__C,Secondary_Contact_Full_Name__c,One_Line_Address__c FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
    	public Account getAccount() {
        return account;
    }
    	public PageReference Save(){
        Update account;
        Return Null;
    }

}

This Test Class (for the second controller) achieves 7/8 coverage in Sandbox but fails deployment because of error below

"System.DmlException: Update failed. First exception on row 0 with id 0012E00002KmF35QAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Unable to create/update fields: Name. Please check the security settings of this field and verify that it is read/write for your profile or permission set.: [Name]
Stack Trace: Class.MyPageController.Save: line 13, column 1 Class.MyPageControllerTest.testMethodMyController: line 20, column 1"

Anyone have any insight as to how to test Controller 1 or improve test for controller 2 so as not to fail deployment for that reason?