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

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();
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;
David VPDavid VP

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.


Message Edited by David VP on 09-29-2008 09:37 AM