function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Isaac GomolkaIsaac Gomolka 

Help with Visualforce Extension Class

Hi everyone, so I'm having trouble making my extension class for my VF page be able to interact with custom fields that I need. When the picklists in my VF page get selected Yes/No it triggers a method in the extension class to check a checkbox Checked for Yes, Not Checked for No. But as of right now when I run my VF page and try to select Yes/No it gives me the error "Attempt to de-reference a null object. Error is in expression '{!setDataIntegrity}' in page escalation_question_page: Class.EscalationPageExtension.setDataIntegrity: line 31, column 1". So there is clearly something wrong when trying to check the checkbox.

Any help would be absolueltly fantastic and greatly appreciated. If you would also be able to give me an exmaple of how my Test Class for the extension would look I would really appreciate it. Im horrible with test classes and have no idea how it would look. Thanks again!

Extension:
/***************************************************************
 * Used for the Visualforce page Escalation Question Page
 * Created by Isaac Gomolka
 * 7/13/2017
 * ************************************************************/

public with sharing class EscalationPageExtension { 
    
    public String picklist1{get;set;}
    public String picklist2{get;set;}
    public String picklist3{get;set;}
    private Case cases;
   
    private ApexPages.StandardController ctrl;
    public EscalationPageExtension(ApexPages.StandardController controller)
    {
        ctrl = controller; 
        this.ctrl = ctrl;   
    }
    
    public List<SelectOption> options { get; private set; }
    {
    options = new List<SelectOption>();
    options.add(new SelectOption('', '-Select-')); // ==> option null
    options.add(new SelectOption('false', 'No'));
    options.add(new SelectOption('true', 'Yes'));
    }
    
    public void setDataIntegrity()
    {
        this.cases.Escalation_Data_Integrity__c = Boolean.valueof(picklist1); 
    }
    
    public void setViableWorkaround()
    {
        this.cases.Escalation_Viable_Workaround__c = Boolean.valueof(picklist2);
    }
    
    public void setAppFunction()
    {
        this.cases.Escalation_Application_Functionality__c = Boolean.valueof(picklist3);
    }
}

VF Page
<apex:page standardController="Case" extensions="EscalationPageExtension"><!-- We should keep sidebars and headers to give them a way out -->
    <apex:pageBlock title="{!$User.FirstName}, Answer Questions to Escalate Case.">
    
    <apex:form id="formID">
      
      <h1>How many users are affected?</h1>
      <apex:inputField value="{! Case.Escalation_Users_Affected__c }" required="true"/>
    
      <br/>
      <h1>Does this affect data integrity?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist1}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel1" action="{!setDataIntegrity}"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel1">
      <apex:outputText value="How?" style="display:{!if(picklist1=='true', 'block', 'none')}"/>
      <apex:inputField value="{!Case.Escalation_Data_Integrity_Explain__c}" required="{!picklist1}" style="display:{!if(picklist1=='true', 'block; width:250px; height:75px;', 'none')}"/>
      <!-- <apex:inputCheckbox value="{!Case.Escalation_Data_Integrity__c}" selected="true" style="display:none;" rendered="{!picklist1}"/> -->
      </apex:outputPanel>
      
      <br/> <br/>
      <h1>Do you have a viable workaround?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist2}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel2" action="{!setViableWorkaround}"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel2">
      <apex:outputText value="What is the viable workround?" style="display:{!if(picklist2=='true', 'block','none')}"/>
      <apex:inputField value="{!Case.Escalation_Viable_Workaround_Explain__c}" required="{!picklist2}" style="display:{!if(picklist2=='true', 'block; width:250px; height:75px;', 'none')}" />
      <!-- <apex:inputCheckbox value="{!Case.Escalation_Viable_Workaround__c}" selected="true" style="display:none;" rendered="{!picklist2}"/> -->
      </apex:outputPanel>
    
      <br/> <br/>
      <h1>Does this affect critical application functionality?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist3}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel3" action="{!setAppFunction}"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel3">
      <apex:outputText value="How?" style="display:{!if(picklist3=='true', 'block','none')}"/>
      <apex:inputField value="{!Case.Escalation_App_Functionality_Explain__c}" required="{!picklist3}" style="display:{!if(picklist3=='true', 'block; width:250px; height:75px;', 'none')}" />
      <!-- <apex:inputCheckbox value="{!Case.Escalation_Application_Functionality__c}" selected="true" style="display:none;" rendered="{!picklist3}"/> -->
      </apex:outputPanel>
      
      <br/> <br/>
      <h1>What business functionality are you unable to perform?</h1>
      <br/>
      <apex:inputField value="{! Case.Escalation_Business_Functionality__c }"  style="width: 300px; height: 100px" required="true"/>
      
      <br/> <br/> <br/>
      <apex:inputCheckbox value="{!Case.IsEscalated}" selected="true" style="display:none;"/>
      <apex:commandButton action="{!save}" value="Submit" />
      
    </apex:form>  
    </apex:pageBlock>   
</apex:page>

Thanks again in advanced for the help!​
Best Answer chosen by Isaac Gomolka
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
My Approach to this is as follows

1) prepare necessary test data with all necessary fields for your case object that you are going to test
2) try to test the extension controller as mentioned below examples (https://developer.salesforce.com/forums/?id=906F00000008y7QIAQ)
public class newAccountPlan {
    public final AccountPlan__c addPlan;   
    public List<Contact> accountContact;   
    public apexpages.standardController controller {get; set;}
    
    public newAccountPlan(ApexPages.StandardController stdController) 
    {   controller = stdController;      
        this.addPlan = (AccountPlan__c)Controller.getRecord();  
    } 
    
     public List<Contact> getaccountContact()  
         
    {    
        string account_id_st = addPlan.Account__c;
              accountContact = [Select id, Name,ReportsTo.Id, Title, ReportsTo.Name, Account.Id from Contact where Account.Id = :account_id_st];<br>    
                return accountContact;   }            
                public PageReference save()   {      
                 try{           
                     update addplan;    
                     } 
                    catch(DmlException ex){
                       ApexPages.addMessages(ex);       
                     }
                                                                    
                    return null;
                }   
    }

test method
 
static testmethod void validateStandardController(){
            List<Account> accounts = new List<Account>{};
            List<AccountPlan__c> accountPlans= new List<AccountPlan__c>{};
            List<Contact> accountContact = new List<Contact>();
            
            String companyString;

            Account testAccount = new Account(Name='Test Company Name123');
            accounts.add(testAccount);
            insert accounts;
            
            companyString = accounts[0].id;
            AccountPlan__c testAccountPlanInsert = new AccountPlan__c(name = 'Account Plan Test123',Account__c = companyString , accountmanager__c = Userinfo.getUserId());
            accountPlans.add(testAccountPlanInsert);           
        insert accountPlans;
        
         ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
        newAccountPlan testAccPlan = new newAccountPlan(sc);
        
        PageReference pageRef = Page.AccountPlan;
        pageRef.getParameters().put('id', String.valueOf(testAccountPlanInsert.Id));
        Test.setCurrentPage(pageRef);
    
        
        testAccPlan.getaccountContact();
        testAccPlan.save();
        
        
      }

3) for figuring out select option test  please go through this link (https://developer.salesforce.com/forums/?id=906F00000008zA1IAI)


This would be my best approach to start, if you need any further assistance please go through this link (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm)


Let me know, what you tried for writing the test classs and post it here so that i can assist you better

Thank you!

All Answers

Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
HI Isaac you need to get the id from the standard controller (https://developer.salesforce.com/forums/?id=906F00000008z4nIAA) and coming to test classes for controllers this link (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm)should help!

If this helps to solve your problem, please choose this as best answer.

Thank you!
Isaac GomolkaIsaac Gomolka
Hey Akhilesh, would you be able to explain a bit more? I tried using the getting ID thing, but it doesnt work because im trying to get a field and be able to change it. I think that way would only give me the value of the field, not the field itself. Would u be able to show me using a bit of code? I'm just really confused and nothing seems to be working.

Thanks!
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Isaac, all you need is to update an objects field value from visual force page, if i understood correct please follow this code which achieves the same on account object. it update the account description field based on the field selected by the user in Visual force page.


Visual force page
 
<apex:page  standardController="Account" extensions="ActionSupportTest" showHeader="true">
    <apex:form id="form">
        <h1>{!now()}</h1><br/>
        <apex:pageMessages/>
        <apex:selectList value="{!discountScheduleID}" size="1" >
            <apex:actionSupport event="onchange" action="{!calcPricing}" rerender="form"/>
            <apex:selectOptions value="{!schedules}" />
        </apex:selectList>
        
    </apex:form>
</apex:page>
Controller
public class ActionSupportTest {
    public string acc{get;set;}
    
    public ActionSupportTest(ApexPages.StandardController con){
       acc=con.getRecord().Id;
    }

    public SelectOption[] getSchedules() {
        return new SelectOption[] { new SelectOption('Value1', 'Option1'), 
            new SelectOption('Value2', 'Option2'), new SelectOption('Value3', 'Option3') };
    }

    public String discountScheduleID { get; set; }
    
    public PageReference calcPricing(){
        system.debug('EXECUTED'+discountScheduleID);
        account a=new account(id=acc);
        a.Description=discountScheduleID;
        update a;
        return null;
    }
    
}

So to test this page pass the account id parameter in the url of the visual force page and select an option, opent the account and observe the description field.


I feel you need to figure out some thing similar to this as per your requirement.

If this helps to reach your approach please choose this as the best answer.

Thank you! 
 
Isaac GomolkaIsaac Gomolka
That example definetly helps. Would you be able to show me the code for the test class? My code works all good now, but I'm still having trouble with making my test class. If youd be able to help me out and show me either what my test class would look like or at least the code you put, I'd really appreciate it.

Thanks,
Isaac
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Isaac, can you post your working controller class, to give you an idea about the test class!
thank you
Isaac GomolkaIsaac Gomolka
yup, heres the new controller class:
/***************************************************************
 * Used for the Visualforce page Escalation Question Page
 * Created by Isaac Gomolka
 * 7/13/2017
 * ************************************************************/

public with sharing class EscalationPageExtension { 
    
    public String picklist1{get;set;}
    public String picklist2{get;set;}
    public String picklist3{get;set;}
   
    private ApexPages.StandardController ctrl;
    public EscalationPageExtension(ApexPages.StandardController controller)
    {
        ctrl = controller;    
    }
    
    public List<SelectOption> options { get; private set; }
    {
    options = new List<SelectOption>();
    options.add(new SelectOption('', '-Select-')); // ==> option null
    options.add(new SelectOption('false', 'No'));
    options.add(new SelectOption('true', 'Yes'));
    }
    
    
    public void setDataIntegrity()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Data_Integrity__c = Boolean.valueof(picklist1); 
    }
    
    public void setViableWorkaround()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Viable_Workaround__c = Boolean.valueof(picklist2);
    }
    
    public void setAppFunction()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Application_Functionality__c = Boolean.valueof(picklist3);
    }
}

Thanks again for the help, this has been seriously stressing me out!
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
My Approach to this is as follows

1) prepare necessary test data with all necessary fields for your case object that you are going to test
2) try to test the extension controller as mentioned below examples (https://developer.salesforce.com/forums/?id=906F00000008y7QIAQ)
public class newAccountPlan {
    public final AccountPlan__c addPlan;   
    public List<Contact> accountContact;   
    public apexpages.standardController controller {get; set;}
    
    public newAccountPlan(ApexPages.StandardController stdController) 
    {   controller = stdController;      
        this.addPlan = (AccountPlan__c)Controller.getRecord();  
    } 
    
     public List<Contact> getaccountContact()  
         
    {    
        string account_id_st = addPlan.Account__c;
              accountContact = [Select id, Name,ReportsTo.Id, Title, ReportsTo.Name, Account.Id from Contact where Account.Id = :account_id_st];<br>    
                return accountContact;   }            
                public PageReference save()   {      
                 try{           
                     update addplan;    
                     } 
                    catch(DmlException ex){
                       ApexPages.addMessages(ex);       
                     }
                                                                    
                    return null;
                }   
    }

test method
 
static testmethod void validateStandardController(){
            List<Account> accounts = new List<Account>{};
            List<AccountPlan__c> accountPlans= new List<AccountPlan__c>{};
            List<Contact> accountContact = new List<Contact>();
            
            String companyString;

            Account testAccount = new Account(Name='Test Company Name123');
            accounts.add(testAccount);
            insert accounts;
            
            companyString = accounts[0].id;
            AccountPlan__c testAccountPlanInsert = new AccountPlan__c(name = 'Account Plan Test123',Account__c = companyString , accountmanager__c = Userinfo.getUserId());
            accountPlans.add(testAccountPlanInsert);           
        insert accountPlans;
        
         ApexPages.StandardController sc = new ApexPages.StandardController(testAccountPlanInsert);
        newAccountPlan testAccPlan = new newAccountPlan(sc);
        
        PageReference pageRef = Page.AccountPlan;
        pageRef.getParameters().put('id', String.valueOf(testAccountPlanInsert.Id));
        Test.setCurrentPage(pageRef);
    
        
        testAccPlan.getaccountContact();
        testAccPlan.save();
        
        
      }

3) for figuring out select option test  please go through this link (https://developer.salesforce.com/forums/?id=906F00000008zA1IAI)


This would be my best approach to start, if you need any further assistance please go through this link (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm)


Let me know, what you tried for writing the test classs and post it here so that i can assist you better

Thank you!
This was selected as the best answer
Isaac GomolkaIsaac Gomolka
Thank you so much for all the help. I will take a look at the links and post back here if I still can't figure it out. Thanks again!