+ Start a Discussion
Nirmal ChristopherNirmal Christopher 

Issue with database.query() in attachment object?

Im unable to save this code but it works well with contact or account sobject the error im getting is "attcon Compile Error: Dynamic SOQL loop variable must be an SObject or SObject list (concrete or generic) at line 27 column 32" Did i miss anything here?

public pagereference search(){
        if(searchResults==null){
        searchResults=new list<AttachmentWrapper>();//initiate searchresults if the value is null
            }
        else{
        searchResults.clear();//clear out the current results if the values pre exist
            }
        //dynamic SOQL query to fetch the attachments
                String qry = 'Select Name, Id From attachment Where Name LIKE \'%'+searchText+'%\' Order By Name';
                for(attachment att:database.query(qry)){
               
                }

    return null;
    }
Best Answer chosen by Nirmal Christopher
Nirmal ChristopherNirmal Christopher
Hi Piyush, The error was caused because some one has used a class name as Attachment  previously in the org. So whihle im trying to use the attachement object the compiler was giving error that i am trying to call the instance of the class "attachment". I deleted the old class in the system and the problem was resolved.

All Answers

Sri549Sri549
Hello Nirmal,
Could you please paste your complete code,so that i may help you.

Thanks
Srinivas
Nirmal ChristopherNirmal Christopher
public with sharing class attcon {
    public List<AttachmentWrapper>searchResults{get;set;}
    public string searchText{
      get{
      if (searchText == null)
       searchText = 'Enter a keyword'; // prefill the serach box for ease of use
       return searchText;
        }
    set;
    }
    public attcon(){
   
    }
    public pagereference Search_Attachments(){
   
    return null;
    }
    public pagereference search(){
        if(searchResults==null){
        searchResults=new list<AttachmentWrapper>();//initiate searchresults if the value is null
            }
        else{
        searchResults.clear();//clear out the current results if the values pre exist
            }
        //dynamic SOQL query to fetch the attachments
                String qry = 'Select Name, Id From attachment Where Name LIKE \'%'+searchText+'%\' Order By Name';
                for(account att:database.query(qry)){
               
                }

    return null;
    }
    public class AttachmentWrapper{
      public Boolean checked{get;set;}
      public attachment att{get;set;}
      public AttachmentWrapper(){
            att=  new attachment();
            checked = false;
    }
     public AttachmentWrapper(Attachment a){
            att= a;
            checked = false;
        }
    }
}
piyush parmarpiyush parmar
Hi Nirmal,

Database.query() return sObject or List<sObject>
So you have to use sObject instaed of Account.

You can follow this code.

for (sObject s: database.query(str))
{
Account a = (Account)s;
}

Thanks,
Piyush
Nirmal ChristopherNirmal Christopher
Hi Piyush, i tried like this    
                   for (sObject s: database.query(qry))
                    {
                    attachment a = (attachment)s;
                    }
Im getting the error "Incompatible types since an instance of SObject is never an instance of attachment"...the above code works fine in my developer org onl in the sandbox the error is showed in the forloop

piyush parmarpiyush parmar
Hi Nirmal,

I tried with my intance it is working for Attachment object.

You can run same on developer console,

Sobject s = database.query('select id from attachment limit 1');
Attachment a = (Attachment)s;
System.debug(''+a);


Can you post you code on the same. So I can do refered.

Regards,
Piyush


Nirmal ChristopherNirmal Christopher
Hi Piyush, The error was caused because some one has used a class name as Attachment  previously in the org. So whihle im trying to use the attachement object the compiler was giving error that i am trying to call the instance of the class "attachment". I deleted the old class in the system and the problem was resolved.
This was selected as the best answer