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
Lakshmi Gupta 4Lakshmi Gupta 4 

Get Set method

I'm new to Salesforce and trying to understand Get, Set method.  I have written this simple code, where user can search for a record from custom object Attendance. Set method is not assigning the value to variable "Keyword".  Can someone explain what is the problem with my code?

Thank you

public class simplegetset
{  

    string keyword;
    list <attendance__c> att;
    
    public string getkeyword(){
    return keyword;
    }

    public list <attendance__C> getatt(){
     return att;
    }   
    
    Public void setKeyword(string s){
      keyword = s;
      }
      
     Public pagereference ShowResults(){
     att = [select contact__c, Manager__c, Manager_s_Manager__c,
               Approved__c,Attendance_Date__c, Hours__C,
              Is_Eligible_for_compensatory_off__c, AttendanceType__c FROM Attendance__c where contact__C = :keyword];
     return null;
     }
     
}
============================================================================

<apex:page controller="simplegetset">
  <apex:form >
   <apex:pageblock title="List of Employee Attendance">
   <apex:inputtext value="{!Keyword}"/>
   
   <apex:commandButton value="Show Results" action="{!ShowResults}" rerender="details"/>  
    <apex:pageblockTable value="{!att}" Var="a" id="details">
    <apex:column value="{!a.Contact__c}"/>
    <apex:column value="{!a.Manager__c}"/>
    <apex:column value="{!a.Manager_s_Manager__c}"/>
    <apex:column value="{!a.Approved__c}"/>  
    <apex:column value="{!a.Attendance_Date__c}"/>
    <apex:column value="{!a.Hours__c}"/>
    <apex:column value="{!a.AttendanceType__c }"/>    
   </apex:Pageblocktable>
   
   <apex:outputtext >{!keyword}
   </apex:outputtext> 
    </apex:pageblock>                
  </apex:form>    
</apex:page>

 
Best Answer chosen by Lakshmi Gupta 4
Raj VakatiRaj Vakati
Try this 
 
select contact__c, Manager__c, Manager_s_Manager__c,
               Approved__c,Attendance_Date__c, Hours__C,
              Is_Eligible_for_compensatory_off__c, AttendanceType__c FROM Attendance__c where contact__r.name LIKE :'%'+keyword+'%'

 

All Answers

Raj VakatiRaj Vakati
Your code is working fine but the search logic is returnng only if the name is excat matches 

Update YOUR SOQL to check the like as shown below 
select contact__c, Manager__c, Manager_s_Manager__c,
               Approved__c,Attendance_Date__c, Hours__C,
              Is_Eligible_for_compensatory_off__c, AttendanceType__c FROM Attendance__c where contact__C LIKE :'%'+keyword+'%'


 
Lakshmi Gupta 4Lakshmi Gupta 4
Thanks alot Raj,  I found the exact issue.Basically, Records are created in another custom VF page using a look up on Contact table. When I save the record contact__C is having Contact Id number in Attendance__c object.  So in my SOQL query  where Contact__C is looking for Contact id number. But I want the user to be able to query for name. How can I do this?  

When I remove the where clause and search for all the records in the above  code. Display table is showing the name of the contact. So, I'm not sure how it is working.
Raj VakatiRaj Vakati
Try this 
 
select contact__c, Manager__c, Manager_s_Manager__c,
               Approved__c,Attendance_Date__c, Hours__C,
              Is_Eligible_for_compensatory_off__c, AttendanceType__c FROM Attendance__c where contact__r.name LIKE :'%'+keyword+'%'

 
This was selected as the best answer
Raj VakatiRaj Vakati
select contact__c, Manager__c, Manager_s_Manager__c,
               Approved__c,Attendance_Date__c, Hours__C,
              Is_Eligible_for_compensatory_off__c, AttendanceType__c FROM Attendance__c where contact__c.Name LIKE :'%'+keyword+'%'

 
Lakshmi Gupta 4Lakshmi Gupta 4
Raj,

Thanks a lot !  contact__r.name LIKE :'%'+keyword+'%' works!!