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 {

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>();
      skill = [Select Id From Skill Where DeveloperName = :'S'+userId[0].Id];

      return skill[0].Id;
     return null;    


Your Answer

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

Browse other questions tagged or ask your own question.