Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3241230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:10:29+00:00 2026-05-17T18:10:29+00:00

I am new to Apex development and I’m starting to work on my project

  • 0

I am new to Apex development and I’m starting to work on my project in Apex. I would like to do following:


using an email service //I figured this part
get emailAddress from an email //know how to do this
if there is already exisiting lead or contact with the same emailAddress
update his information
else
create new lead


I would like to kow how I can search my database (saleforce’s leads and contacts) and update existing user’s info.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:10:30+00:00Added an answer on May 17, 2026 at 6:10 pm

    You should execute a query on lead and contact using the email address as the criteria for you query. If it returns 0 records for the Lead and 0 records for the Contact, then you would create a new Lead.

    Your query could return more than 1 record for either Lead or Contact, so you will have to develop a strategy to handle that case. Email address is not required to be unique.

    public class StackOverflowExample {
    
    public static void test(String email) {
        handleNewEmail(email);
    }
    
    private static void handleNewEmail(String email) {
        List<Lead> leads = [select id, email from Lead where email = :email];
        List<Contact> contacts = [select id, email from Contact where email = :email];
        if (leads.size() == 0 && contacts.size() == 0) {
            //Create new lead
            insert new Lead(Company = 'Lead Company', FirstName = 'firstname', LastName = 'lastname', Email = email);
        } else if (leads.size() == 1) {
            // Update this lead
            leads[0].FirstName = 'newfirstname';
            update leads;
        } else if (contacts.size() == 1) {
            // Update this contact
            contacts[0].FirstName = 'newfirstname';
            update contacts;
        } else {
            // Must be more than 1 contact or lead
            System.debug('\nMore than 1 contact or lead.');
        }
    }
    

    }

    The other option is to execute a search limiting the scope to email fields.

    public class StackOverflowExample {
    
    public static void test(String email) {
        handleNewEmail(email);
    }
    
    private static void handleNewEmail(String email) {
        List<List<SObject>> searchResults = [FIND :email IN Email Fields RETURNING 
                    Lead(Id, FirstName, LastName), 
                    Contact(Id, FirstName, LastName)];
        List<Lead> leads = ((List<Lead>)searchResults[0]);
        List<Contact> contacts = ((List<Contact>)searchResults[1]);
        if (leads.size() == 0 && contacts.size() == 0) {
            //Create new lead
            insert new Lead(Company = 'Lead Company', FirstName = 'firstname', LastName = 'lastname', Email = email);
        } else if (leads.size() == 1) {
            // Update this lead
            leads[0].FirstName = 'newfirstname';
            update leads;
        } else if (contacts.size() == 1) {
            // Update this contact
            contacts[0].FirstName = 'newfirstname';
            update contacts;
        } else {
            // Must be more than 1 contact or lead
            System.debug('\nMore than 1 contact or lead.');
        }
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to APEX ( well, just started using it ~an hour ago),
I am new in apex programming. I am using site.com. I am creating New
We need to add an attachment for new contact. We are using APEX class
I have written a working class in Apex. It is an Email service extender,
Hello Folks, I am really new to Apex Development and currently trying to write
I'm new to Apex and have just downloaded Eclipse to get to work Eclipse
I am new to salesforce application. I am using Apex. My requirement is to
I am new to using APEX PL/SQL and apologise in advance if my question
I'm taking an online class with Google's Apex system, and would like to be
I am new to salesforce and apex as well. trying to use to understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.