You need to sign in to do that
Don't have an account?

Join 2 unrelated objects based on field? Wrapper Class?
Hi,
I have two unrelated objects where a specific column might have the same value in another object. I'm trying to find out if a value in an object, already exist in another object. I was reading up on a wrapper classes, but I couldn't get my head around it. Any idea how this is possible?
For example I have the following two objects with the following fields
Object_1__c (id, Name, Status)
Object_2__c(id, Name, Area)
In SQL, it would be written something like below. Please NOTE that there are fields that need to be displayed from both objects.
SELECT o1.id, o1.Name, o1.Status, o2.Area FROM Object_1__c o1, Object_2__c o2 WHERE o1.Name = o2.Name
. I was able to get this to work though. I used the Map<String, Object> and basically used the Key Value as their join condition.
Please note: This is just partial coding... but you get the idea.
Something like that.. and then for calling them I used something like the below to be able to display the record\
All Answers
What is the issue here ? This query should work fine.
That query will not work fine with SOQL. It is my understanding that you cannot JOIN two unrelated objects. I'm trying to figure out if there's a way to join two unrelated object based on a common data field.
Ok got it.. You are right.
The above query will not work as we are not providing any value for o2.Name. Can we use Junction object for this ?
I was thinking this might be possible with a Wrapper class, but I'm having difficulty understanding how to do it with Wrapper class in Apex classes.
I think this would be helpful:
List<Obj2__c> l = [select name from Obj2__c];
List<String> s = new List<String>();
for(Obj2__c f : l){
s.add(f.name);
}
Select <specify your list of fields in object1> from Obj1__c where name in : s;
Thanks.. However, there are fields that need to be displayed from object 2 as well though.
SELECT o1.id, o1.Name, o1.Status, o2.Area FROM Object_1__c o1, Object_2__c o2 WHERE o1.Name = o2.Name
. I was able to get this to work though. I used the Map<String, Object> and basically used the Key Value as their join condition.
Please note: This is just partial coding... but you get the idea.
Something like that.. and then for calling them I used something like the below to be able to display the record\
I have a similar requirement. Could you please post your complete solution how you have acheived this.
It will be helpful
below solution works without wrapper.
Public List<ProcessInstance> ProcessInstanceList;
Public List<WF_Request__c> RequestList;
ProcessInstanceList = [SELECT Id, ProcessDefinitionID, ProcessDefinition.name, TargetObjectId, TargetObject.name, status, CreatedDate, (SELECT actor.name FROM Workitems) FROM ProcessInstance where status = 'pending' and ProcessDefinition.name like 'APM%' LIMIT 100];
RequestList = [SELECT Id, Name, RecordTypeId, RecordType.Name, CreatedDate, Company_Code__c, Salesforce_ID__c, Workflow_number__c, Workflow_Name__c FROM WF_Request__c where Workflow_Name__c like '%AP Monitor%' LIMIT 100];
List<Sobject> Combinedrecord=new List<Sobject>();
//return CombinedItems;
for ( ProcessInstance pi : ProcessInstanceList) {
for(WF_Request__c wr : RequestList) {
if(pi.TargetObjectId == wr.id) {
Combinedrecord.add(pi);
Combinedrecord.add(wr);
}
}
}
system.debug('CombinedItems'+Combinedrecord);
Best Regards,
Chitranjan
Best Regards,
Chitranjan