6

There are many posts about creating Case threadIds with Apex, notably Case.Thread_Id, is Thread_Id a field on Case object?

As the posters have noted, the SFDC rules of the threadId are subject to change over time and the only reliable way that I've seen is to do this:

// To be replaced with a real case Id.
Id caseId = '500c0000008kJL5'; 

List<Messaging.RenderEmailTemplateBodyResult> renderResults =
  Messaging.renderEmailTemplate(null, caseId, new List<String>{'{!Case.Thread_Id}'});

System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());

HOWEVER ... The method call Messaging.renderEmailTemplate burns a SOQL (see doc)

So you can't prepopulate your Cases in an afterInsert or beforeUpdate Trigger because a transaction can be up to 2,000 (Platform Events), up to 10,000 (direct Apex DML) or more, typically, 200 rows. All of these are > 100 SOQL limit.

So, before I go to the trouble of doing this in a chained set of async tasks; is there more recent thinking on this subject of creating ThreadIds?

2

2 Answers 2

2

For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.

1
  • yes -- a difficult issue as one wants the threadId for use in VF email templates based on deltas in Case; so PBs and WFR need to be gated by presence of threadId before executing and async Txn, even with soql limit of 200 can't consume all 200 doing renderEmailTemplate as that leaves no headroom for soqls done by the aforementioned WFR/PBs. Tuning for the right "batch size" in the async txn is "guessing/leaving enough headroom and hoping for the best". Your remark if you need this... is spot-on
    – cropredy
    Commented Oct 25, 2018 at 1:43
0

This solution is working perfectly....:)

I am sending out email from Apex by passing case id and using template.

I've replaced thread_id from html body with the new formula string, and on case I can see all reply's getting captured in same thread.

/*Configuring thread ID*/

String caseId = cri.CaseId;

Case_ThreadID = 'ref:!' + UserInfo.getOrganizationId().left(5)  + '0' + UserInfo.getOrganizationId().mid(11,4) + '.!'  + caseId.left(5) + '0' + caseId.mid(10,5)+':ref';

 
//replacing template threadid from HTML body//

if(htmlBody.contains('{!Case.Thread_Id}')){

    htmlBody = htmlBody.replace('{!Case.Thread_Id}', Case_ThreadID);

}

Thanks!

1
  • I don't think this is a great answer for this question because it assumes what the format of the thread id is, and as OP stated, As the posters have noted, the SFDC rules of the threadId are subject to change over time.
    – Derek F
    Commented Oct 29, 2023 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .