You need to sign in to do that
Don't have an account?
Ivar
Problem with lookup for a contact
Hi everyone. I have been searching the boards for a solution for something similar, have found several but none have seemed to fix my problem. Can anyone shed a light on why the following code segment (run within an Inbound Email Processor) keeps creating new Contacts each run around, instead of creating one the first time and then subsequently finding an already created one?
Contact contact = new Contact();
try{
contact = [SELECT Id, Name FROM Contact Where Name = :txtContactName AND AccountId = :account.Id limit 1];
}
catch( Exception e ){
contact.LastName = txtContactName;
contact.AccountId = account.Id;
insert contact;
}
Contact contact = new Contact();
try{
contact = [SELECT Id, Name FROM Contact Where Name = :txtContactName AND AccountId = :account.Id limit 1];
}
catch( Exception e ){
contact.LastName = txtContactName;
contact.AccountId = account.Id;
insert contact;
}
use upsert instead of insert ?
upsert = update when Id already exists, insert when not
insert = always create
I would also not use the 'catch exception' but Contact[] contacts = .... and test how many results you have returned.
David
Message Edited by David VP on 09-29-2008 09:37 AM