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
Padmanabhan KamuniPadmanabhan Kamuni 

how extract List values in Set Collection & display SET elements in VF page

Controller class.

 public class SetVFPage {
   public Set<Group__c>acc { get; set; }
     public string keyword{get;set;}
     List<Group__c > accList{get;set;}
      Set<Group__c > accSET = new Set<Group__c >();                
     public pagereference Search(){    
       accList =[SELECT Name, id,Course_Name__c FROM Group__c where Name Like: '%'+keyword+'%'];
       for(Group_c gp : accList){
       gp.name;
       }         
           return null;
     }
}

VF Page
<apex:page controller="SetVFPage ">
    <apex:form >
        <apex:pageBlock >
            <!--<apex:inputField value="{!keyword}"/>-->
            <apex:commandButton value="Search" action="{!Search}" />
            <apex:pageBlockTable value="{!acc}" var="s">
                <apex:column value="{!s.name}"/>
                <apex:column value="{!s.Course_Name__c} "/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Error: Task08 Compile Error: Invalid type: Group_c at line 12 column 12
Request your comments for solution.
Thanks
Best Answer chosen by Padmanabhan Kamuni
Suraj TripathiSuraj Tripathi
Hi Padmanabhan Kamuni,

There are some mistake in your code i have fixed those mistake now it is searching the records from object Group__c 
please refer the following Code. 

Controller:
public class SetVFPage {
     public Set<Group__c>acc { get; set; }
     public string keyword{get;set;}
     public List<Group__c> accList{get;set;}
      Set<Group__c> accSET = new Set<Group__c>();                
     public pagereference Search(){    
       accList =[SELECT Name, id,Course_Name__c FROM Group__c where Name Like: '%'+keyword+'%'];
       for(Group__c gp : accList){
             accSET.add(gp);
       }       
         acc = accSET;
           return null;
     }

}
Visualforce Page:
<apex:page controller="SetVFPage">
    <apex:form >
        <apex:pageBlock >
            <apex:inputText value="{!keyword}"/>
            <apex:commandButton value="Search" action="{!Search}" />
            <apex:pageBlockTable value="{!accList}" var="s">
                <apex:column value="{!s.Name}"/>
                <apex:column value="{!s.Course_Name__c} "/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Regards,
Suraj​

All Answers

Alisha Mehta 9Alisha Mehta 9
Hi,
Syntax error. It's Group__c and not Group_c.
Suraj TripathiSuraj Tripathi
Hi Padmanabhan Kamuni,

There are some mistake in your code i have fixed those mistake now it is searching the records from object Group__c 
please refer the following Code. 

Controller:
public class SetVFPage {
     public Set<Group__c>acc { get; set; }
     public string keyword{get;set;}
     public List<Group__c> accList{get;set;}
      Set<Group__c> accSET = new Set<Group__c>();                
     public pagereference Search(){    
       accList =[SELECT Name, id,Course_Name__c FROM Group__c where Name Like: '%'+keyword+'%'];
       for(Group__c gp : accList){
             accSET.add(gp);
       }       
         acc = accSET;
           return null;
     }

}
Visualforce Page:
<apex:page controller="SetVFPage">
    <apex:form >
        <apex:pageBlock >
            <apex:inputText value="{!keyword}"/>
            <apex:commandButton value="Search" action="{!Search}" />
            <apex:pageBlockTable value="{!accList}" var="s">
                <apex:column value="{!s.Name}"/>
                <apex:column value="{!s.Course_Name__c} "/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Regards,
Suraj​
This was selected as the best answer
Satish PrajapatSatish Prajapat
Hi Padmanabhan Kamuni,
please check code below:
//VF Page
<apex:page controller="SetVFPage">
    <apex:form >
        <apex:pageBlock >
            <apex:inputText value="{!serchKerywords}" />
            <apex:commandButton value="Search" action="{!Search}" reRender="table" />
            <apex:pageBlockTable id="table" value="{!newList}" var="item">
                <apex:column value="{!item.Course_Name__c} "/>
                <apex:column value="{!item.Name} "/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


//Apex Code
public class SetVFPage 
{
    public String serchKerywords {get;set;}
    public List<Group__c> accList{get;set;}
    public List<Group__c> newList{get;set;}
    public Set<Group__c> accSet{get;set;}
    public SetVFPage()
    {
        accSet = new Set<Group__c>();
        newList = new List<Group__c>();
    }
    public void Search()
    {    
        accList =[SELECT id, Name, Course_Name__c FROM Group__c where Name Like: '%'+serchKerywords+'%'];
        accSet.clear();
        newList.clear();
        accSet.addAll(accList);
        newList.addAll(accSet);
    }
}
If this code will help you then,
choose it as a best answer.
thanks:
Satish Kumar Prajapat.
 
Suraj TripathiSuraj Tripathi
Thanks Padmanabhan Kamuni for selecting my answer as a best.
 
Padmanabhan KamuniPadmanabhan Kamuni
Suraj this has solved my problem.. Thanks a Lot
Satish PrajapatSatish Prajapat
Hello Padmanabhan Kamuni,
Suraj Tripathi code is printing the duplicate record. becase its never using Set data on VF page.
please check it.
Thanks.
Padmanabhan KamuniPadmanabhan Kamuni
Hi.Satish Yes, I know but i had modified the code as per the my requirment.
the modified version as below
public class SetVFpage{
     public Set<Group__c>acc { get; set; }
     public string keyword{get;set;}
     public List<Group__c> accList{get;set;}
      public List<Group__c > l{get;set;}
      Set<Group__c> accSET = new Set<Group__c>();                
     public pagereference Search(){    
       accList =[SELECT Name, id,Course_Name__c FROM Group__c where Name Like: '%'+keyword+'%'];
       for(Group__c gp : accList){
             accSET.add(gp);
       }       
        acc = accSET;
       l=new List<Group__c >();
        l.addAll(acc);
           return null;
     }
     public pagereference Save(){
     update l;
     return null;
     
     }
 
<apex:page controller="SetVFpage">
        <apex:form >
        <apex:pageBlock >
            <apex:inputText value="{!keyword}"/>
            <apex:commandButton value="Search" action="{!Search}" />
            <apex:pageBlockTable value="{!l}" var="s">
                <apex:column value="{!s.Name}"/>
                <apex:column >
                <apex:outputField value="{!s.Course_Name__c} "/>
                </apex:column>
                <apex:inlineEditSupport />
            </apex:pageBlockTable>
            <apex:commandButton value="Save" action="{!Save}" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Anyways thanks for updating me.