0

I have the following configuration:

I have a trigger in which I am trying to add an Inventory record when a treatment gets created. This inventory record requires an order ID as it has a master detail relationship to order. This treatment record has a lookup to the Visit Object which in turn has a lookup to a Work Order object which in turn has a lookup to the Order object. The inventory record itself is not directly related to the treatment record that is creating it - it is only by the order id. In simple form.

Inventory - child (what I am trying to insert via the trigger) Treatments - parent Visits - grand parent Work Order - great grand parent Orders - great great grand parent (Order ID from inventory comes from here)

Essentially I have a simple trigger as follows, ran on after insert via a trigger handler. It executes when i expect:

Apex code

public class TreatmentsActions {
    public static void createTreatmentInventory(List<Treatments__c> NewTreatments){     

        List<Inventory__c> InventoryToCreate = new list<Inventory__c>{};        

        for(Treatments__c t: NewTreatments) {
            InventoryToCreate.add(new Inventory__c( Product_InventoryLookup__c = t.product_treatmentlookup__c,
                                                   Quantity__c = t.Quantity__c * -1, Order__c = t.Visit__r.Work_Order__r.Order__r.Id ));
        }

        try {
                insert InventoryToCreate;
            }
            catch(DMLException e){
                System.debug('--------------Exception in createTreatmentInventory');
                System.debug('Exception occurred '+String.valueOf(e));
            }
  }   

}

As you probably expect the t.Visit__r.Work_Order__r.Order__r.Id and t.product_treatmentlookup__c are null. The record is not inserted.

I currently "think" it is down to context, the treatment record has id values for the visit and product fields but if I try and use system.debug to access the related fields (visits__r.work_order__r.order__r.id etc) I just receive nulls.

Edit: if I run the following SOQL "select visit__r.work_order__r.order__r.id FROM Treatments__c" it returns [object object] which maybe a console bug!?

Edit: I have actually been able to make a really simple Flow in the Process Builder that worked 2nd time. I'd still like to know where I went wrong above but interesting how easy it was in the process builder :(

1 Answer 1

1

Your code is working as expected nothing is bug here.

In the trigger context we can't access parent fields. If you want to access parent fields then you need to query them in your trigger handler.

Also when you run this query then Salesforce instead of showing parent Id it display [Object Object] but you are getting value. If you want to see the value you can run this query in Execute Anonymous window (ALT + E) where you can see the result.

Also you don'te need __r.Id select visit__r.work_order__r.order__c FROM Treatments__c this will also work.

You need query something like

for(Treatments__c t: [select visit__r.work_order__r.order__c,product_treatmentlookup__c,Quantity__c  FROM Treatments__c WHERE ID IN: NewTreatments}) {
            InventoryToCreate.add(new Inventory__c( Product_InventoryLookup__c = t.product_treatmentlookup__c,
                                                   Quantity__c = t.Quantity__c * -1, Order__c = t.Visit__r.Work_Order__r.Order__r.Id ));
        }//Also you want to check null here else you will get exceptionQuantity__c 
1
  • 1
    I'm really annoyed at myself. Ive done exactly the above before just had a mind meltdown and complete forget. That worked so many thanks for the help.
    – n34_panda
    Commented Oct 17, 2017 at 16:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .