2010-09-23 54 views
0

我是Apex開發新手,我開始在Apex的項目中工作。我想這樣做以下操作:使用電子郵件服務如何在Salesforce中添加新銷售線索並更新現有銷售線索


//我想這部分
從電子郵件中得到EMAILADDRESS //知道如何做到這一點
儼然已經exisiting鉛或接觸的同樣EMAILADDRESS
更新自己的信息
其他
創造新的領導


我想叩頭浩w我可以搜索我的數據庫(saleforce的銷售線索和聯繫人)並更新現有用戶的信息。

回答

2

您應該使用電子郵件地址作爲查詢條件對潛在客戶和聯繫人執行查詢。如果它返回0記錄的聯繫人和0記錄的聯繫人,那麼你會創建一個新的潛在客戶。

您的查詢可能會爲Lead或Contact返回多於1條記錄,因此您必須制定處理該案例的策略。電子郵件地址不必是唯一的。

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

}

0

在Salesforce內置幫助中,您可以找到一些info about the InboundEmail object。該頁面還包含2個關於根據從郵件中提取的電子郵件地址處理聯繫人和潛在客戶的示例。

相關問題