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
Abdul RazzaqAbdul Razzaq 

creating a trigger

A trigger to insert email value in custom object from lead object (lookup).
Best Answer chosen by Abdul Razzaq
Pankaj_GanwaniPankaj_Ganwani
Hi Abdul,

By the way this can be accomplished by creating formula field on child object ie. validlead. But, by using trigger approach you can do the following:

Create a custom field on validlead object which will store the Email field value of selected lead. Using lookup, you can only get the name at the UI and in backend Id will come(as per salesforce internal mechanism).

Now, Iterate over list of insert validlead records(before insert event of trigger):

for(validlead obj : lstvalidlead)// here lstvalidlead is the list of records that are going to be inserted
{
    setlLead.add(obj.Lead__c);
}

Now, fetch the lead's email using soql:

map<Id, Lead> mapIdToLead  = new Map<Id,Lead>([select Id, Email from Lead where Id IN : setLeadId]);

for(validlead obj : lstvalidlead)
{
     obj.Email__c = mapIdToLead.get(obj.Lead__c).Email;// here Email__c is custom field on child object that has to be created.
}

Please let me know if this helps you.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Abdul,

Can you please let us know which custom objects you want to fill the email value in? Is there any criteria?
ra1ra1
There are many ways to populate any field value:
1. Formula field : Use formula to dynamically fetch value from any related record. For example: new formula field with return type as Text & formula can be:
   
Lead__r.Email
Drowback : It is act like text field and you can't refer it as EMAIL field in Sent Email action.

2. Workflow : Use workflow's field update action to populate email value from related record. Use " Lead__r.Email " within "formula to set the new value" option.

3. Trigger: sample trigger code will look lie: 
trigger UpdateEmailTrigger on Test_Lead__c (before insert) {
    Map<Id, Id> objIdLeadIdMap = new Map<Id, Id>();
	for(Test_Lead__c testLead : Trigger.new){
        if(null != testLead.Lead__c ) {
            objIdLeadIdMap.put(testLead.Id, testLead.Lead__c);
        }
    }
    
    if(!objIdLeadIdMap.isEmpty()) {
        Map<Id, Lead> leadMap = new Map<Id, Lead>([select Id, Email from Lead where Id in: objIdLeadIdMap.values()]);
        
        Lead tempLead;
        for(Test_Lead__c testLead : Trigger.new){
            if(null != testLead.Lead__c && leadMap.containsKey(testLead.Lead__c)) {
                tempLead = leadMap.get(testLead.Lead__c);
                testLead.Email__c = tempLead.Email;
            }
        }
    }
}


 
Abdul RazzaqAbdul Razzaq

Hi pankaj,

Good to see you again for my help.

My custom object name is validlead. And I have created a Lookup relationship with standard Lead object, the field name is Email.
Whenever I have to enter the Email Value in ValidLead I get a small lookup icon on the side and I am able to Enter only Name in that field. I want Email address to be populated in that field. And I am asked to use Trigger to do that. Any Idea??

Thanks and Regards,

Razzzaq.

Pankaj_GanwaniPankaj_Ganwani
Hi Abdul,

By the way this can be accomplished by creating formula field on child object ie. validlead. But, by using trigger approach you can do the following:

Create a custom field on validlead object which will store the Email field value of selected lead. Using lookup, you can only get the name at the UI and in backend Id will come(as per salesforce internal mechanism).

Now, Iterate over list of insert validlead records(before insert event of trigger):

for(validlead obj : lstvalidlead)// here lstvalidlead is the list of records that are going to be inserted
{
    setlLead.add(obj.Lead__c);
}

Now, fetch the lead's email using soql:

map<Id, Lead> mapIdToLead  = new Map<Id,Lead>([select Id, Email from Lead where Id IN : setLeadId]);

for(validlead obj : lstvalidlead)
{
     obj.Email__c = mapIdToLead.get(obj.Lead__c).Email;// here Email__c is custom field on child object that has to be created.
}

Please let me know if this helps you.
This was selected as the best answer
Abdul RazzaqAbdul Razzaq

Hi Pankaj,

That was like a "Bulls Eye" solution for me.

I am pretty new to triggers. So, I may get more doubts on triggers. Is there a way? I could tag you on my questions. I would prefer you now for my answers. 

Thanks alot Dude.

Five Stars.