+ Start a Discussion
Alex MezaAlex Meza 

How can I create an automatic connection between between two different custom objects based off custom fields.

Hello, I am trying to set up a connection where I can connect two custom objects that have the same custom field values such as first, last name, and/or zipcode. If matching records based off of these custom feilds are found then the connection needs to autopopulate the lookup field that points back to each objecet, which in this case is just the standard Salesforce generated ID of each record in their respective custom object.

Technically, what I want to happen is for there to be a scan of all records in one object and look for any matches based off one or multiple custom fields in another object, after that have an automation that will happen to connect those matching records.

I tired doing this through the process builder, and the workflow but I had no luck, I was then told by techical support that Apex Trigger would be the only way to do this. 

Please help, also If I am unclear please let me know. 

Thanks. 
Best Answer chosen by Alex Meza
rajat Maheshwari 6rajat Maheshwari 6

Hi Alex,

Requirement :  -

 To connect two objects records on basis of custom fields (which you have specified).

Solution : - 

Let's assume there is two custom object (FirstObj) and (SecondObj)

Rightnow, I am doing Trigger context in update operaton,

trigger firstObjectTrigger on firstObj (before insert,before update,after insert,after update)
 {
    Set<String> set_Str = new Set<string>();
    Map<String,SecondObject> mp_SecondObj;

     if(Trigger.isAfter && Trigger.isUpdate)
         {
              for(SecondObject secondObj : [Select Id,FirstName,LastName,ZipCode,lookupField  from SecondObject])
                    {
                         if(mp_SecondObj==null)
                               mp_SecondObj = new Map<String,SecondObject>();

                        mp_SecondObj.put(secondObj.FirstName +''+ secondObj.LastName +''+ secondObj.ZipCode,secondObj);


                    }

for(firstObject fstObj : Trigger.new)
    {
        if(mp_SecondObj!=null && mp_SecondObj.containsKey(fstObj.FirstName +''+ fstObj.LastName +''+ fstObj.ZipCode))

{
            
       mp_SecondObj.get(fstObj.FirstName +''+ fstObj.LastName +''+ fstObj.ZipCode).lookupField = fstObj.id;

}

}

if(mp_SecondObj!=null && mp_SecondObj.values()!=null)
          update mp_SecondObj.values();

}
 

This code fetch all second object record, and try to match with newly updated first object fields (Firstname + LastName + ZipCode) and on basis of that it associate secondObject to firstObject by populating firstObject id in lookup field.

 

Note : This code is ideal for unique (Firstname + LastName + ZipCode), means there must not be duplicate combination of these fields. Please let me know the same.

Thanks
 

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Alex,

Requirement :  -

 To connect two objects records on basis of custom fields (which you have specified).

Solution : - 

Let's assume there is two custom object (FirstObj) and (SecondObj)

Rightnow, I am doing Trigger context in update operaton,

trigger firstObjectTrigger on firstObj (before insert,before update,after insert,after update)
 {
    Set<String> set_Str = new Set<string>();
    Map<String,SecondObject> mp_SecondObj;

     if(Trigger.isAfter && Trigger.isUpdate)
         {
              for(SecondObject secondObj : [Select Id,FirstName,LastName,ZipCode,lookupField  from SecondObject])
                    {
                         if(mp_SecondObj==null)
                               mp_SecondObj = new Map<String,SecondObject>();

                        mp_SecondObj.put(secondObj.FirstName +''+ secondObj.LastName +''+ secondObj.ZipCode,secondObj);


                    }

for(firstObject fstObj : Trigger.new)
    {
        if(mp_SecondObj!=null && mp_SecondObj.containsKey(fstObj.FirstName +''+ fstObj.LastName +''+ fstObj.ZipCode))

{
            
       mp_SecondObj.get(fstObj.FirstName +''+ fstObj.LastName +''+ fstObj.ZipCode).lookupField = fstObj.id;

}

}

if(mp_SecondObj!=null && mp_SecondObj.values()!=null)
          update mp_SecondObj.values();

}
 

This code fetch all second object record, and try to match with newly updated first object fields (Firstname + LastName + ZipCode) and on basis of that it associate secondObject to firstObject by populating firstObject id in lookup field.

 

Note : This code is ideal for unique (Firstname + LastName + ZipCode), means there must not be duplicate combination of these fields. Please let me know the same.

Thanks
 

This was selected as the best answer
Alex MezaAlex Meza
Awesome I think I understand, only thing is that I am running into an error on line 13 where it says Incompatible key type Schema.SObjectField for Map<String,Voter_File__c>.  Also for the purpose of just figuring out this inital code and logic i've simplfied it to only look for matches base off first name.

With this my first custom objects= TGA Email List, and my second is=Voter File

Where the lookup field I am trying to update is Voter_ID_del__c on the TGA Email List custom object, where Voter_ID_del__c is the salesforce generated id record for a record in the Voter File custom object. 

Also note because realtionships between these two custom objects should be one to one, there is a lookup field on the Voter File object that reads Email_ID_del__c which is simply the Unique salesforce generated id record of the TGA Emai List Record


Can you take a look at the code and tell me where I went wrong. 
trigger EmailtoVoterFile on TGA_Email_List__c (before insert,before update,after insert,after update)
{
	Set<String> set_Str = new Set<string>();
    Map<String,Voter_File__c> mp_voterfileObj;
    
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        for(Voter_File__c voterfileobj : [Select ID,First_Name__c,Email_ID_del__c From Voter_File__c])
        {
            		if(mp_VoterfileObj==null)
                        mp_VoterFileObj = new map<String,Voter_File__c>();
        
            mp_VoterFileObj.put(Voter_File__c.First_Name__c,VoterFileObj);

 
                    }
        
        for(TGA_Email_List__c TGAEmailListobj : Trigger.new)
    {
        if(mp_VoterFileObj!=null && mp_Voter_File__c.containsKey(TGA_Email_List__c.First_Name__c))

        {

       mp_Voter_File__c.get(TGA_Email_List__c.First_Name__c).Voter_ID_del__c = Voter_File__c.id;

}

}
 
if(mp_VoterFileObj!=null && mp_Voter_File__c.values()!=null)
		update mp_Voter_File__c.values();

    }
}

Also this goes without saying but thank you so much for you help.