Skip to content

Instantly share code, notes, and snippets.

@CcDora
Last active November 30, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CcDora/1c7b7e320cb744cd5bddb664b20f7e74 to your computer and use it in GitHub Desktop.
Save CcDora/1c7b7e320cb744cd5bddb664b20f7e74 to your computer and use it in GitHub Desktop.
AWS lambda function: Create a Dynamics Case (incident) record based on input data
//AWS lambda function
//Create a Dynamics Case (Incident) record based on input data
//Change the following variables: token, dynamicsLookupHost, accountId, callerPhoneNumber
/*Invoke with:
{
"currentIntent": {
"dialogState": "ReadyForFulfillment",
"intentName": "OpenDynamicsIncident",
"message": null,
"responseCard": null,
"slotToElicit": null,
"slots": {
"caseDescription": "Not able to find delivery contact",
"caseTitle": "Delivery issue"
},
"confirmationStatus": "confirmed"
}
}
*/
const https = require('https');
exports.handler = (event, context, callback) => {
// Dynamics API authentication token and CRM base URL
var token = 'xxxxx';
const dynamicsLookupHost = "company.crm.dynamics.com";
// Dynamics Account ID or Contact ID
var accountId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
// Caller's phone number
var callerPhoneNumber = "xxxxxxxxxx";
// Amazon Lex response parameters
var lexResponseContent = "message";
jsonLexResponse = {
"sessionAttributes": {
"key1": "value1",
"key2": "value2"
},
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": lexResponseContent
}
}
};
//Building Dynamics API request
var callRequestHeaders = {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json; charset=utf-8',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
'Accept': 'application/json'
};
var options = {
host: dynamicsLookupHost,
path: "/api/data/v8.2/incidents",
method: "POST",
headers: callRequestHeaders
};
var incident = {};
incident.title = "" + event.currentIntent.slots.caseTitle;
incident.statuscode = 1; // 1 -> In Progress incident state
incident.description = "" + event.currentIntent.slots.caseDescription + "\nCaller:" + callerPhoneNumber;
incident.severitycode = 1; // 1 -> Normal priority
incident.caseorigincode = 1; // 1 -> Case opened by phone
incident["customerid_account@odata.bind"] = "accounts(" + accountId + ")";
var incidentRequestBody = JSON.stringify(incident);
console.log('Attempting to create a Case in Dynamics CRM...');
var incidentRequestHttpsClient = https.request(options, function (response) {
var insertIncidentResponse = "";
response.on('data', function (dataChunk) {
insertIncidentResponse += dataChunk;
});
response.on('end', function () {
jsonLexResponse.dialogAction.message.content = "Successfully created new Dynamics Incident.";
callback(null, jsonLexResponse);
});
});
incidentRequestHttpsClient.on('error', function (e) {
callback(e);
});
incidentRequestHttpsClient.write(incidentRequestBody);
incidentRequestHttpsClient.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment