You need to sign in to do that
Don't have an account?
David Kerr 5
Apex trigger similar to Index,Match formula in Excel
I'm new to Apex & trying to figure out a trigger that will match the email of a Lead & a Child user and give me the Id in one object. If a lead already exists as a child, I want the child Id. Otherwise, the field in the Lead object should be left blank. Any idea how to approach this?
Sure thing. I want to identify if any Leads that come into the system already exist as a Child user. The trigger would fire and check if there is a match between the emails of the Lead and Child. If there is a match, I want to pull the ID of the Child user, if not, it would be left blank. I've created a lookup relationship between Child and Lead and I want to autofill this field.
Looks like the code saved fine. However, I'm getting the following error when I create a test Lead and a test Child user:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadExist caused an unexpected exception, contact your administrator: LeadExist: execution of BeforeInsert caused by: System.StringException: Invalid id: (*Parent_Customer_ID__c*): External entry point
The Parent_Customer_ID__c is the custom field that I want to pull the value for if the email of the Lead matches the email of the Child user. That value should auto-populate on my looku Here is my code:
trigger LeadExist on Lead (before insert) {
Set<String> EmailId = new Set<String>();
Map<String,Id> MapuserId = new Map<String,Id>();
List<Child__c> usrlist = new List<Child__c>();
for(Lead ld : trigger.new)
{
If(ld.Email != null)
{
EmailId.add(ld.Email);
}
}
If(EmailId.size() > 0)
{
usrlist = [Select Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];
}
if(usrlist.size() > 0)
{
for(Child__c c : usrlist)
{
if(!MapuserId.containsKey(c.Email__c))
{
MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);
}
}
}
for(Lead ld : trigger.new)
{
ld.Email = MapuserId.get(ld.Email);
}
}
I appreciate your assistance on this!
Please use the below updated code:
trigger LeadExist on Lead (before insert) {
Set<String> EmailId = new Set<String>();
Map<String,Id> MapuserId = new Map<String,Id>();
List<Child__c> usrlist = new List<Child__c>();
for(Lead ld : trigger.new)
{
If(ld.Email != null)
{
EmailId.add(ld.Email);
}
}
If(EmailId.size() > 0)
{
usrlist = [Select Id,Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId]; // Check did you need the Parent_Customer_ID__c or ID of the child user record Id
}
if(usrlist.size() > 0)
{
for(Child__c c : usrlist)
{
if(!MapuserId.containsKey(c.Email__c))
{
MapuserId.put(c.Email__c, c.Parent_Customer_ID__c); ///Use the c.Id if you want the ID of the child user record Id or if you want the Parent_Customer_ID__c from the child user object keep it as line exist
}
}
}
for(Lead ld : trigger.new)
{
ld.Parent_Customer_ID__c = MapuserId.get(ld.Email); /// In which field you want to populate the ID in Lead Object use that API Name
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadExist caused an unexpected exception, contact your administrator: LeadExist: execution of BeforeInsert caused by: System.StringException: Invalid id: TestID_1: External entry point
TestID_1 is the Parent_Customer_ID_c value for the Child whose email matches a Lead with the same Email.
trigger LeadExist on Lead (before insert) {
Set<String> EmailId = new Set<String>();
Map<String,Id> MapuserId = new Map<String,Id>();
List<Child__c> usrlist = new List<Child__c>();
for(Lead ld : trigger.new)
{
If(ld.Email != null)
{
EmailId.add(ld.Email);
}
}
If(EmailId.size() > 0)
{
usrlist = [Select Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];
}
if(usrlist.size() > 0)
{
for(Child__c c : usrlist)
{
if(!MapuserId.containsKey(c.Email__c))
{
MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);
}
}
}
for(Lead ld : trigger.new)
{
Id.Email= MapuserId.get(ld.Child_Exist__c);
}
}
trigger LeadExist on Lead (before insert) {
Set<String> EmailId = new Set<String>();
Map<String,String> MapuserId = new Map<String,String>();
List<Child__c> usrlist = new List<Child__c>();
for(Lead ld : trigger.new)
{
If(ld.Email != null)
{
EmailId.add(ld.Email);
}
}
If(EmailId.size() > 0)
{
usrlist = [Select Parent_Customer_ID__c, Email__c from Child__c Where Email__c IN: EmailId];
}
if(usrlist.size() > 0)
{
for(Child__c c : usrlist)
{
if(!MapuserId.containsKey(c.Email__c))
{
MapuserId.put(c.Email__c, c.Parent_Customer_ID__c);
}
}
}
for(Lead ld : trigger.new)
{
ld.Email= MapuserId.get(ld.Child_Exist__c);
}
}