0

I have a Selectlist in my Visualforce page and I'm passing the values from the controller. The value selected is not getting captured. The code is given below.

Visualforce page

<apex:page standardcontroller="Case" extensions="createB2BorB2CController" showHeader="false" sidebar="false">
   <apex:pageMessages />
   <apex:form >
   <apex:sectionHeader title="Case"/>
   <apex:pageBlock title="Account Creation">
       <apex:pageBlockButtons >
           <apex:commandButton value="Submit" action="{!Save}"/>
           <apex:commandButton value="Cancel" action="{!Cancel}"/>
       </apex:pageBlockButtons> 
       <apex:pageblockSection title="Account Type" id="companyType">
           <apex:selectList label="Account Type" value="{!accountType}" multiselect = "false" size="1">
              <apex:actionSupport event="onchange" rerender="cmpny"/>
              <apex:selectOptions value="{!type}"/>
           </apex:selectList>
           <apex:outputPanel id="cmpny" >
           <apex:InputText label = "Company" value="{!company}" rendered="{!IF(accountType  == 'B2B' ,true,false)}" required = "true"/>
           </apex:outputPanel>
      </apex:pageblockSection>
      <apex:pageblockSection title="Personal Information">
           <apex:InputText label = "First Name" value="{!firstName}" />
           <apex:InputText label = "Last Name" value="{!lastName}" required = "true"/> 
      </apex:pageblockSection>
      </apex:pageBlock>
   </apex:form>

</apex:page>

Apex Controller

public without sharing class createB2BorB2CController{
    public String accountType {get;set;}
    public String company {get;set;}
    public String firstName {get;set;}
    public String lastName {get;set;}
    public Id caseId;
    Account newAcc = null;
    public PageReference redirectPage;

    public createB2BorB2CController(ApexPages.StandardController controller){
        caseId = ApexPages.currentPage().getParameters().get('id');
        redirectPage = new PageReference('/'+caseId);
    }

    public List<SelectOption> getType(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new Selectoption('None','None'));
        options.add(new SelectOption('B2C','B2C'));
        options.add(new SelectOption('B2B','B2B'));
        return options;
    }

    public pageReference save(){
        if(accountType == 'B2B' && company == NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'If the Account type is B2B, Company should be filled'));
            return null;
        }
        if(lastName == NULL){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Last Name cannot be empty'));
            return null;
        }
        if(company != NULL){
            Account acc = null;
            try{
                newAcc = [Select Id, Name from Account WHERE Name =: Company LIMIT 1];
            }catch(exception e){
                Acc = new Account(Name = Company);
                try{
                    insert acc;
                }catch(exception e1){}
            }
            Contact con = new Contact (FirstName = firstName, LastName = lastName, AccountId = newAcc != NULL? newAcc.Id : acc.Id);
            try{
                insert con;
            }catch(Exception e){}
            Case c = new Case (id = caseId, ContactId = con.Id);
            try{
                update c;
            }catch(exception e){}
        }
        else{
            Account acc = new Account(FirstName = firstName, lastName = lastName);
            try{
                insert acc;
            }catch(exception e){}
            Case c = new Case (id = caseId, AccountId = acc.Id);
            try{
                update c;
            }catch(exception e){}
        }
        return redirectPage;
    }

    public pageReference cancel(){
        return redirectPage;
    }
}

The gist is when the Account Type is selected as 'B2B', the field Company has to rendered. But the value of Account Type is returning NULL

0

I figured the error after a lot of trial and error.

I had to remove required = "true" from

<apex:InputText label = "Company" value="{!company}" rendered="{!IF(accountType  == 'B2B' ,true,false)}" required = "true"/>

and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.