You need to sign in to do that
Don't have an account?
Andrew marshal 3
Create test class for this
Please create a test class for this
public with sharing class LeadProcessor {
public static void updateLeadStatus(List<Lead> leadsToUpdate) {
Set<Id> accountIds = new Set<Id>();
// Loop through the leads and collect the Account Ids
for (Lead lead : leadsToUpdate) {
accountIds.add(lead.AccountId);
}
// Query the related Accounts to check for duplicate leads
List<Account> relatedAccounts = [SELECT Id, (SELECT Id, Email FROM Leads) FROM Account WHERE Id IN :accountIds];
Map<String, Set<String>> emailToLeadIds = new Map<String, Set<String>>();
// Loop through the related Accounts and Leads and create a map of Email to Lead Ids
for (Account account : relatedAccounts) {
for (Lead lead : account.Leads) {
if (lead.Email != null) {
String emailKey = lead.Email.toLowerCase();
if (emailToLeadIds.containsKey(emailKey)) {
emailToLeadIds.get(emailKey).add(lead.Id);
} else {
Set<String> leadIds = new Set<String>{lead.Id};
emailToLeadIds.put(emailKey, leadIds);
}
}
}
}
// Loop through the leads and check for duplicates based on Email
for (Lead lead : leadsToUpdate) {
if (lead.Email != null) {
String emailKey = lead.Email.toLowerCase();
if (emailToLeadIds.containsKey(emailKey) && !emailToLeadIds.get(emailKey).contains(lead.Id)) {
lead.addError('This Lead has the same email address as another Lead in the related Account.');
} else {
if (lead.Status == 'New') {
lead.Status = 'Open';
}
}
}
}
update leadsToUpdate;
}
}
public with sharing class LeadProcessor {
public static void updateLeadStatus(List<Lead> leadsToUpdate) {
Set<Id> accountIds = new Set<Id>();
// Loop through the leads and collect the Account Ids
for (Lead lead : leadsToUpdate) {
accountIds.add(lead.AccountId);
}
// Query the related Accounts to check for duplicate leads
List<Account> relatedAccounts = [SELECT Id, (SELECT Id, Email FROM Leads) FROM Account WHERE Id IN :accountIds];
Map<String, Set<String>> emailToLeadIds = new Map<String, Set<String>>();
// Loop through the related Accounts and Leads and create a map of Email to Lead Ids
for (Account account : relatedAccounts) {
for (Lead lead : account.Leads) {
if (lead.Email != null) {
String emailKey = lead.Email.toLowerCase();
if (emailToLeadIds.containsKey(emailKey)) {
emailToLeadIds.get(emailKey).add(lead.Id);
} else {
Set<String> leadIds = new Set<String>{lead.Id};
emailToLeadIds.put(emailKey, leadIds);
}
}
}
}
// Loop through the leads and check for duplicates based on Email
for (Lead lead : leadsToUpdate) {
if (lead.Email != null) {
String emailKey = lead.Email.toLowerCase();
if (emailToLeadIds.containsKey(emailKey) && !emailToLeadIds.get(emailKey).contains(lead.Id)) {
lead.addError('This Lead has the same email address as another Lead in the related Account.');
} else {
if (lead.Status == 'New') {
lead.Status = 'Open';
}
}
}
}
update leadsToUpdate;
}
}
One question for my knowledge, if you can help me with that?
- Where did you get the AccountId (Looks like a Standard field as there is no __c), I know the ConvertedAccountId field but never came across that field.
- Is your code really working?
Also I placed some safety checks (null pointer checks) so your code doesn't crash in certain scenarios.Regards,
Abdul Aziz Khatri
All Answers
Please check with below and let me know further ;
Hope the above test class works !
Thank you.
One question for my knowledge, if you can help me with that?
- Where did you get the AccountId (Looks like a Standard field as there is no __c), I know the ConvertedAccountId field but never came across that field.
- Is your code really working?
Also I placed some safety checks (null pointer checks) so your code doesn't crash in certain scenarios.Regards,
Abdul Aziz Khatri
If you find this answer helpful, Please mark it as the best answer.