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
noobfnnoobfn 

Visualforce popultng contacts from Account using fieldset

Hello, I'm a noob still to all of these.  And I'm confused with my code, it doesn't work.  Initially I used a list, I was populating contacts from an Account by entering accountId & click submit button, now I want to do the same but using a FieldSet.  I created my fieldset called "properNames" and I'm stuck and confused with my code.  Can someone help?

 

VP:

<apex:page standardController="Account" extensions="fieldSetExtension">


 <apex:form >
  <apex:pageBlock >
     Enter Account Id:
   <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
        <apex:inputText value="{!ContactForAcc.AccountId}" />
        <apex:commandbutton value="Submit" action="{!redirectUser}"/>
        </apex:pageBlockSectionItem>
   </apex:pageBlockSection>
     
 
   <apex:pageBlockSection columns="1" id="contactTable" title="Contats Details">
        <apex:pageBlockTable value="{!lstCon}" var="con">
        <apex:repeat value="{!$ObjectType.Contact.FieldSets.properNames}" var="f">
        <apex:column value="{!con[f]}"/>
        </apex:repeat>
        </apex:pageBlockTable>
   </apex:pageBlockSection>
   
   </apex:pageBlock>
   
 </apex:form>
 
 
</apex:page>

 Extension Controller:

public with sharing class fieldSetExtension {

    public Contact ContactForAcc {get;set;} 
    public fieldSetExtension(ApexPages.StandardController controller) {
         ContactForAcc = new Contact();
        lstCon = new List<Contact>();
         fields = new List<Schema.FieldSetMember>();
    }
    
  
  public String AccountId {get;set;}  
  Public List<Contact> lstCon {get;set;}
  Public List<Schema.FieldSetMember> fields =
     Schema.SObjectType.Contact.fieldSets.properNames.getFields();
    
    
  public PageReference redirectUser(){
   AccountId  = ContactForAcc.AccountId;
   
        if(AccountId != null)
        {
            string query = 'select';
            for(Schema.fieldSetMember f : fields){
                query += f.getFieldPath() + ', ';
            }
             query += 'id, Name from Contact where Accountid =: AccountId';
             lstCon = Database.query(query); 
        }
        
        return null;
    }
}

 Thank you so much!

Best Answer chosen by Admin (Salesforce Developers) 
noobfnnoobfn

rubber duck effect, here is the solution:

 

VP:

<apex:page standardController="Account" extensions="fieldSetExtension">


 <apex:form >
  <apex:pageBlock >
     Enter Account Id:
   <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
        <apex:inputText value="{!ContactForAcc.AccountId}" />
        <apex:commandbutton value="Submit" action="{!redirectUser}"/>
        </apex:pageBlockSectionItem>
   </apex:pageBlockSection>
     
 
   <apex:pageBlockSection columns="1" id="contactTable" title="Contats Details">
        <apex:pageBlockTable value="{!lstCon}" var="con">
        <apex:repeat value="{!$ObjectType.Contact.FieldSets.properNames}" var="f">
        <apex:column value="{!con[f]}"/>
        </apex:repeat>
        </apex:pageBlockTable>
   </apex:pageBlockSection>
   
   </apex:pageBlock>
   
 </apex:form>
 
 
</apex:page>

 Controller: 

public with sharing class fieldSetExtension {

    public Contact ContactForAcc {get;set;} 
    public fieldSetExtension(ApexPages.StandardController controller) {
         ContactForAcc = new Contact();
        lstCon = new List<Contact>();
    }
    
  
  public String AccountId {get;set;}  
  Public List<Contact> lstCon {get;set;}
    
    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Contact.FieldSets.properNames.getFields();
    }
    
  public PageReference redirectUser(){
   AccountId  = ContactForAcc.AccountId;
   
        if(AccountId != null)
        {
            String query= 'Select ';
            for(Schema.FieldSetMember f : this.getFields()) {
            query+= f.getFieldPath() + ', ';
            }
            query+= 'id from Contact where Accountid =: AccountId';
            lstcon = Database.query(query);
        }
        
        return null;
    }
}

 

Regards,

NoobFN