Name
sn_openframe.OpenframeInteractionUtility
Description
Helper for Open Frame Interactions
Script
var OpenframeInteractionUtility = Class.create();
OpenframeInteractionUtility.prototype = {
initialize: function() {},
CONSTANTS: {
INTERACTION_TABLE: 'interaction',
INTERACTION_CALLBACK_FIELD: 'callback_task',
INTERACTION_ACTIVE_FIELD: 'active',
NOT_IN: 'NOT IN',
INTERACTION_PHONE_TYPE: 'phone',
SYS_ID: 'sys_id',
NUMBER: 'number',
TYPE: 'type',
CALLBACK_REFERENCE_FIELD: 'callback_task',
OPENFRAME_EXTENSIONPOINT: 'sn_openframe.OpenframeCreateInteraction',
CALLBACK_RETRY_ATTEMPT_FIELD: 'retry_attempts',
CALLBACK_LAST_RETRY_FIELD: 'last_retry',
CALLBACK_PLUGIN: 'com.sn.omnichannel.callback'
},
createOrUpdateInteractionForOpenframe: function(context) {
if (!context || !context.interactionSysId) {
return {
"error": "interactionSysId property is not present in the request",
"status": "failure"
};
}
var isUpdate = (context.interactionSysId != -1);
var interaction = new GlideRecord('interaction');
if (isUpdate) {
interaction.addQuery('sys_id', context.interactionSysId);
interaction.query();
interaction.next();
} else {
interaction.initialize();
if (!context.type) {
context.type = 'phone';
}
}
delete context.interactionSysId;
for (var intField in context) {
switch (intField) {
case "state":
if (context[intField] == 'closed_complete' || context[intField] == 'closed_abandoned') {
interaction.setValue('closed_by', gs.getUserID());
}
break;
case "assigned_to":
if (context[intField] == 'current_user') {
context[intField] = gs.getUserID();
}
break;
case "contact":
interaction.setValue('opened_for', context[intField]);
interaction.setValue('verified', context[intField] ? true : false);
break;
default:
break;
}
interaction.setValue(intField, context[intField]);
}
var interactionSysId;
var interactionFields = {};
var result = {};
if (isUpdate) {
interaction.update();
interactionSysId = interaction.getValue('sys_id');
result.operation = 'update';
} else {
interactionSysId = interaction.insert();
result.operation = 'create';
}
result.status = 'success';
for (var intField2 in context) {
switch (intField2) {
case "context_table":
if (interaction.getValue(intField2) != 'interaction_json_blob' && interaction.getValue('context_document')) {
this.insertInteractionRelatedRecord(interactionSysId, interaction.getValue('context_document'), interaction.getValue(intField2));
}
break;
}
interactionFields[intField2] = interaction.getValue(intField2);
}
interactionFields.interactionSysId = interactionSysId;
interactionFields.number = interaction.getValue("number");
result.fields = interactionFields;
return result;
},
interactionTransferForOpenframe: function(context) {
var result = {};
if (!context || !context.interaction) {
result.status = "failure";
result.error = "interaction property is not present in the request";
return result;
}
var interactionTransfer = new GlideRecord('interaction_agent_transfer');
if (!context.from_agent) {
interactionTransfer.setValue('from_agent', gs.getUserID());
}
for (var intFields in context) {
interactionTransfer.setValue(intFields, context[intFields]);
}
var interactionTransferSysId = interactionTransfer.insert();
var data = {
"interactionTransferSysId": interactionTransferSysId
};
return data;
},
updateInteractionContextJson: function(contextSysId, contextJson) {
var interactionContext = new GlideRecord('interaction_json_blob');
if (!gs.nil(contextSysId)) {
interactionContext.get(contextSysId);
} else {
interactionContext.initialize();
}
if (!gs.nil(contextJson)) {
interactionContext.setValue('value', contextJson);
}
if (!gs.nil(contextSysId)) {
interactionContext.update();
} else {
contextSysId = interactionContext.insert();
}
return contextSysId;
},
updateInteractionContext: function(interaction, key, value) {
if (!gs.nil(interaction)) {
var interactionContext = new GlideRecord('interaction_context');
interactionContext.initialize();
interactionContext.setValue('interaction', interaction);
interactionContext.setValue('name', key);
interactionContext.setValue('value', value);
interactionContext.insert();
}
return;
},
insertInteractionRelatedRecord: function(interaction, document_id, document_table) {
var interactionRelatedRec = new GlideRecord('interaction_related_record');
interactionRelatedRec.initialize();
interactionRelatedRec.setValue('document_id', document_id);
interactionRelatedRec.setValue('document_table', document_table);
interactionRelatedRec.setValue('interaction', interaction);
interactionRelatedRec.insert();
},
createInteractionWithPropertyCheck: function(context) {
var result = {};
if (!context || !context.interactionSysId) {
result.status = "failure";
result.error = "interactionSysId property is not present in the request";
return result;
}
// Check if the request is callback and then decide whether to
// create an interaction or not.
var callbackData = this.checkAndHandleCallbackRequest(context);
if (callbackData != null) {
return callbackData;
}
if (gs.getProperty('sn_openframe.create_interaction') === 'true') {
var data = this.createOrUpdateInteractionForOpenframe(context);
return data;
} else {
result.status = "failure";
result.error = "sn_openframe.create_interaction is false";
return result;
}
},
checkAndHandleCallbackRequest: function(context) {
if (GlidePluginManager.isActive(this.CONSTANTS.CALLBACK_PLUGIN) && context.context_table == this.CONSTANTS.INTERACTION_TABLE) {
var interactionGr = new GlideRecord(this.CONSTANTS.INTERACTION_TABLE);
interactionGr.addQuery(this.CONSTANTS.SYS_ID, context.context_document);
interactionGr.addNotNullQuery(this.CONSTANTS.CALLBACK_REFERENCE_FIELD);
interactionGr.query();
if (interactionGr.next()) {
this.updateCallbackRetryContext(interactionGr);
interactionGr.addValue();
if (interactionGr.active == true && interactionGr.getValue("type") == this.CONSTANTS.INTERACTION_PHONE_TYPE) {
return result = {
fields: {
context_table: context.context_table,
context_document: context.context_document,
interactionSysId: context.context_document,
number: interactionGr.getValue('number'),
type: this.CONSTANTS.INTERACTION_PHONE_TYPE
}
};
} else {
var extPoint = new GlideScriptedExtensionPoint().getExtensions(this.CONSTANTS.OPENFRAME_EXTENSIONPOINT);
for (var i = 0; i < extPoint.length; i++) {
if (extPoint[i].isValid({
table: context.context_table,
id: context.context_document,
openframe_config: context.openframe_config
})) {
var interactionDetails = extPoint[i].createInteraction({
table: context.context_table,
id: context.context_document,
openframe_config: context.openframe_config
});
return result = {
fields: {
context_table: context.context_table,
context_document: context.context_document,
interactionSysId: interactionDetails.interactionSysId,
number: interactionDetails.number,
type: this.CONSTANTS.INTERACTION_PHONE_TYPE
}
};
}
}
}
}
}
return null;
},
updateCallbackRetryContext: function(interactionGr) {
var callbackTaskRef = interactionGr.callback_task.getRefRecord();
if (callbackTaskRef.isValidRecord()) {
if (gs.nil(callbackTaskRef.retry_attempts)) {
callbackTaskRef.setValue(this.CONSTANTS.CALLBACK_RETRY_ATTEMPT_FIELD, 0);
} else {
callbackTaskRef.addValue(this.CONSTANTS.CALLBACK_RETRY_ATTEMPT_FIELD, 1);
}
callbackTaskRef.update();
}
},
type: 'OpenframeInteractionUtility'
};
Sys ID
239cea8e53733300a4a7ddeeff7b1207