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
Brian Chipman 4Brian Chipman 4 

Trigger to update a related object record on delete

I can write a simple trigger to update fields on the same object.  However, I have requirements that I'm not having success with even though there are a few pertinent examples.  The requirements are:

1) When the Total_Reimbursement__c field is updated on a Service_Order__c custom object record, the SOChanged__c field on the related (Lookup) Asset record is set to true.

2) When a Service_Orders__c record is deleted,  the SOChanged__c field on the related (Lookup) Asset record is set to true.​

The goal of course is to be able to, especially in the case of 2) above, kick off a variety of flows/processes that we might create when a Service Order is deleted on the update of this field. 1) above of could be done in a workflow, but if I'm doing a trigger, might as well put it there too.  Anything to help get me on my way would be appreciated. 
Best Answer chosen by Brian Chipman 4
Abdul KhatriAbdul Khatri
Sorry that should trigger.oldMap
 
trigger UpdateAsset on Service_Order__c (after update, after delete) {
    
    List<Id> idAssetList = new List<Id>();
    List<Asset> assetListToUpdate = new List<Asset>();
    
    if(trigger.isUpdate) {
        
        for (Service_Order__c so : trigger.new) {
            
            if(trigger.oldMap.get(so.Id).Total_Reimbursement__c == so.Total_Reimbursement__c) continue;
            
            idAssetList.add(so.Asset__c);
        }       
    }
    
    if(trigger.isDeleted) {
        
        for (Service_Order__c so : trigger.old) {
                      
            idAssetList.add(so.Asset__c);
        }         
    }
    
    if(idAssetList.isEmpty()) return;
    
    for(Id id : idAssetList) {
        
        Asset asset = new Asset(Id = id);
        asset.SOChanged__c = true;
        
        assetListToUpdate.add(asset);
        
    }
    
    if(assetListToUpdate.size() > 0)
        Database.update(assetListToUpdate);
    
}

 

All Answers

Abdul KhatriAbdul Khatri
Here is the trigger, You can change the name as per your need. I ran through field names as per your instructions
 
trigger UpdateAsset on Service_Order__c (after update, after delete) {
    
    List<Id> idAssetList = new List<Id>();
    List<Asset> assetListToUpdate = new List<Asset>();
    
    if(trigger.isUpdate) {
        
        for (Service_Order__c so : trigger.new) {
            
            if(oldMap.get(so.Id).Total_Reimbursement__c == so.Total_Reimbursement__c) continue;
            
            idAssetList.add(so.Asset__c);
        }       
    }
    
    if(trigger.isDeleted) {
        
        for (Service_Order__c so : trigger.old) {
                      
            idAssetList.add(so.Asset__c);
        }         
    }
    
    if(idAssetList.isEmpty()) return;
    
    for(Id id : idAssetList) {
        
        Asset asset = new Asset(Id = id);
        asset.SOChanged__c = true;
        
        assetListToUpdate.add(asset);
        
    }
    
    if(assetListToUpdate.size() > 0)
        Database.update(assetListToUpdate);
    
}



 
Brian Chipman 4Brian Chipman 4
This is great Abdul.  One question, where or how are we defining 'OldMap' in line 10?  I get a 'variable does not exist' there.
Abdul KhatriAbdul Khatri
Sorry that should trigger.oldMap
 
trigger UpdateAsset on Service_Order__c (after update, after delete) {
    
    List<Id> idAssetList = new List<Id>();
    List<Asset> assetListToUpdate = new List<Asset>();
    
    if(trigger.isUpdate) {
        
        for (Service_Order__c so : trigger.new) {
            
            if(trigger.oldMap.get(so.Id).Total_Reimbursement__c == so.Total_Reimbursement__c) continue;
            
            idAssetList.add(so.Asset__c);
        }       
    }
    
    if(trigger.isDeleted) {
        
        for (Service_Order__c so : trigger.old) {
                      
            idAssetList.add(so.Asset__c);
        }         
    }
    
    if(idAssetList.isEmpty()) return;
    
    for(Id id : idAssetList) {
        
        Asset asset = new Asset(Id = id);
        asset.SOChanged__c = true;
        
        assetListToUpdate.add(asset);
        
    }
    
    if(assetListToUpdate.size() > 0)
        Database.update(assetListToUpdate);
    
}

 
This was selected as the best answer
Brian Chipman 4Brian Chipman 4
Works perfectly (fixed line 10 to "trigger.isDelete" instead of "trigger.isDeleted" ) and I have learned a couple of things.  Many thanks Abdul.
Brian Chipman 4Brian Chipman 4
Correction:  fixed line 16, not line 10 "trigger.isDelete" instead of "trigger.isDeleted"