2011-07-26 33 views
1

我正在編寫一個Windows服務,它將調查我的IMAP4收件箱中的來自客戶端的電子郵件,並基於它們在Salesforce中創建新案例。Salesforce API:如何通過電子郵件參考碼(「[Ref:...:Ref]」)識別個案?

有時候電子郵件會附帶主題中的案例參考碼。例如:「[ref:00FFwxyz.500FFJJS5:ref]」。我想將這些電子郵件分配給代碼標識的現有案例,而不是創建一個新的案例。

我的問題是:是否有一個確定的公式從引用代碼中提取唯一的案例標識符?我已經看到了幾個相反的公式,但它們都看起來像猜測:Blog post on KnowThyCloud.com,Force.com Discussion Board thread

回答

2

找到一個體面的解決方案。我錯誤地稱the post on KnowThyCloud.com猜測。在正確的上下文中,它工作正常。

我的解決方案是在「Formula(文本)」類型的Case記錄上創建一個新的自定義字段。該字段的值是在博客文章中提到的公式:

TRIM(" [ ref:" + LEFT($Organization.Id, 4) + RIGHT($Organization.Id, 4) +"."+ LEFT(Id, 4) + SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(Id, RIGHT(Id, 4), ""), LEFT(Id, 4), ""), "0", "") + RIGHT(Id, 4) + ":ref ] ") 

現在,每一個案例記錄的自定義字段的值是相同的電子郵件中的參考編號,我可以簡單地與Salesforce的API查詢它。

1

我實現了urig的解決方案,它運行良好。

這是一個Apex代碼解決方案,可以定位沒有此字段的案例。

String emailSubject = 'FW: Re: RE: order 36000862A Case: 00028936 [ ref:00D7JFzw.5007Ju10k:ref ]'; 
String caseNumber = null; 
/* 
Extract the text after ref: and before the period. Could use this to locate the organization. 
In the text after the period and before the :ref split into a 4 digit number and remaining number. 
Insert 0's to get ref id. 
*/ 
String patternString = '.*ref:(.{8}).(.{4})(.+):ref.*'; 
Pattern thePattern = Pattern.compile(patternString); 
Matcher matcher = thePattern.matcher(emailSubject); 

if (matcher.matches()) { 
    String caseId = matcher.group(2) + '000000' + matcher.group(3); 
    Case[] matchingCases = [Select CaseNumber from Case where Id = :caseId]; 
    if(matchingCases.size() == 1) { 
     Case theCase = matchingCases[0]; 
     caseNumber = theCase.CaseNumber; 
    }  
} 
0

我們爲了支持包含underrscores新的參考字符串(例如_00DC0PxQg._500C0KoOZS)改性以上簡的代碼段。

String patternString = '.*ref:(.{11}).(.{5})(.+):ref.*'; 
Pattern thePattern = Pattern.compile(patternString); 
Matcher matcher = thePattern.matcher(emailSubject); 

if (matcher.matches()) { 
    String caseId = matcher.group(2) + '00000' + matcher.group(3); 
    system.debug('### '+caseId); 
    Case[] matchingCases = [Select CaseNumber from Case where Id = :caseId]; 
    if(matchingCases.size() == 1) { 
     Case theCase = matchingCases[0]; 
     caseNumber = theCase.CaseNumber; 
    }  
}