You need to sign in to do that
Don't have an account?
Nirmal 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;
}
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;
}
All Answers
Could you please paste your complete code,so that i may help you.
Thanks
Srinivas
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;
}
}
}
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
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
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