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
sathishsfdcsathishsfdc 

fetch the attachments related to custom object

I m getting error for this.Basically i am populating an value in the Texbox and querying the searchstring value in the apex class.Can someoneone help me.I am getting invalid conversion error.How to pass the searchstring value into the soql query??
vfpage::

<apex:page controller ="documentSearch">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  
     <apex:column >  
       </apex:column>  
     </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>  


class::


public with sharing class documentSearch { 

 
   public list <Document__c> acc {get;set;}  
   public string searchstring {get;set;}  
    
     public documentSearch () { }
    
     public void search(){  
       string query = 'select id,name,description  from attachment where parentID =:searchstring';
        system.debug('query ' + query ); 
       
       acc= Database.query(query);  
       
       
   }

THanks

 
Best Answer chosen by sathishsfdc
sandeep sankhlasandeep sankhla
Hi Sathish,

Just copy paste the below page and class to see how it works..

page:
<apex:page controller="TestClass">
<apex:form > 
 <apex:inputText value="{!searchstring}" label="Input"/>  
  <apex:commandButton value="Search records" action="{!search}"/> 
   <apex:pageBlock title="Search Result"> 
    <apex:pageblockTable value="{!acc}" var="a"> 
     <apex:column >  
     <apex:outputtext value = "{!a.Name}"></apex:outputtext>
       </apex:column> 
       <apex:column > 
     <apex:outputtext value = "{!a.Id}"></apex:outputtext>
       </apex:column> 
       <apex:column > 
     <apex:outputtext value = "{!a.Description}"></apex:outputtext>
       </apex:column> 
     </apex:pageBlockTable>     
   </apex:pageBlock>  
  </apex:form> 

</apex:page>

Class:

public with sharing class TestClass {

   public list <attachment> acc {get;set;} 
   public string searchstring {get;set;} 
     
     public TestClass () { 
     acc = new list<attachment>();
         searchstring = '';}
     
     public void search(){ 
         
         system.debug('===searchstring===='+searchstring);
       string query = 'select id,name,description  from attachment where Name =:  searchstring';
        system.debug('query ' + query );
        system.debug('=======Database.query(query)========='+Database.query(query));
       acc= Database.query(query);  
        
        
   }

}

In this if you will put a correct attachment name and hit search , then it will find the attachment and show the details on your VF page...

Now if you want to restrict to search only attachment related to Document__c object then you should provide the object Id..

Please try above code and let me know if you hve any question

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Sathish,

FFrom where you are launching this VF page ?? because you need to first pass treh ParentId, ParentID will contain the Documnent__c object Id ..
In this case your search string is a simple text on a parentId....
let me know from where this page will launch then if we can pass the Docemnt__c id and then searchString will seimply match the Name..

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi Sathish,

Just copy paste the below page and class to see how it works..

page:
<apex:page controller="TestClass">
<apex:form > 
 <apex:inputText value="{!searchstring}" label="Input"/>  
  <apex:commandButton value="Search records" action="{!search}"/> 
   <apex:pageBlock title="Search Result"> 
    <apex:pageblockTable value="{!acc}" var="a"> 
     <apex:column >  
     <apex:outputtext value = "{!a.Name}"></apex:outputtext>
       </apex:column> 
       <apex:column > 
     <apex:outputtext value = "{!a.Id}"></apex:outputtext>
       </apex:column> 
       <apex:column > 
     <apex:outputtext value = "{!a.Description}"></apex:outputtext>
       </apex:column> 
     </apex:pageBlockTable>     
   </apex:pageBlock>  
  </apex:form> 

</apex:page>

Class:

public with sharing class TestClass {

   public list <attachment> acc {get;set;} 
   public string searchstring {get;set;} 
     
     public TestClass () { 
     acc = new list<attachment>();
         searchstring = '';}
     
     public void search(){ 
         
         system.debug('===searchstring===='+searchstring);
       string query = 'select id,name,description  from attachment where Name =:  searchstring';
        system.debug('query ' + query );
        system.debug('=======Database.query(query)========='+Database.query(query));
       acc= Database.query(query);  
        
        
   }

}

In this if you will put a correct attachment name and hit search , then it will find the attachment and show the details on your VF page...

Now if you want to restrict to search only attachment related to Document__c object then you should provide the object Id..

Please try above code and let me know if you hve any question

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
sathishsfdcsathishsfdc
HI Sandeep,

Thanks for the reply....i got wat i was trying to do