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
John AthitakisJohn Athitakis 

Repost: Creating a List/Updating Records of a Child in a Master-Detail

Earlier Question got burried! Reposting it here.

So I have a bit of code (fully below) that another community member helped me on and it works really well save for one thing I'm trying to suss out. The objects involved are Project (which has a lookup to Opportunity), Opportunity, Sales Invoice (a custom object) and Sales Invoice Line (a custom object that is Master-Detail to Sales Invoice as a child). These were installed as a managed package and the relationship of the child to the parent is the rather strange R00N40000001p914EAA. All this code compiles/no errors/no errors during run time.

What it should do: When a project is created with a link to an opportunity, the opportunty, any sales orders it has and any sales order line items THEY have have a lookup to project populated (with some added functionality if we null out the links). Everything is working until you get to the child in the master-detail, where it seems it is simply not doing anything.
Here is the full code followed by the portion where the problem is occuring. 
 
trigger PSA_Project_Automation on pse__Proj__c (before insert, before update) {
    
  list<id> opportunityIds = new list<id>();
  map<id,id> mOpptyProjIds = new map<id,id>();

  for(pse__Proj__c proj : trigger.new) {
    if(proj.pse__Opportunity__c != null) {
      opportunityIds.add(proj.pse__Opportunity__c);
      mOpptyProjIds.put(proj.pse__Opportunity__c,proj.id);
    }
    if(trigger.isUpdate) {
      if(trigger.oldmap.get(proj.id).pse__opportunity__c != null
          && proj.pse__Opportunity__c == null
         ) {
        opportunityIds.add(trigger.oldmap.get(proj.id).pse__opportunity__c);
        mOpptyProjIds.put(trigger.oldmap.get(proj.id).pse__opportunity__c,null);
      }
    }
  }

  // Get the Opportunities and update them
  list<opportunity> opportunitiesToUpdate = [SELECT id,pse__Primary_Project__c, Primary_Project_Test__c FROM Opportunity WHERE id IN :opportunityIds];

    
  for(opportunity o :opportunitiesToUpdate) {
    if(mOpptyProjIds.containsKey(o.id)) {
      o.pse__Primary_Project__c = mOpptyProjIds.get(o.id);
    }
  }

  if(opportunitiesToUpdate.size()>0) {
    update opportunitiesToUpdate;
  }
  
  list<Sales_Invoice__c> SalesInvoiceToUpdate = [SELECT id, Primary_Project__c, Opportunity__c FROM Sales_Invoice__c WHERE 	Opportunity__c IN :opportunityIds];
    for(Sales_Invoice__c s: SalesInvoiceToUpdate){
      if(mOpptyProjIds.containsKey(s.Opportunity__c)) {   
     s.Primary_Project__c=mOpptyProjIds.get(s.Opportunity__c);
      }
    }
   if(SalesInvoiceToUpdate.size()>0) {
    update SalesInvoiceToUpdate;
  }
  
 list<Sales_Invoice__c> SalesInvoiceItemToUpdate = [SELECT id, Primary_Project__c, Opportunity__c, (SELECT Opportunity__c, Primary_Project__c, Sales_Invoice__c FROM R00N40000001p914EAA__r) FROM Sales_Invoice__c  WHERE Opportunity__c IN :opportunityIds];
    for(Sales_Invoice__c si: SalesInvoiceItemToUpdate){
      List<Sales_Invoice_Item__c> itemList = si.R00N40000001p914EAA__r; 
        for(Sales_Invoice_Item__c i : itemList){  
      i.Primary_Project__c=mOpptyProjIds.get(si.Opportunity__c);
      
            }
    }
   if(SalesInvoiceItemToUpdate.size()>0) {
    update SalesInvoiceItemToUpdate;
  }  
    
}
And here is the exact portion that doesn't seem to do anything. 
 
list<Sales_Invoice__c> SalesInvoiceItemToUpdate = [SELECT id, Primary_Project__c, Opportunity__c, (SELECT Opportunity__c, Primary_Project__c, Sales_Invoice__c FROM R00N40000001p914EAA__r) FROM Sales_Invoice__c  WHERE Opportunity__c IN :opportunityIds];
    for(Sales_Invoice__c si: SalesInvoiceItemToUpdate){
      List<Sales_Invoice_Item__c> itemList = si.R00N40000001p914EAA__r; 
        for(Sales_Invoice_Item__c i : itemList){  
      i.Primary_Project__c=mOpptyProjIds.get(si.Opportunity__c);
      
            }
    }
   if(SalesInvoiceItemToUpdate.size()>0) {
    update SalesInvoiceItemToUpdate;
  }

 
Best Answer chosen by John Athitakis
RaidanRaidan
Hi John,

I think the problem is SalesInvoiceItemToUpdate is a List of Sales_Invoice__c object and not Sales_Invoice_Item__c. You will have to create a separate list of Sales_Invoice_Item__c and save the values. Make sure you also query the ID of the Sales_Invoice_Item__c to call the update statement.
 

All Answers

RaidanRaidan
Hi John,

I think the problem is SalesInvoiceItemToUpdate is a List of Sales_Invoice__c object and not Sales_Invoice_Item__c. You will have to create a separate list of Sales_Invoice_Item__c and save the values. Make sure you also query the ID of the Sales_Invoice_Item__c to call the update statement.
 
This was selected as the best answer
John AthitakisJohn Athitakis
Aha! I was a dufus and was overlooking which list I was updating. This helped me figure out the answer. Thank you so much!