1

I'm looking to retrieve the string value for the Inventory_Stage__c field in the following logic. The code is incomplete, but I'll post the relevant stuff of what I have. Any help in doing this would be greatly appreciated. Thanks in advance! I'll explain what I think is happening here - I'm a bit new to this so if my logic is wrong here, please advise.

Here I'm building a map of all the Transaction__c records with a start date from this week (Renamed to Production Orders or Production Shifts elsewhere). I am setting the key to the Name of the Inventory_Stage__c and the value to the Transaction__c object.

Map<String, Transaction__c> thisWeeksShifts = new Map<String, Transaction__c> ();
for (Transaction__c p : [SELECT Id, Name, Inventory_Stage__r.Name FROM Transaction__c WHERE Shift_Start__c = THIS_WEEK]) {
    String p_name = p.Inventory_Stage__c;
    thisWeeksShifts.put(p_name, p);
}  

And here I'm trying to loop over all the Inventory Transactions in the trigger and get the Production Shift for which the Inventory_Stage__c in the Production Shift (Transaction__c) matches that of the Inventory_Stage__c in the Inventory Transaction so I can assign it as the parent in the new record I'm creating. I'll also eventually want to assign the Inventory_Stage__c of the cloned inventory transaction to the inventory stage of the current parent Transction__c object.

for(Inventory_Transaction__c invTrans : Trigger.new) {
    if (invTrans.Type__c == 'Out') {
        SObject nextProductionOrder = (String) thisWeeksShifts.get(invTrans.getSobject('Production_Shift__r').get('Inventory_Stage__c'));
        system.debug(thisWeeksShifts.size());
        Inventory_Transaction__c invTransNew = invTrans.clone(false, true, false, false);
        invTransNew.Type__c = 'In';
        toClone.add(invTransNew);
        }
    }
  • What if you change getsObject('Production_Shift__r') to getsObject('Production_Shift__c') or if the Production_Shift is a detail record try getsObjects('Production_Shift__r').....Also what is the value of your debug, if the initial get was not working you should be getting a DNO error – Eric Oct 24 '14 at 5:10
  • Tried that. The production shift object is a parent object. – Zachery Tapp Oct 24 '14 at 5:11
  • what is the value if you debug: invTrans.getSobject('Production_Shift__r').get('Inventory_Stage__c') – Eric Oct 24 '14 at 5:26
1

Ok, someone will correct me if I am wrong as I do not use that method very often and I can only get it to work when including the __r.Name in the query which the trigger does not have. I do not think you can use it in this scenario.

You are going to have to get a Set for all the Production_Shift__c and the create a map to use for the values

i.e.

Set<ID> parentIDs = New Set<ID>();
for(Inventory_Transaction__c invTrans : Trigger.new) {
    if(invTrans.Production_Shift__c != null) parentIDs.add(invTrans. Production_Shift__c);
}

Map<ID,Production_Shift__c> shitMap = New Map<ID,Production_Shift__c>([Select ID, OtherFields From Production_Shift__c Where ID IN :parentIDs]);

Then cycle through the trigger records again and use the map to get your values

   for(Inventory_Transaction__c invTrans : Trigger.new) {

     If(shiftMap.containsKey(invTrans.Production_Shift__c){ //If is probably not necessary as they should be there
             //Do your stuff
     }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.