Name

global.AgentFirstResponseWaitTime

Description

Populate first response wait time for interaction.

Script

var AgentFirstResponseWaitTime = Class.create();
AgentFirstResponseWaitTime.prototype = {
  process: function(current) {
  	var interactionGR = new GlideRecord("interaction");
  	interactionGR.addActiveQuery();
  	interactionGR.addQuery("channel_metadata_table", "sys_cs_conversation");
  	interactionGR.addQuery("channel_metadata_document", current.conversation);
  	interactionGR.query();
  	// Do nothing when interaction record not found, interaction is not assigned and interaction first response wait time is populated
  	if(!interactionGR.next() || interactionGR.assigned_to.nil() || !interactionGR.first_response_wait_time.nil())
  		return;

  	var workItemGR = new GlideRecord('awa_work_item');
  	workItemGR.addActiveQuery();
  	workItemGR.addQuery('document_id', interactionGR.getUniqueValue());
  	workItemGR.addQuery('state', 'accepted');
  	workItemGR.query();
  	if(!workItemGR.next())
  		return;

  	// If auto greeting message is set, verify whether the current message is agent typed message
  	if(workItemGR.queue.initial_agent_response) {
  		var csMessageGR = new GlideRecord("sys_cs_message");
  		csMessageGR.addQuery("conversation", current.conversation);
  		csMessageGR.addQuery("direction", "outbound");
  		csMessageGR.addQuery("is_agent", true);
  		csMessageGR.addQuery("sequence", "<", current.sequence);
  		csMessageGR.addQuery("message_type", "!=", "action");
  		csMessageGR.query();
  		if(csMessageGR.getRowCount()!=1)
  			return;
  	}
  	// Calculate time duration for the first response from agent taken
  	var timeTakenTillTheMessageSent = GlideDateTime.subtract(interactionGR.live_handoff_time.getGlideObject(), current.sys_created_on.getGlideObject());
  	var waitDuration = new GlideDuration(interactionGR.wait_time.getGlideObject().getNumericValue());
  	var firstResponseWaitTime = timeTakenTillTheMessageSent.subtract(waitDuration);
  	interactionGR.setValue("first_response_wait_time", firstResponseWaitTime);
  	interactionGR.update();
  },

  type: 'AgentFirstResponseWaitTime'
};

Sys ID

c83a218d0f703010d106ac5397767e79

Offical Documentation

Official Docs: