How to detach articles detached from the Chat Transcript from the Case in Salesforce?

How to detach articles detached from the Chat Transcript from the Case in Salesforce?

When the Articles are detached from the Salesforce Chat Transcripts, the LinkedArticle Object/Entity records will be deleted. To detach Articles from the Salesforce Cases, we have to remove the relevant records from the CaseArticle Object/Entity.

In the following Sample Flow:

  1. We first check whether the Article is attached to the Chat Transcript.
  2. Using Get Records Element to fetch the Chat Transcript records. We need Case Id. So, we are fetching it.
  3. Using Get Records Element to fetch the Case Articles.
  4. Using Decision Element, we are checking whether the Case Articles are fetched.
  5. If the Case Articles are found, then using Delete Record Elements, we are deleting the Case Articles.

Sample Flow:

Start Element Configuration:

Fetch Chat Transcript:

Fetch Case Articles:

Case Articles Check:

Delete Case Articles:

If you would like to use Apex Trigger, then you can check the following Apex Trigger.

In the following sample Apex Trigger, we are using after delete event LinkedArticle object/entity.

  1. We are checking whether the LinkedEntityId is Chat Transcript Id.
  2. We are storing the Knowledge Article Ids and the Chat Transcript Ids.
  3. We are fetching the Chat Transcripts to get the related Case Ids.
  4. We are fetching the Case Articles using the Case Ids and the Knowledge Article Ids.
  5. If Case Articles are present, then they are deleted  detach articles detached from the Chat Transcript from the Case in Salesforce.

Sample Apex Trigger:

trigger LinkedArticleTrigger on LinkedArticle ( after delete ) {
    
    Set < Id > setKAIds = new Set < Id >();
    Set < Id > setChatIds = new Set < Id >();
    
    for ( LinkedArticle objLA : trigger.old ) {
        
        String strLinkedEntityId = objLA.LinkedEntityId;
        
        if ( strLinkedEntityId.startsWith( '570' ) ) {
            
            setKAIds.add( objLA.KnowledgeArticleId );
            setChatIds.add( objLA.LinkedEntityId );
            
        }
        
    }
    
    if ( setChatIds.size() > 0 ) {
        
        Set < Id > setCaseIds = new Set < Id >();
        
        for ( 
            LiveChatTranscript objCT : [
                SELECT CaseId
                FROM LiveChatTranscript
                WHERE Id IN: setChatIds
            ]
        ) {
            
            setCaseIds.add( objCT.CaseId );
            
        }
        
        if ( setCaseIds.size() > 0 ) {
            
            List < CaseArticle > listCAs = [
                SELECT Id
                FROM CaseArticle 
                WHERE CaseId IN:setCaseIds
                AND KnowledgeArticleId IN: setKAIds
            ];
            
            if ( listCAs.size() > 0 ) {
                
                delete listCAs;
                
            }
            
        }
    }

}

SOQL for LinkedArticle:

SELECT Id, LinkedEntityId
FROM LinkedArticle

SOQL for CaseArticle:

SELECT Id, CaseId
FROM CaseArticle

If you have an use case to attach articles attached to the Chat Transcript to Case in Salesforce, please check the following:

Leave a Reply