I have the following code that works for me. My main concern is the nested for-loops I have toward the end of the code. I'm in the sandbox and don't have enough records to bulk test.
I figured I'd ask you experts whether this is a problem or a violation of a "best practice"?
Thanks in advance.
Update: Structure and goal of code
I am trying to create a record using information from 4 separate objects that have various relationship structures, overview below:
- Location: this is a "parent" object in the lookups/MD's for every other object in this scenario
- Receipt: where the trigger originates. This contains a LU to a "Location" record.
- Inventory Lots: This is a child in LU Relationship to "Location". I'm checking to see if one exists for the location specified on the receipt, if not, create one
- Storage Metric: This is the Child in a MD relationship to "Location"
- FG_Transaction: This is the record I'm wanting to create. It takes information from the Receipt and has lookups to Location, Receipt, Inventory_Lots and Storage Metric
This class is the helper to my unified trigger on the Receipt object.
public static void createICFG(Map<Id,Id> RecIds){
List<Inventory_Lots__c> icList = new List<Inventory_Lots__c>();
List<FG_Transactions__c> fgList = new List<FG_Transactions__c>();
RecordType rt = [SELECT Id from RecordType WHERE Name = 'Bulk Batch' AND sObjectType = 'Inventory_Lots__c'];
RecordType rt2 = [SELECT Id from RecordType WHERE Name = 'Receipt' AND sObjectType = 'FG_Transactions__c'];
Product_Sesaco__c prd = [SELECT Id from Product_Sesaco__c WHERE Name = 'Commercial, Field Run'];
Set<Id> icIds = new Set<Id>();
icList = [SELECT Id, Location__c, Closed__c, On_Hold__c from Inventory_Lots__c WHERE Location__c IN :RecIds.Keyset() AND Closed__c != TRUE AND On_Hold__c != TRUE];
System.debug('---------------------icList-----------------------------'+icList);
for(Inventory_Lots__c ic : icList){
icIds.add(ic.Location__c);
}
if(icList.isEmpty()){
List<Locations__c> locList = [SELECT Id, Name,(SELECT Id, Beginning_Date__c FROM Storage_Metrics__r) from Locations__c WHERE Id IN: RecIds.keySet() AND Id NOT IN :icIds];
for(Locations__c l : locList){
Inventory_Lots__c icNew = new Inventory_Lots__c();
icNew.Name = l.Name+':'+' '+system.date.today().year()+'/'+system.date.today().month()+'/'+system.date.today().day();
icNew.Location__c = l.id;
icNew.RecordTypeId = rt.id;
icNew.Products_sesaco__c = prd.id;
icList.add(icNew);
}
insert icList;
}
System.debug('------------------------------------IC List----------------------------------------'+icList);
List<Receiving__c> recList = [SELECT Id, GRLBS__c, Glyphosate_Used__c, Date_Received__c, Locations__c FROM Receiving__c WHERE Id IN :RecIds.values() AND Locations__c IN :RecIds.keySet()];
System.debug('-----------------------------------Rec List---------------------------------------'+RecList);
List<Locations__c> locList = [SELECT Id, Name, (SELECT Id, Beginning_Date__c FROM Storage_Metrics__r) from Locations__c WHERE Id IN: RecIds.keySet() AND Id IN :icIds];
for(Locations__c l : locList){
for(Receiving__c r : recList){
for(Inventory_Lots__c ic : icList){
for(Storage_Metric__c sm : l.Storage_Metrics__r){
if(ic.Location__c == r.Locations__c && r.Date_Received__c.toStartOfMonth() == sm.Beginning_Date__c){
FG_Transactions__c fgNew = new FG_Transactions__c();
fgNew.Receiving_Number__c = r.id;
fgNew.FG_Lots__c = ic.id;
fgNew.Locations_primary__c = r.Locations__c;
fgNew.Trx_Date__c = r.Date_Received__c;
fgNew.Trx_Qty_Lbs__c = r.GRLBS__c;
fgNew.RecordTypeId = rt2.id;
fgNew.HA_Applied__c = r.Glyphosate_Used__c;
fgNew.Storage_Metric__c = sm.id;
fgList.add(fgNew);
}
}
}
}
}
insert fgList;
}
}