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
Kacy CoatesKacy Coates 

Create New Related Email object on Contact Record with trigger

I'm new to writing Apex triggers, and I'm not really sure where to start, so I'm hoping someone can help me. We currently have a trigger as part of our managed package that creates a new record for an Object called Related Address when you update the address field on a contact record.

I would basically like to copy this trigger for our email address field on the contact. Because it's part of our managed package, I can't view what the Trigger coding is to just mimick it myself. We already have the Related Email object created, so now I just need to figure out how to format the Trigger. Can anybody advise me on a trigger to do this? I would be much appreciated!
Neha AggrawalNeha Aggrawal
Hi Kacy,

I dont think you can get the code from the managed package except from the developer.
You can write an update trigger on Contact object:
 
//Class called by update trigger on Contact
public class CreateRelatedEmailRecord   {
public void createFunc(){
//get the contact records which triggered the class
for(Contact c: Trigger.new)
ConSet.add(c.Id);
}
 
for(Contact c:[Select Id, Address from Contact where Id in: ConSet])
{
RelatedEmail__c re=new RelatedEmail__c(Name='relatedemail', email__c=c.email);
//can't insert record within for loop, adding to list and inserting later
relatedlist.add(re);
}
insert relatedlist;
}
The code is obviously not tested, and I have also skipped the variable declarations. This is just to give you some idea of how the code works. Go through the link to understand more: https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

Give it a shot, you will learn. Post the code here if you face errors.

Hope this helps.
Thanks and Regards, 
Neha
www.initaura.com - Everything Salesforce (https://initaura.com)
Deepali KulshresthaDeepali Kulshrestha
Hi Kacy,

I have gone through your question. Create a new record on the update of the email address field. 
Please go through the code given below:-
 
trigger createRelatedEmail on Contact (after insert,after update) {
    List<String>  emaillist = new List<String>();
    for(Contact con : trigger.old){
        emaillist.add(con.Email);
    }
    List<Related_Email__c> relatedEmailList = new List<Related_Email__c>();
    if((trigger.isInsert||trigger.isUpdate)&& trigger.isAfter){
        for(Contact con : trigger.new){
            if(con.Email != null && !emaillist.contains(con.Email)){
                Related_Email__c r = new Related_Email__c();
                r.Name = con.Email;
                relatedEmailList.add(r); 
            }
        }
        insert relatedEmailList;
    }
}

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
www.kdeepali.com