I have two lists for two different objects. They are both are related to opportunities.
I want to check to see if the two lists are associated with the same opportunity and, if they are associated with the same opportunity, populate a field on one object, with a field from the other object.
List<abacus__Ledger_Entry__c> ledgList2 =
[SELECT abacus__Opportunity__c, abacus__GL_Code__c,
EF_GL_Code__c
FROM abacus__Ledger_Entry__c
WHERE abacus__Opportunity__c IN :oppIds AND
(abacus__Type__c =: 'Allocation' AND
abacus__Debit_Credit__c =: 'Credit')];
List<npsp__Allocation__c> alloList1 =
[SELECT Name, GAU_Account_Structure__c,
npsp__Opportunity__c
FROM npsp__Allocation__c
WHERE npsp__Opportunity__c IN :oppIds]
I think a map is the best way to do this maybe? But I'm not familiar with implementing those.
The below is the idea of logic I was going to try. I think it is definitely not the right way to go because I have nested for loops.
if(!ledgList2.isEmpty()){
List<abacus__Ledger_Entry__c> updateLedgList2 = new List<abacus__Ledger_Entry__c>();
List<npsp__Allocation__c> updateAlloList1 = new List<npsp__Allocation__c>();
for(npsp__Allocation__c allo1 : alloList1){
for(abacus__Ledger_Entry__c ledg2 : ledgList2){
ledg2.EF_GL_Code__c = allo1.GAU_Account_Structure__c;
updateLedgList2.add(ledg2);
}
if(!updateLedgList2.isEmpty()){
update updateLedgList2;
I also am going to run into the issue of there being 1 opportunity with 2 Allocations and 2 Ledger Entries. In this case it would not know which Allocation field to pull my field from. Please let me know if you have any suggestions. Thanks!