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
SSimionSSimion 

Apex Trigger to populate Lookup Relationship

Hi All,

I hope you're all well.

I have a custom object - Object 1 and the Opportunity object. I've created a lookup field from the Opportunity to Object 1 and I want to be able to populate the id of a related record into the lookup field, in order to reference some of the values, back on the Opportunity.

The related records on the Object 1 are being created first (as part of an integration), so I want my trigger to fire after the records are created, or modified.

One Opportunity, could have 1+ related records.

In order to differentiate for each Opportunity, those records are related to, we have two custom fields, field 1 on the Opportunity that contains an unquie number and another custom field on the Object 1 that contains the same unquie number the Opp has.

I want my trigger to fire everytime the related records are created and populate the id of one of them into the lookup, so I can reference different values from the Object 1.

This is what I have attempted to build so far, but I am not having much success... so a little bit of help, it's much appreciated. 

I built the trigger on the custom object 1, but I am not sure if  this is correct...
 
ttrigger UpdateLookup on Object_1__c (after insert, after update){
for (Object_1__c obj: trigger.new){
    Obj1Ids.add(obj.id);
}

Map <Opportunity> matchingIdsMap = new Map <Opportunity> ();

for (Opportunity obj: [Select Id, field_1__c from Opportunity, where id in:ObjIds]){
matchingIdsMap.put(Opportunity.id, Opportunity);
}

List <Opportunity>OpportunitiesToUpdate = new List <Opportunity> ();

for (Object_1__c obj: trigger.new){

if (matchingIdsMap.get(obj.id) ! = null)



{

   matchingIdsMap.get(obj.id).Object_1__C = ObjIds;

   OpportunitiesToUpdate.add (matchingIdsMap.get(ObjIds));

}

update OpportunitiesToUpdate;
}

Thanks a lot.
 
Ajay K DubediAjay K Dubedi
Hi,

* Please try below Code:
 
trigger UpdateLookup on Object_1__c (after insert, after update)
{
    set<id> Obj1Ids = new set<id>();
    for (Object_1__c obj: trigger.new)
    {
        Obj1Ids.add(obj.id);
    }
    Map <id,Opportunity> matchingIdsMap = new Map <id,Opportunity> ();
    for (Opportunity obj: [Select Id, field_1__c from Opportunity where field_1__c in:Obj1Ids])
    {
        matchingIdsMap.put(obj.field_1__c, obj);
    }
    List <Opportunity>OpportunitiesToUpdate = new List <Opportunity> ();
    for (Object_1__c obj: trigger.new)
    {
        if(matchingIdsMap.get(obj.id) != Null)    
        {
            matchingIdsMap.get(obj.id).Object_1__c = obj.id;
            OpportunitiesToUpdate.add(matchingIdsMap.get(obj.id));
        }
        update OpportunitiesToUpdate;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
SSimionSSimion
Hi Ajay,

Thanks a lot for coming back to me. I have managed to save the code successfully, but unfortunately it doesn't populate my lookup field.

Any ideas what it could've gone wrong?

Thanks a lot for your time.
Deepali KulshresthaDeepali Kulshrestha
Hi Simion,

You can use my trigger code:
 
trigger UpdateLookup on Object_1__c (after insert, after update){
    List<Opportunity> allOpportunities = new List<Opportunity>();
    allOpportunities = [SELECT Id,Name, UniqueField FROM Opportunity];
    
    //map of that unique key and Obj object Id
    Map<Integer,Object_1__c>  objMap = new Map<Integer,Object_1__c>();
    for(Object_1__c obj : Trigger.new){
        if(Object_1__c.UniqueField != null)
            objMap.put(obj.UniqueField, obj.Id);
    }
    
List<Opportunity> OpportunitiesToUpdate = new List<Opportunity>();
    for(Opportunity opp : allOpportunities){
        if(objMap.containsKey(opp.UniqueField)){
            opp.Object_1__c = objMap.get(opp.UniqueField);
            OpportunitiesToUpdate.add(opp);
        }
    }

update OpportunitiesToUpdate;
}


Just replace a few keywords here:
UniqueField: with the name of the unique field with which you are comparing both the records.
Object_1__c : Your Object Name
also, change the datatype of Key if your unique field is not a number.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
SSimionSSimion
Hi Deepali,

Thanks for coming back to me and sharing the code with me. Much appreciated.

I have two fields'values I am trying to compare. I have field 1 on the Opportunity and field 2 on the Object 1. If these two fields have the same value, then I want to populate the lookup field with the id of one of the related records.

I was just reading that SOQL is limited when it comes to compare two fields in a query, so I should be creating a formula field.

P.S. I have tried saving you code, but I am getting error message 
Error: Compile Error: Method does not exist or incorrect signature: void put(String, Id) from the type Map<Integer,

Thanks.
Deepali KulshresthaDeepali Kulshrestha
Hi SSimion,

I have gone through the error. You are getting this error because maybe you are trying to insert a string value in the map instead of Integer.

Just change the map with this:

Map<String,Object_1__c>  objMap = new Map<String,Object_1__c>();

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
SSimionSSimion
Hi Deepali,

Thank you for coming back to me. I've amended the code with your suggestion and I've still been unable to save it.

I have also tried to create the trigger from the Opportunity, rather than the custom object (Object_1__c), but still unable to save.
 
"Variable does not exist:Opportunity"
"Method does not exist or incorrect signature: void put (Strind, Id) from the type Map.

Esentially what I am trying to achieve is to query if:


The value of Field 1 on Opportunity = The value of Field 2 on Custom Object, then populate the lookup field with Id of one of the related records on the Custom Object.

Field 1 and Field 2 will always have numeric values.

So if field 1 on Opportunity = 123, then check if field 2 on the custom object = 123 (same value), then populate lookup field field 3 (from Opportunity to Custom Object) = with the id of one of the related records from the custom object. For one Opportunity, there is more than one related record on the custom object.


Thanks a lot for all your help so far.