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
Travis Malle 9Travis Malle 9 

create lookup between two unrelated objects


I’m trying to create a lookup between two custom objects were there is a matching account number field. I would like to create a trigger on ObjectA that says: Whenever ObjectA is created or edited, search ObjectB and see if a record with the same account number exists. If a record exists, take the ID from ObjectB and insert it into the matching ObjectA record as a lookup field. I have been at this for a while and am at a loss. I am very new to APEX and any assistance in pointing me in the right direction would be greatly appreciated.
Best Answer chosen by Travis Malle 9
sandeep sankhlasandeep sankhla
Hi Travis,
Please check the below code.
Trigger on ObjectA (before Insert, before Update)
{
    
    set<string> setAccountnumbers = new set<string>();
    map<String, String> mapAccNumberToObjBId = new map<String, String>();
    
    if(Trigger.isInsert){
    
        for(ObjectA obj : Trigger.new)
        {
            setAccountnumbers.add(obj.AccNumber);
        }
        
        for(ObjB objbb : [select id , accNumber from ObjB where accNumber IN: setAccountnumbers])
        {
            mapAccNumberToObjBId.put(objbb.accNumber, objbb.Id);
        }
        
        for(ObjectA obj : Trigger.new)
        {
            if(mapAccNumberToObjBId.containskey(obj.accNumber))
            {
                obj.ObjB__c = mapAccNumberToObjBId.get(obj.accNumber);
            }
        }
    }
    
}



Please refer the above code and replace field names whith your fields and it will work and same you can implement for update as well.

In above code I just collected all accNumberin a set teh I did a query on ObjectB and then I collect account number and related ObjectB in a map then again I iterated over ObjectA list and then checking if accNumberis matching then I am updating the lookup field.

Please check and let me knwo if it helps you.

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks!
Sandeep

All Answers

sandeep sankhlasandeep sankhla
Hi Travis,

So your need is that you have ObjectA and ObjectB. ObjectA is having lookup to ObjectB. ObjectA & ObjectB both are having a field called Account number.

On insert of ObjectA you need to just find the ObjectB where that feidl value is same then populate the ObjectB on ObjectA.

Please confirm the data model so I can help. It is a very simple trigger.
sandeep sankhlasandeep sankhla
Hi Travis,
Please check the below code.
Trigger on ObjectA (before Insert, before Update)
{
    
    set<string> setAccountnumbers = new set<string>();
    map<String, String> mapAccNumberToObjBId = new map<String, String>();
    
    if(Trigger.isInsert){
    
        for(ObjectA obj : Trigger.new)
        {
            setAccountnumbers.add(obj.AccNumber);
        }
        
        for(ObjB objbb : [select id , accNumber from ObjB where accNumber IN: setAccountnumbers])
        {
            mapAccNumberToObjBId.put(objbb.accNumber, objbb.Id);
        }
        
        for(ObjectA obj : Trigger.new)
        {
            if(mapAccNumberToObjBId.containskey(obj.accNumber))
            {
                obj.ObjB__c = mapAccNumberToObjBId.get(obj.accNumber);
            }
        }
    }
    
}



Please refer the above code and replace field names whith your fields and it will work and same you can implement for update as well.

In above code I just collected all accNumberin a set teh I did a query on ObjectB and then I collect account number and related ObjectB in a map then again I iterated over ObjectA list and then checking if accNumberis matching then I am updating the lookup field.

Please check and let me knwo if it helps you.

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks!
Sandeep
This was selected as the best answer
Travis Malle 9Travis Malle 9
Hello Sandeep,
Thank you for your quick reply and for helping me through this issue. I tried the code you provided; it compiled without error but did not work as expected. I added an update DML operation thinking that was the issue but to no avail. Here is the code I have so far. Also I added a field “Missing_Info_Errors_Link__c” this is a lookup field the Missing_info_Errors__c object (object B). here is the code I have so far (not working)
 
Trigger matchacctnumbers on audit__c (before Insert, before Update){
    
    set<string> setAccountnumbers = new set<string>();
    map<string, string> mapAccNumberToMissId = new map<string, String>();
    
    if(Trigger.isInsert){
    
        for(audit__c aud : Trigger.new){
            setAccountnumbers.add(aud.name);
        }
        
        for(Missing_Info_Errors__c miss : [select id , Account_Number__c from Missing_Info_Errors__c where Account_Number__c IN: setAccountnumbers]){
            mapAccNumberToMissId.put(miss.Account_Number__c, miss.id);
        }
        
        for(audit__c aud : Trigger.new){
            if(mapAccNumberToMissId.containskey(aud.name)){
                aud.Missing_Info_Errors_link__c = mapAccNumberToMissId.get(aud.name);
                
            }
            update aud;
        }
    }
    
}

Thank you again for your time and effort
sandeep sankhlasandeep sankhla
Hi

Update statment is not required as it is before update. 

You are adding name at line no 9, there it should be number instead of name. because your are finding based on account number.

Thanks!
Travis Malle 9Travis Malle 9
Sandeep,

got it working, i endedup commenting out the if(trigger.isinsert clause. I was testing by updating my records