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
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli 

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');
               }
       }
}
Best Answer chosen by Subrahmanyam Konathlapalli
Jainam ContractorJainam Contractor
Your welcome.

Can you please mark this as the solution to remove from the list of Unsolved question.

Thanks,
Jainam

All Answers

Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
Email feid type is Text... I am unable to chage the feild type also becuase this feild is used in class & triggers Please help me, i am in lerning stage...
Jainam ContractorJainam Contractor
Hi Subrahmanyam,

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
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
Thank you so much Jainam.
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
In 3 rd line i changed the datatype that code also worked..
Set<string> Email = new Set<string>();

 
Jainam ContractorJainam Contractor
Your welcome.

Can you please mark this as the solution to remove from the list of Unsolved question.

Thanks,
Jainam
This was selected as the best answer