You need to sign in to do that
Don't have an account?

Hi Team,Apex trigger duplicateemailcheck caused an unexpected exception, contact your administrator: duplicateemailcheck: execution of BeforeInsert caused by: System.StringException: Invalid id The code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trigger duplicateemailcheck on Laptop__c (before insert, before update) {
Laptop__c[] LaptopList1 = Trigger.new;
Set<id> Email = new Set<id>();
for(Laptop__c s : LaptopList1)
{
Email.add(s.Email__c);
}
system.debug(LaptopList1);
List <Laptop__C> duplicateLaptopList1 = new list <Laptop__C>();
duplicateLaptopList1 = [Select id,Email__c from Laptop__c where Email__c IN :Email];
system.debug(duplicateLaptopList1);
Set<id> duplicateemail = new Set<id>();
for(Laptop__c s : duplicateLaptoplist1)
{
duplicateemail.add(s.Email__c);
}
system.debug(duplicateemail);
for(Laptop__c s : LaptopList1)
{
if(duplicateemail.contains(s.Email__c))
{
s.Email__C.addError('Email already exist');
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trigger duplicateemailcheck on Laptop__c (before insert, before update) {
Laptop__c[] LaptopList1 = Trigger.new;
Set<id> Email = new Set<id>();
for(Laptop__c s : LaptopList1)
{
Email.add(s.Email__c);
}
system.debug(LaptopList1);
List <Laptop__C> duplicateLaptopList1 = new list <Laptop__C>();
duplicateLaptopList1 = [Select id,Email__c from Laptop__c where Email__c IN :Email];
system.debug(duplicateLaptopList1);
Set<id> duplicateemail = new Set<id>();
for(Laptop__c s : duplicateLaptoplist1)
{
duplicateemail.add(s.Email__c);
}
system.debug(duplicateemail);
for(Laptop__c s : LaptopList1)
{
if(duplicateemail.contains(s.Email__c))
{
s.Email__C.addError('Email already exist');
}
}
}
Can you please mark this as the solution to remove from the list of Unsolved question.
Thanks,
Jainam
All Answers
First error is, you are trying to add Email to the List of Id's and there are some redundant statement in the trigger.
Try the below code instead:
trigger CheckUniqueEmail on Laptop__c(before insert, before update) {
Set<String> emailSet = new Set<String> ();
for (Laptop__c laptopInstance : Trigger.new) {
if (String.isNotBlank(laptopInstance.Email__c))
emailSet.add(laptopInstance.Email__c);
}
Set<String> emailExistSet = new Set<String> ();
for (Laptop__c laptopExist : [SELECT Email__c FROM Laptop__c WHERE Email IN : emailSet]) {
emailExistSet.add (laptopExist.Email__c);
}
for (Laptop__c laptopInstance : Trigger.new) {
if (emailExistSet.contains (laptopInstance.Email__c))
leadInstance.Email__c.addError ('Duplicate Email !');
}
}
Please let me know if it helps you or you need any more assistance. Please mark this as the solution to help community grow.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Set<string> Email = new Set<string>();
Can you please mark this as the solution to remove from the list of Unsolved question.
Thanks,
Jainam