You need to sign in to do that
Don't have an account?
SFDC16
trigger not working for existing lead email
Hello Developer,
I am trying to create a trigger when a new lead is created it will check lead email in the account exist or not.
If "YES" then update the account name with the lead name.
If "No" the create a new account with lead name.
Any help would be greatly appreciated.
trigger leadMap on Lead (before insert)
{
List<String> lea=new List<String>();
for(lead le:trigger.new)
{
lea.add(le.Email);
}
System.debug('Email------------------------>'+lea);
list<account> accList=[Select id,name,Email__C from Account where Email__C =:lea];
System.debug('accList------------->'+accList);
if(accList.size()>0)
{
for(Lead l:trigger.new)
{
System.debug('before first for loop');
for(Account ac:accList)
{
System.debug('ac===========>'+ac.name);
if(ac.email__C==l.email)
{
System.debug('ac.email__C===============>'+ac.email__C);
System.debug('l.email===================>'+l.email);
ac.name=l.LastName;
ac.Description='trigger fire';
System.debug('Inside if statement');
}
}
}
}
else
{
for(lead ls:trigger.new)
{
Account a=new Account();
a.Name=ls.name;
accList.add(a);
}
}
}
Regards,
SFDC16
I am trying to create a trigger when a new lead is created it will check lead email in the account exist or not.
If "YES" then update the account name with the lead name.
If "No" the create a new account with lead name.
Any help would be greatly appreciated.
trigger leadMap on Lead (before insert)
{
List<String> lea=new List<String>();
for(lead le:trigger.new)
{
lea.add(le.Email);
}
System.debug('Email------------------------>'+lea);
list<account> accList=[Select id,name,Email__C from Account where Email__C =:lea];
System.debug('accList------------->'+accList);
if(accList.size()>0)
{
for(Lead l:trigger.new)
{
System.debug('before first for loop');
for(Account ac:accList)
{
System.debug('ac===========>'+ac.name);
if(ac.email__C==l.email)
{
System.debug('ac.email__C===============>'+ac.email__C);
System.debug('l.email===================>'+l.email);
ac.name=l.LastName;
ac.Description='trigger fire';
System.debug('Inside if statement');
}
}
}
}
else
{
for(lead ls:trigger.new)
{
Account a=new Account();
a.Name=ls.name;
accList.add(a);
}
}
}
Regards,
SFDC16
Try the below code and used always best practice . I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi