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
Nichole SmithNichole Smith 

Apex Trigger for Sibling Object

I am trying to write a trigger to compare the area code from the lead phone and the Area Code name, pull the Parent Market from the Area Code and populate it on a field in the Lead upon creation. The relationship between the 3 objects are: 
Area_Code__c (child Object of Market__c)
Market__c(Custom Object with master-detail to Area_Code)
Lead(Lookup relationship to Market__c called Lead_Market__c)
I created a formula field on the lead object to pull the area code out of the phone number on lead and it is called Area_Code__c.
I have created this Trigger and it saves but and I am not getting any errors but it is not populating the Lead_Market__c field either. According to the debug logs the problem seems to be in the last for loop called  LeadNameLoop. Does anyone see anything wrong with this?
Trigger AreaCode on Lead (before insert) {

Set <String> InitialLeadName = new Set <String> ();

for(Lead LeadNameLoop : trigger.New)
{
    InitialLeadName.add(LeadNameLoop.Area_Code__c);
}
Map <String, Area_Code__c> matchingAreaCodeMap = new Map <String, Area_Code__c> ();

for (Area_Code__c Record : [Select Id, Name, Market__c From Area_Code__c Where Name IN :InitialLeadName])
{
    matchingAreaCodeMap.put(Record.Market__c, record);
}

List <Lead> FinalLeadList = new List <Lead> ();

    for(Lead LeadNameLoop : trigger.New)
{
    if (matchingAreaCodeMap.get(LeadNameLoop.Name) != null)
    {
          LeadNameLoop.Lead_Market__c = matchingAreaCodeMap.get(LeadNameLoop.Area_Code__c).Market__c;
         FinalLeadList.add(LeadNameLoop);
    }
} 
upsert FinalLeadList;
}
Best Answer chosen by Nichole Smith
v varaprasadv varaprasad
Trigger AreaCode on Lead (before insert) {

Set <String> InitialLeadName = new Set <String> ();

for(Lead LeadNameLoop : trigger.New)
{   
    if(LeadNameLoop.Area_Code__c != null){
	  InitialLeadName.add(LeadNameLoop.Area_Code__c);
	}
    
}

Map <String, Area_Code__c> matchingAreaCodeMap = new Map <String, Area_Code__c> ();

for (Area_Code__c Record : [Select Id, Name, Market__c From Area_Code__c Where Name IN :InitialLeadName])
{
    matchingAreaCodeMap.put(Record.name, record);
}



 for(Lead LeadNameLoop : trigger.New)
{
  
         LeadNameLoop.Lead_Market__c = matchingAreaCodeMap.get(LeadNameLoop.Area_Code__c).Market__c;
        
   
} 

}

 

All Answers

v varaprasadv varaprasad
Hi Nichole,

In before events we don't need to perform DML on the same object.
Remove below lines in your code and check once.

FinalLeadList.add(LeadNameLoop);
upsert FinalLeadList;
List <Lead> FinalLeadList = new List <Lead> ();



Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
v varaprasadv varaprasad
Trigger AreaCode on Lead (before insert) {

Set <String> InitialLeadName = new Set <String> ();

for(Lead LeadNameLoop : trigger.New)
{   
    if(LeadNameLoop.Area_Code__c != null){
	  InitialLeadName.add(LeadNameLoop.Area_Code__c);
	}
    
}

Map <String, Area_Code__c> matchingAreaCodeMap = new Map <String, Area_Code__c> ();

for (Area_Code__c Record : [Select Id, Name, Market__c From Area_Code__c Where Name IN :InitialLeadName])
{
    matchingAreaCodeMap.put(Record.name, record);
}



 for(Lead LeadNameLoop : trigger.New)
{
  
         LeadNameLoop.Lead_Market__c = matchingAreaCodeMap.get(LeadNameLoop.Area_Code__c).Market__c;
        
   
} 

}

 
This was selected as the best answer
Nichole SmithNichole Smith
That makes sense.. Thank you!