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
Tim AndrewsTim Andrews 

Help with simple cross-object field update trigger

Hello all, I am new to Apex and am trying to do a simple field update between two custom objects; I looked through other posts and tried to model mine on the others I saw, but I'm getting stuck. In a nutshell, when a date is entered into the Date_Received__c field on the Invoices__c object, that date should be written to the Invoice_Received_Date__c field on the Work_Products__c object. Work_Products__c is linked to Invoices__c via a lookup relationship. Below is the code I have written:

trigger DateReceivedUpdate on Invoices__c (before insert, before update){
   
  List<ID> WorkProductIds = New List<ID>();
   
  for(Invoices__c a : Trigger.new){
   if(a.Date_Received__c != null){
     WorkProductIds.add(a.Work_Product_Name__c);
   }
  }         
  List<Invoices__c> WorkProductsList = [SELECT id, Date_Received__c FROM Invoices__c WHERE id in :WorkProductIds];
   
    Map<id,Invoices__c> mInvtoWP = New Map<id,Invoices__c>();
   
    for(Work_Products__c p : trigger.new)
     mInvtoWP.put(p.Invoice_Received_Date__c, p);
   
    for(integer i = 0 ; i < WorkProductList.size(); i++){
      WorkProductsList[i].Work_Products__c = mInvtoWP.get(WorkProductsList[i].ID).Invoices__c;
    }
        
    update WorkProductsList;
}

Any help that I can get would be greatly appreciated.
Thanks!
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Tim,

If you have access to the Process Builder, you should use that instead of a trigger. I believe your requirements can be accomplished using Process Builder and therefore avoid using code.

That being said, here is how you would accomplish it with a trigger. I am making a few assumptions since you did not provide this information.
  • Invoice is the parent object and Work Products is the child object.
    • This means that there is a lookup field on the Work Products object that relates it to an Invoice.
  • I assumed the the lookup field on the Work Products object is called "Invoice123__c". Im pretty sure that is not the case, so you will have to update any reference to "Invoice123__c" with the correct field API name.
Trigger code:
trigger DateReceivedUpdate on Invoices__c (after update) 
{
  //You want to make sure that you are running this AFTER updates. 
  //When you do cross object updates from a trigger, you should always use after trigger.

  Map<id,Invoices__c> invoicesMap = New Map<id,Invoices__c>();
  //Loop through all modified invoices and get a map of Invoices whose date received changed
  for(Invoices__c a : Trigger.new)
  {
    //Checking if date_Received__c changed
    if(a.Date_Received__c != trigger.oldMap.get(a.Id).Date_Received__c)
    {
      invoicesMap.put(a.id,a);
    }
  }

  //Query for all Work_Products__c related to the affected (Date changed) Invoices
  List<Work_Products__c> WorkProductsList = [SELECT id, Invoice_Received_Date__c, Invoice123__c 
                                        FROM Work_Products__c WHERE Invoice123__c in :invoicesMap.keyset()];
  
  //Update the Work_Products__c with the dates from the Parent Invoice
  for(Work_Products__c p : WorkProductsList)
  {
    p.Invoice_Received_Date__c = invoicesMap.get(p.Invoice123__c).Date_Received__c;
  }
        
  update WorkProductsList;
}

Good luck!!
 
Tim AndrewsTim Andrews
@Luis Luciani, Thank You for the help! When describing the lookup relationship, I should have said that Work Products__c is the parent and Invoices__c is the child. I had tried to use Process Builder, but was unable to due to the downward-looking relationship between the objects. I'll give your code a try and let you know how it goes. -Tim
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hi Tim,

If that is the case, you could run into a situation in which one "Work Product" can have many Invoice childs. You will have to decide which Date_Received__c of the child Invoices you will be using to set the Invoice_Received_Date__c on the Work Product.

Keep that in mind. I think  in terms of the code, you will to make some modifications to make it work.

I'm here if you have any questions.