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
SF Admin.HRGSF Admin.HRG 

Trigger to update a related record?

Hello

We have a custom object Individual Email Sends and I'm trying to write a trigger that updates a text field (Emails__c) on the related Account (lookup field).
So every time a new Individual Email is sent, it would then add the name of this email to the Email__c field, without deleting the previous data in it.

I know I'll need to create a List of all the Individual Email Sends, with after insert and after update.
Then a List of related Accounts that need to be updated, but how do I find the related Accounts for it?

Thanks!
Best Answer chosen by SF Admin.HRG
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello SF Admin.HRG, 

I have made some changes in Line no 18 to get the Name from Child and assign it to Account's text field.

So, Text field in Account will be updated with Name from Child record. 
 
trigger TestTrigger on Child__c (after insert, after update) {
    Map<ID, Account> parentAccs = new Map<ID, Account>();
    
    List<Id> listIds = new List<Id>();
    
    
   
   for (Child__c childObj : Trigger.new) {
        listIds.add(childObj.Account__c);
       
    }
    
    parentAccs = new Map<Id, Account>([SELECT id, TextField__c FROM Account WHERE ID IN :listIds]);
    
    for (Child__c child : Trigger.new){
     Account parentAcc = parentAccs.get(child.Account__c);
     if(parentAcc.TextField__c != NULL)
  parentAcc.TextField__c = parentAcc.TextField__c +','+ child .Name;//Update Statement. This statement is for updating the Text Field in Account record with Name of Child record     
     else
     parentAcc.TextField__c = 'Mail Subject';
  }
  update parentAccs.values();
}


Thanks,
Vinoth
 

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello, 

Please find the below Code Sample for updating the field in related(look up) Account object. 
 
trigger TestTrigger on Child__c (after insert, after update) {
    Map<ID, Account> parentAccs = new Map<ID, Account>();
    
    List<Id> listIds = new List<Id>();
    
    
   
   for (Child__c childObj : Trigger.new) {
        listIds.add(childObj.Account__c);
       
    }
    
    parentAccs = new Map<Id, Account>([SELECT id, TextField__c FROM Account WHERE ID IN :listIds]);
    
    for (Child__c child : Trigger.new){
     Account parentAcc = parentAccs.get(child.Account__c);
     if(parentAcc.TextField__c != NULL)
     parentAcc.TextField__c = parentAcc.TextField__c +','+ 'Mail Subject';//Replace  'Mail Subject' with your Text for Email 
     
     else
     parentAcc.TextField__c = 'Mail Subject';
  }
  update parentAccs.values();
}

Thanks,
Vinoth
SF Admin.HRGSF Admin.HRG

Hello Vinoth, thank your for the help! =)

 

As I understand this trigger will insert a certain value (set in Mail Subject) to the TextField.
However I'll need it to take the Mail Subject from the name field from the Child__c object.

 

(As this trigger will fire every time a new EmailSend is happening, the idea is to collect the Email names on the Account level.)

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hello SF Admin.HRG, 

I have made some changes in Line no 18 to get the Name from Child and assign it to Account's text field.

So, Text field in Account will be updated with Name from Child record. 
 
trigger TestTrigger on Child__c (after insert, after update) {
    Map<ID, Account> parentAccs = new Map<ID, Account>();
    
    List<Id> listIds = new List<Id>();
    
    
   
   for (Child__c childObj : Trigger.new) {
        listIds.add(childObj.Account__c);
       
    }
    
    parentAccs = new Map<Id, Account>([SELECT id, TextField__c FROM Account WHERE ID IN :listIds]);
    
    for (Child__c child : Trigger.new){
     Account parentAcc = parentAccs.get(child.Account__c);
     if(parentAcc.TextField__c != NULL)
  parentAcc.TextField__c = parentAcc.TextField__c +','+ child .Name;//Update Statement. This statement is for updating the Text Field in Account record with Name of Child record     
     else
     parentAcc.TextField__c = 'Mail Subject';
  }
  update parentAccs.values();
}


Thanks,
Vinoth
 
This was selected as the best answer
SF Admin.HRGSF Admin.HRG

You are awesome, Vinoth!

Thank you, this code works perfectly. Learned to understand Maps in the process as well.