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
jojoforcejojoforce 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
jojoforcejojoforce

 

. 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. 

 

Map<String, Object> o1
Map<String, Object> o2



o1.put(key1, ObjectOneRecord);
o1.put(key2, ObjectOneRecord);

o2.put(key1, ObjecTwotRecord);
o2.put(key2, ObjectTwoRecord);

 



 

Something like that.. and then for calling them I used something like the below to be able to display the record\

 

for(Object3 obj : select id, key, name from Object3) { 

o1[obj.key].Field1
o2[obj.key].Field2

}

All Answers

AmitSahuAmitSahu

What is the issue here ? This query should work fine.

jojoforcejojoforce

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.

AmitSahuAmitSahu

Ok got it.. You are right.

SELECT o1.id, o1.Name, o1.Status, o2.Area FROM Object_1__c o1, Object_2__c o2 WHERE o1.Name = o2.Name

The above query will not work as we are not providing any value for o2.Name. Can we use Junction object for this ?

jojoforcejojoforce

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.

vagishvagish

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;

jojoforcejojoforce

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

 



SELECT o1.id, o1.Name, o1.Status, o2.Area FROM Object_1__c o1, Object_2__c o2 WHERE o1.Name = o2.Name

 

jojoforcejojoforce

 

. 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. 

 

Map<String, Object> o1
Map<String, Object> o2



o1.put(key1, ObjectOneRecord);
o1.put(key2, ObjectOneRecord);

o2.put(key1, ObjecTwotRecord);
o2.put(key2, ObjectTwoRecord);

 



 

Something like that.. and then for calling them I used something like the below to be able to display the record\

 

for(Object3 obj : select id, key, name from Object3) { 

o1[obj.key].Field1
o2[obj.key].Field2

}
This was selected as the best answer
PreeSFDCDevPreeSFDCDev
Hi JojoForce,

I have a similar requirement. Could you please post your complete solution how you have acheived this.
It will be helpful
Chitranjan Chetkar 7Chitranjan Chetkar 7
Hi Jojo,

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
Chitranjan Chetkar 7Chitranjan Chetkar 7
Hi Jojo,

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
Jotsna GowdaJotsna Gowda
Hi chitranjan...so you used two loops,It is going to get difficult during bulk insert.Is there any other way