如何在Salesforce中添加新潜在客户并更新现有潜在客户

时间:2010-09-23 05:31:56

标签: salesforce apex-code

我是Apex开发的新手,我开始在Apex开展我的项目。我想做以下几点:


使用电子邮件服务//我想到了这一部分 从电子邮件中获取emailAddress //知道如何执行此操作 如果已经存在领先或联系同一个emailAddress
  更新他的信息
否则
  创造新的领导


我想知道如何搜索我的数据库(saleforce的潜在客户和联系人)并更新现有用户的信息。

2 个答案:

答案 0 :(得分:2)

您应该使用电子邮件地址作为查询条件,对潜在客户和联系人执行查询。如果它返回Lead的0记录和Contact的0记录,那么您将创建一个新的Lead。

您的查询可以为潜在客户或联系人返回多条记录,因此您必须制定策略来处理该情况。电子邮件地址不必是唯一的。

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.');
    }
}

}

另一种选择是执行将范围限制为电子邮件字段的搜索。

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.');
    }
}

}

答案 1 :(得分:0)

在Salesforce内置帮助中,您可以找到一些info about the InboundEmail object。该页面还包含2个基于从消息中提取的电子邮件地址使用“联系人”和“潜在客户”的示例。