+ Start a Discussion
anu jainanu jain 

how to change case owner field from live chat transcript case owner field

Hi All,
I have written the trigger for changing the case owner field from livechattranscript object  case owner field.If one chat request came and agent accept that chat and after that agent assign that request to new agent.If we assign the same case to another agent.when request came to another agent and agent accept that chat then transcript case owner field is changing to "who accept the Chat" but when case is linked to that chat then case owner field is not changing to current agent name .Please anyone suggest how we can write the trigger for that .I have written the trigger but its not working.
when chat request is coming then its not associating case to that chat.
My code below.

trigger LiveChatTranscriptTrigger on LiveChatTranscript (after insert) {

    if(Trigger.isInsert && Trigger.isAfter){        
        Set<String> caseIds = new Set<String>();
        for ( LiveChatTranscript transcript : Trigger.new ) {
            if ( !String.isBlank( transcript.caseId ) ) {
                caseIds.add( transcript.caseId );
                system.debug(caseIds);
            }
            
        }
        System.debug('CaseIds' +caseIds);
        if ( caseIds.size() > 0 ) {
            List<Case> cases = new List<Case>([SELECT id from Case WHERE Id IN :caseIds ORDER BY createdDate ASC ]);            
            Map<String, Case> CasesIdsMap = new Map<String, Case>();
            System.debug('2');
            for ( Case cs : cases ) {
                CasesIdsMap.put( cs.id, cs );
            }
            System.debug( 'CasesIdsMap=' + CasesIdsMap );
            List<case> caseToUpdate = new List<case>();
            
            for ( LiveChatTranscript transcript : Trigger.new ) {
             System.debug('3');
                if ( !String.isBlank( transcript.caseId ) ) {
                    Case cs = CasesIdsMap.get( transcript.caseId );
                    if ( cs != null ) {
                        caseToUpdate .add( new case(
                        ownerId=transcript.ownerId,
                            id = transcript.caseid
                            //caseId = cs.id
                            
                        ));
                    }
                    
                }
                
            }
            if ( caseToUpdate .size() > 0 ) {
                System.debug( 'caseToUpdate : ' +caseToUpdate );
                 update caseToUpdate ;
            }
            
        }
    }
    
}
And Trigger is not genering the log also
Ambuj Singh 1Ambuj Singh 1
Please use the below trigger

public class LiveChatTranscriptTriggerHandler {
    

    public static void onAfterUpdate(list<LiveChatTranscript> liveChatTranscriptList, Map<Id, LiveChatTranscript> liveChatTranscriptOldMap){
        System.debug('inside onAfterUpdate' );
        updateCaseOwner(liveChatTranscriptList, liveChatTranscriptOldMap);
    }
    

    
    public static void updateCaseOwner(list<LiveChatTranscript> liveChatTranscriptList, Map<Id, LiveChatTranscript> liveChatTranscriptOldMap){
        Map<Id, Id> transcriptCaseMap = new Map<Id, Id>();
        List <case> lstOfCases =  new List <case>();
        System.debug('inside updateCaseOwner' );
        for (LiveChatTranscript transcript : liveChatTranscriptList){
             System.debug('inside for loop' );
            LiveChatTranscript transcriptOld = liveChatTranscriptOldMap.get(transcript.id);
            
            if(transcript.OwnerId != transcriptOld.OwnerId ){
                
                case c = new case(id=transcript.caseId);
                c.ownerId = transcript.OwnerId;
                lstOfCases.add(c);
               
            }
            
            
        }
        if(lstOfCases.size()>0){
            
            update lstOfCases;
            
        }
        
        
        
    }
    
}
prathyusha nalaboluprathyusha nalabolu
create a process builder with the criteria : 1.Livetranscript.caseid doesnotequals Null AND LivechatTranscript.OwnerId ischanged boolean false and under actions select action type as update a record->select related record as caseId, select the action as OwnerId Formula as $user.Id.
2.Criteria as liveChattranscript.CaseId doesnotequals NUll AND liveChatTranscript.OwnerId doesnotequals formula liveChatTranscript.Case.OwnerId --> select related record as caseId, select the action as Ownerid formula as liveChatTranscript.OwnerId.
Dont forget to choose stop and select evaluate next criteria for 1 condition.
prathyusha nalaboluprathyusha nalabolu
Please follow the below screenshots.
picture 1-criteria 1action 1criteria 2action2
Charles ThompsonCharles Thompson

There are some problems with the above solution in Process Builder.

I agree that Process Builder (PB) is a better choice for this task than using Apex - clicks, not code!  Apex quickly gets out of date (API versions increate 3 times per year) and adds to technical debt, especially if undocumented like the sample code.

The business problem (requirement) is to ensure that the connected case's owner changes to match the live chat transcript when its owner changes.

The solution is to configure a PB to trigger on the LiveChatTranscript object.  If you're following PB best practices, you should have only ONE PB per object, so if you already have a PB triggered on LiveChatTranscript, then add the following to that existing PB.

Decision: (blue diamond)
Name: Transcript Owner Changed?
Criteria:
1. [LiveChatTranscript].OwnerID > Is Changed > Boolean > True
2. [LiveChatTranscript].CaseID > Is null > Boolean > False
3. [LiveChatTranscript].Case.OwnerIsQueue__c > Equals > Boolean > False 
<This is a formula field on Case: IF(LEFT(OwnerId, 3) = '00G', True, False)>
4. [LiveChatTranscript].Case.OwnerId > Does not equal > Field Reference > [LiveChatTranscript].OwnerID
All of the conditions are met (AND)

(Note: if the case owner is a queue, which is unlikely but possible, we want to leave the case owner unchanged)

Immediate Action:
Action Type: Update Records
Name: Update Case Owner
Record: Select a record related to the LiveChatTranscript > Case ID  --> [LiveChatTranscript].Case ID
No criteria - just update the records!
New field value:
Owner Id > Field Reference > [LiveChatTranscript].OwnerId

I can't understand why the earlier "solution" has two decisions.  In that example, the first decision will update the case's owner to match the current user's ID.  I'm not sure why this is there, because it wasn't in the original requirement or Apex code sample.  The requirement is to change the case owner to match the transcript owner, not the current user (which is unlikely to be the transcript owner, since the owner wants to push the chat to someone else).  The first decision has some of the correct criteria (there is a case, the transcript owner is changed), but it doesn't check if the case owner is different from the transcript owner.  We only want to perform the action if all three of these rules are true (plus my specific rule that the case owner must be a User, not a Queue).   We certainly don't want to change the case owner to the current user!   

The second decision checks that there is a case and its owner doesn't match the transcript owner.  OK, fine.  But this will be evaluated any time any field on the transcript changes. That's not our original criteria - we want to update the case owner only when the transcript owner changes.  Hence our first decision with four criteria.  If case owner changes in future,  that's OK - it doesn't always have to match the transcript owner.

Aarti MaliAarti Mali
Hi , did anyone get it working with Process builder or Trigger? 
Keith AshleyKeith Ashley
Hi Charles,   I tried following the steps you outlined above.  I created the exact formula field on the case hower when I try to add the [LiveChatTranscript].Case.OwnerIsQueue__c  field I cannot find it.  Any help is appreciated.  thank you