0

I am trying to upsert a contact conditionally if similar contact is found.

Step 1: Prepare new contact data
Contact c = new Contact(lastname = 'test', email = 'test@test.com', externalId__c ='12345');
//Step 2: Check existing contacts for duplicates "with some exceptions (and hence I cannot use uniqueness on this field)"
//Step 3: upsert
upsert c externalId__c;

If any contact is found in step 2 logic and has same external Id, I should be still able to do the upsert, but donot overwrite existing contact email address.

I am trying not to do additional query but instead just remove the email field from being updated.

Any suggestions ? Thanks in advance

3

In step 2, you have to query the contact. Something like this:

Contact c = new Contact(LastName='Test', ExternalId__c='12345');
if([SELECT COUNT() FROM Contact WHERE ExternalId__c = :externalId] == 0) {
    // new contact
    c.Email = 'test@test.com';
} else {
    // existing contact
}
upsert c Contact.ExternalId__c;
1
  • Thanks sfdcfox. I thought almost the same, but posted to get if there are any workarounds. Thankyou
    – sf_user
    Nov 29 '16 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.