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
Irvine DelacroixIrvine Delacroix 

How to populate fields using Map.

Hi Experts!

I hope you can help me with this.

I'm trying this experiment some apex code
I have ObjectX, ObjectA and ObjectB.
ObjectA and ObjectB are a Lookup in ObjectX.

First is that I create ObjectA, then in Related List when ObjectX is created, I want to have fields updated on ObjectA with the value fields from Object B which is a lookup on ObjectX. It should do something like the image below.

User-added image

I'm able to do it with the below code but it's giving me incorrect records when I update All records in DataLoader.

Not sure if my approach is correct. Please Help. Here's my code.

trigger TestFieldUpdate on ObjectX__c (after insert, after update){

Map<ID, ObjectA__c> ObjectA = new Map<ID, ObjectA__c>();
  Set<Id> Ids = new Set<Id>();
  Set<Id> ObjectBids = new Set<Id>();
    
  for (ObjectX__c ObjX : Trigger.new) {
    Ids.add(ObjX.ObjectA__c);
    ObjectBids.add(ObjX.ObjectB);
  }

  ObjectA = new Map<Id, ObjectA__c>([SELECT id, Field_1__c, Field_2__c,
                                        (SELECT id,ObjectB__r.Field_1__c, ObjectB__r.Field_2__c
                                         FROM ObjectX__c) 
                                         FROM ObjectA__c 
                                         WHERE ID IN :Ids]);
  
  //I'm not sure if this part is correct though

  List<ObjectB__c> ObjectB = [SELECT Id, Field_1__c, Field_2__c FROM ObjectB__c 
                                      WHERE Id =: ObjectBids ];
 
  for (ObjectX__c ObjectX: Trigger.new){
      
      ObjectA ObjA = ObectA.get(ObjectX.ObjectA__c);
      ObjA.Field1__c = ObjectB[0].Field_1__c;
      ObjA.Field1__c = ObjectB[0].Field_2__c;
      //And So on..
    
  }
      
  IF(ObjectB.size()>0) update ObjectB.values();
    
}


Please let me know what's is the correct code to use if my code is a total none sense :(

Thanks a Lot!

P.S. I'm still learning things, so please explain what each lines does :)

 
Best Answer chosen by Irvine Delacroix
BDatlaBDatla
Hi Irvine,

Please use the below code and let me know the result :

  //I'm not sure if this part is correct though

  List<ObjectB__c> ObjectB = [SELECT Id, Field_1__c, Field_2__c,ObjectX__r.Id FROM ObjectB__c 
                                      WHERE Id =: ObjectBids ];
  Map<String ,ObjectB__c > obxMap = new   Map<String ,ObjectB__c >();
for(ObjectB__c ob :ObjectB ){
       obxMap.put(ob.ObjectX__r.Id ,ob);
}
 
  for (ObjectX__c ObjectX: Trigger.new){
      
      ObjectA ObjA = ObectA.get(ObjectX.ObjectA__c);
      ObjA.Field1__c = obxMap.get(ObjectX.Id).Field_1__c;
      ObjA.Field1__c = obxMap.get(ObjectX.Id).Field_2__c;
      //And So on..
    
  }

Regards,
BDatla

 

All Answers

BDatlaBDatla
Hi Irvine,

Please use the below code and let me know the result :

  //I'm not sure if this part is correct though

  List<ObjectB__c> ObjectB = [SELECT Id, Field_1__c, Field_2__c,ObjectX__r.Id FROM ObjectB__c 
                                      WHERE Id =: ObjectBids ];
  Map<String ,ObjectB__c > obxMap = new   Map<String ,ObjectB__c >();
for(ObjectB__c ob :ObjectB ){
       obxMap.put(ob.ObjectX__r.Id ,ob);
}
 
  for (ObjectX__c ObjectX: Trigger.new){
      
      ObjectA ObjA = ObectA.get(ObjectX.ObjectA__c);
      ObjA.Field1__c = obxMap.get(ObjectX.Id).Field_1__c;
      ObjA.Field1__c = obxMap.get(ObjectX.Id).Field_2__c;
      //And So on..
    
  }

Regards,
BDatla

 
This was selected as the best answer
BDatlaBDatla
Hi Irvine,

If the above issue is resolved could you please let me know.

Regards,
BDatla
 
Irvine DelacroixIrvine Delacroix
Hi BDatla,

Thanks you so much for the comment and sorry if I didn't respond soon. I just got time to study Apex again today.  I tried your suggestion and it worked.

You're the best :)