You need to sign in to do that
Don't have an account?
Jessica Puckett
Invalid bind expression type of Contact for column of type String
Hello all,
I'm working on an Apex trigger and am running into an issue I'm not sure how to resolve. (I'm still quite new to Apex.) I'm receiving the following problem on lines 2 and 3: "Invalid bind expression type of Contact for column of type String" Suggestions are greatly appreciated!
My trigger code is below:
trigger EmailDuplicate on Contact (before insert, before update, before delete) {
List<Contact> emaillist = [SELECT id,email FROM Contact WHERE Email!=null AND Email IN :trigger.new];
List<Contact> existingcontact = [SELECT id, email,lastname FROM Contact WHERE email IN :emaillist AND id NOT IN :trigger.new];
for(Contact current : emaillist){
for(Contact existing : existingcontact){
if(current.Email==existing.Email){
current.duplicate_email__c=true;
}
else if (current.Email!=existing.Email){
current.duplicate_email__c=false;
update emaillist;
}
}
}
}
I'm working on an Apex trigger and am running into an issue I'm not sure how to resolve. (I'm still quite new to Apex.) I'm receiving the following problem on lines 2 and 3: "Invalid bind expression type of Contact for column of type String" Suggestions are greatly appreciated!
My trigger code is below:
trigger EmailDuplicate on Contact (before insert, before update, before delete) {
List<Contact> emaillist = [SELECT id,email FROM Contact WHERE Email!=null AND Email IN :trigger.new];
List<Contact> existingcontact = [SELECT id, email,lastname FROM Contact WHERE email IN :emaillist AND id NOT IN :trigger.new];
for(Contact current : emaillist){
for(Contact existing : existingcontact){
if(current.Email==existing.Email){
current.duplicate_email__c=true;
}
else if (current.Email!=existing.Email){
current.duplicate_email__c=false;
update emaillist;
}
}
}
}
Try like this
trigger EmailDuplicate on Contact (before insert, before update, before delete) {
Set<String> emaillistSet = new Set<String>() ;
for(Contact c: [Select id, Email from Contact where email !=null]){
emaillistSet.add(c.email) ;
}
List<Contact> emaillist = [SELECT id,email FROM Contact WHERE Email!=NULL AND Email IN : emaillistSet];
List<Contact> existingcontact = [SELECT id, email,lastname FROM Contact WHERE Email IN :emaillistSet AND id NOT IN :trigger.newMap.keySet()];
for(Contact current : emaillist){
for(Contact existing : existingcontact){
if(current.Email==existing.Email){
// current.duplicate_email__c=true;
}
else if (current.Email!=existing.Email){
// current.duplicate_email__c=false;
update emaillist;
}
}
}
}
The problem is that you are checking if Email, which is a String, is in Trigger.new (line 2) and in emaillist (line 3). Both Trigger.new and emaillist are lists containing Contacts, not Strings. You will have to refactor your logic to work around this.
Here's a hint (pseudo-code) for a possible solution:
If you're not familiar with Maps and Sets, please refer to these articles:
Once you come up with a refactored version of your trigger, feel free to post it here, and I will be happy to let you know if you're on the right track.
Also, if you're struggling to find a solution using the pseudo-code from my previous answer, you can try this instead:
The difference between this solution and the last solution is that this one uses two Lists in line 1 where the previous one used a Map. Both should work just fine. But the code in my previous answer may be a useful practice in learning more Apex!