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
R557R557 

Updating a date field on an object

Hi, I am stuck on the following problem statement:
I have  Asset , Invoice_Line_Item__c and Invoice__c  objects.
I need to write a trigger on Invoice__c in which :
if  Delivery_Date__c field on Invoice__c is updated , Delivery_Date__c field on corresponding Asset as well as Delivery_Date__c on Invoice_Line_Item__c should also be updated with the same and a checkbox field labeled - 'Updated?' should be checked.

Note -  Asset field has a lookup on Invoice__c . 
Also, Invoice and Invoice_Line_Item have a Master Detail relationship between them.
 
Andrew GAndrew G
Have you investigated using Process Builder?

Regards
Andrew
R557R557
Yeah . But the requirement is strictly to build a trigger.
Deepali KulshresthaDeepali Kulshrestha
Hi,

Please use below code:
 
trigger

trigger InvoiceTrigger on Invoice__c (After update) {
if(trigger.isAfter){
       if(trigger.isUpdate){
            UpdateDateField.UpdateAssetDate(Trigger.New);
            UpdateDateField.UpdateInvoiceLineItemDate(Trigger.New);
        }   
         
    }
}


Apex class to update date

public class UpdateDateField {
    
    public static void UpdateAssetDate(List<Invoice__c> invoiceList){
        try{
            List<Invoice__c> newInvoiceList = new List<Invoice__c>();
            newInvoiceList = [SELECT Id, Delivery_Date__c, (SELECT Id,Delivery_Date__c,Updated__c FROM Assets__r) FROM Invoice__c WHERE Id IN :invoiceList];
            List<Asset> assetList = new List<Asset>();
            if(newInvoiceList.size()>0){
                for(Invoice__c invoiceObj: newInvoiceList){
                    for(Asset assetObject : invoiceObj.Assets__r){
                        
                        Invoice__c invoiceOld =(Invoice__c)Trigger.oldMap.get(invoiceObj.Id);
                        Date oldInvoiceDate = invoiceOld.Delivery_Date__c;
                        
                        if(oldInvoiceDate != invoiceObj.Delivery_Date__c){
                            assetObject.Delivery_Date__c =  invoiceObj.Delivery_Date__c;
                            assetObject.Updated__c = true;
                            assetList.add(assetObject);
                        }
                    }
                    
                }
                if(assetList.size() > 0){
                    update assetList;
                }
            }  
        }catch(Exception e){
            System.debug('Exception in code--->'+e.getCause()+'Exception in line number--->'+e.getLineNumber());
        }
    } 
    public static void UpdateInvoiceLineItemDate(List<Invoice__c> invoiceList){
        try{
            List<Invoice__c> newInvoiceList = new List<Invoice__c>();
            newInvoiceList = [SELECT Id, Delivery_Date__c, (SELECT Id,Delivery_Date__c, Updated__c FROM Invoice_Line_Items__r) FROM Invoice__c WHERE Id IN :invoiceList]; 
            List<Invoice_Line_Item__c> InvoiceLineItemList = new List<Invoice_Line_Item__c>();
            
            if(newInvoiceList.size()>0){
                for(Invoice__c invoiceObj: newInvoiceList){
                    for(Invoice_Line_Item__c invoiceLineItemObject : invoiceObj.Invoice_Line_Items__r){
                        
                        Invoice__c invoiceOld =(Invoice__c)Trigger.oldMap.get(invoiceObj.Id);
                        Date oldInvoiceDate = invoiceOld.Delivery_Date__c;
                        if(oldInvoiceDate != invoiceObj.Delivery_Date__c){
                            invoiceLineItemObject.Delivery_Date__c =  invoiceObj.Delivery_Date__c;
                            invoiceLineItemObject.Updated__c = true;
                            InvoiceLineItemList.add(invoiceLineItemObject);
                        } 
                    }
                    
                }
                if(InvoiceLineItemList.size() > 0){
                    update InvoiceLineItemList;
                }
            }
            
        }catch(Exception e){
            System.debug('Exception in code--->'+e.getCause()+'Exception in line number--->'+e.getLineNumber());
        }   
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,

Deepali Kulshrestha.