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
Priyanka7Priyanka7 

I have two queries i need to combine and show in single query using 'like' in soql

Hi All,

I have two queries find it as follows:

String partid;
String proId;
id ProcessId = ApexPages.currentPage().getParameters().get('id');
proId = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].Id;
partid = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].part_id__c ;
att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where  a.parent.Id=:partid ];
attch = [Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where  a.parent.Id=:proId ];
Now I need a result where parent.Id matches with both proId and partid. Please help me!!!

Thanks in advance 


    

Best Answer chosen by Priyanka7
Waqar Hussain SFWaqar Hussain SF
String partid;
String proId;
id ProcessId = ApexPages.currentPage().getParameters().get('id');
proId = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].Id;
partid = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].part_id__c ;

set<Id> Ids = new set<Id>();
Ids.Add(partid);
Ids.Add(proId);
att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where  a.parent.Id IN :Ids ];

try this one

All Answers

VamsiVamsi
Hi,

Please find the below.

FinalQuery = [Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where a.parent.Id=:proId AND a.parent.Id=:partid];

Hope this helps..!!
Priyanka7Priyanka7
Hi Vamsi,
I tried that, its not working! Can you help something with 'Like'??
Waqar Hussain SFWaqar Hussain SF
String partid;
String proId;
id ProcessId = ApexPages.currentPage().getParameters().get('id');
proId = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].Id;
partid = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId].part_id__c ;

set<Id> Ids = new set<Id>();
Ids.Add(partid);
Ids.Add(proId);
att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where  a.parent.Id IN :Ids ];

try this one
This was selected as the best answer
VamsiVamsi
Hi,

Are you getting any error ? What does part_id__c  contains ? I guess it should contain the process_sheet__c record ID right ? 
Priyanka7Priyanka7
Hi Waqar/Vamsi,
Thanks for your response. Its working now!
Sohan Raj GuptaSohan Raj Gupta
Hi Priyanka,

In your shared code you are using 2 SOQL query in line 4 and 5, whereas you can use one SOQL query insted of 2.

Also in line 6 and 7 you are fatching same result with same object field, so you can not use AND cluse in WHERE.

Try my below code and let me know is it working for you or not.
 
String partid;
String proId;
id ProcessId = ApexPages.currentPage().getParameters().get('id');

if(ProcessId !=null){
	Process_Sheet__c processSheetObj = [select id,part__c,part_id__c from process_sheet__c where id=:ProcessId];
	proId = processSheetObj.Id;
	partid = processSheetObj.part_id__c;
	
	att=[Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.BodyLength From Attachment a where (a.parent.Id=:proId OR a.parent.Id=:partid)];
}
Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta