0

Here i am calling an apex method from lwc I want to transfer the chat to a particular skill so I am using the below the apex code to transfer the chat request but i am unable to update the AgentWork status

Please tell me how i can acheive transfer chat using Apex code

public class TransferChat {

    @AuraEnabled
    public static void chatTransfer(Id liveChatId, String transferTo) {
        List<AgentWork> agentWorkRecord = [
            SELECT Id, Status,PendingServiceRoutingId
            FROM AgentWork
            WHERE WorkItemId = :liveChatId
        ];

        PendingServiceRouting psrObj = new PendingServiceRouting(
            CapacityWeight = 1,
            IsReadyForRouting = FALSE,
            RoutingModel = 'MostAvailable',
            RoutingPriority = 1,
            RoutingType = 'SkillsBased',
            ServiceChannelId = getChannelId(),
            WorkItemId = liveChatId,
            PushTimeout = 0
        );
        insert psrObj;
        AgentWork agentRecord = new AgentWork();
        agentRecord.PendingServiceRoutingId = psrObj.id;
        agentRecord.Id = agentWorkRecord[0].Id;
        agentRecord.Status = 'Transferred';
        update agentRecord;

        psrObj = [
            SELECT id, IsReadyForRouting
            FROM PendingServiceRouting
            WHERE id = :psrObj.id
        ];
        SkillRequirement srObj = new SkillRequirement(
            RelatedRecordId = psrObj.id,
            SkillId = getSkillId(transferTo),
            SkillLevel = 5
        );
        insert srObj;
        psrObj.IsReadyForRouting = TRUE;
        update psrObj;
    }

    public static String getChannelId() {
        ServiceChannel channel = [
            SELECT Id
            FROM ServiceChannel
            WHERE RelatedEntity = 'LiveChatTranscript'
        ];
        return channel.Id;
    }

    public static Id getSkillId(String transferUser) {
        List<User> userId = [Select Id from User where Name = :transferUser];
        List<Skill> skill = new List<Skill>();
        if (!userId.isEmpty()) {
            skill = [
                SELECT Id 
                FROM Skill 
                WHERE DeveloperName = :'S' + userId[0].Id
            ];
        }
        if (!skill.isEmpty()) {
            return skill[0].Id;
        } else {
            return null;
        }
    }
}
0

You cannot update the Agent Work record. But if you update the owner of the Work Item record (in your case the LiveChatTranscript record) after you update the PendingServiceRouting record with IsReadyForRouting = TRUE it will do the trick.

For example, you can change the owner to the original group to which it was assigned at the beginning.

Try this modification to your code:

public class TransferChat {

    @AuraEnabled
    public static void chatTransfer(Id liveChatId, String transferTo) {
        List<AgentWork> agentWorkRecord = [
            SELECT Id, OriginalGroupId // Query for the original group Id
            FROM AgentWork
            WHERE WorkItemId = :liveChatId
        ];

        PendingServiceRouting psrObj = new PendingServiceRouting(
            CapacityWeight = 1,
            IsReadyForRouting = FALSE,
            RoutingModel = 'MostAvailable',
            RoutingPriority = 1,
            RoutingType = 'SkillsBased',
            ServiceChannelId = getChannelId(),
            WorkItemId = liveChatId,
            PushTimeout = 0
        );
        insert psrObj;

        // All this section won't work
        /*
        AgentWork agentRecord = new AgentWork();
        agentRecord.PendingServiceRoutingId = psrObj.id;
        agentRecord.Id = agentWorkRecord[0].Id;
        agentRecord.Status = 'Transferred';
        update agentRecord;
        */

        psrObj = [
            SELECT id, IsReadyForRouting
            FROM PendingServiceRouting
            WHERE id = :psrObj.id
        ];
        SkillRequirement srObj = new SkillRequirement(
            RelatedRecordId = psrObj.id,
            SkillId = getSkillId(transferTo),
            SkillLevel = 5
        );
        insert srObj;
        psrObj.IsReadyForRouting = TRUE;
        update psrObj;

        // Update the record owner
        LiveChatTranscript liveChat = [
            SELECT Id, OwnerId
            FROM LiveChatTranscript
            WHERE Id = :liveChatId
            LIMIT 1
        ];
        liveChat.OwnerId = agentWorkRecord[0].OriginalGroupId;
        update liveChat;
    }

    public static String getChannelId() {
        ServiceChannel channel = [
            SELECT Id
            FROM ServiceChannel
            WHERE RelatedEntity = 'LiveChatTranscript'
        ];
        return channel.Id;
    }

    public static Id getSkillId(String transferUser) {
        List<User> userId = [Select Id from User where Name = :transferUser];
        List<Skill> skill = new List<Skill>();
        if (!userId.isEmpty()) {
            skill = [
                SELECT Id 
                FROM Skill 
                WHERE DeveloperName = :'S' + userId[0].Id
            ];
        }
        if (!skill.isEmpty()) {
            return skill[0].Id;
        } else {
            return null;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.