Skip to content

Instantly share code, notes, and snippets.

@tdutch1
Last active January 19, 2017 02:10
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 tdutch1/a14c60eed1a005c074f317b581cd6f01 to your computer and use it in GitHub Desktop.
Save tdutch1/a14c60eed1a005c074f317b581cd6f01 to your computer and use it in GitHub Desktop.
Webhook triggered C# Azure Function: Returns the number of hours between now (UTC) and a given UTC-formatted date/time
----------------------------
Function Overview
----------------------------
Purpose: This Azure Function returns the number of hours between now (UTC) and a given UTC-formatted date/time.
Function name: GetHoursSinceUtcDateTime
Function template: GenericWebHook-CSharp
----------------------------
Intended Uses
----------------------------
- Can use in conjuction with an Azure Logic App when UTC formatted date/time values are available.
- Useful for getting the number of hours since a date/time value in Dynamics 365 (CRM). When retrieved into a Logic App, date/time values from CRM are in UTC format, ready to pass to this Function.
----------------------------
Bindings (Inbound and Outbound JSON)
----------------------------
Inbound JSON format (example): {"dateUtc":"2016-12-05T20:45:10Z"}
Outbound JSON format (example): {"hours":995.03157326238886}
----------------------------
Deployment Instructions
-----------------------------
- Create a new Function App in the Azure Portal or select existing Function app
 - For the App name, enter something like ContosoGeneralCrmFunctions
   - A Function App can contain multiple functions, so it's best to keep the App name related to the company, project or other grouping
- If prompted to select a function scenario, bypass it and use the "Create your own custom function" option.
- Function template to select: GenericWebHook-CSharp
- Function name: GetHoursSinceUtcDateTime
- Note: There are no changes to function.json; it is provided in this Gist only for completeness.
----------------------------
Testing the Function
----------------------------
- Testing within the Azure Portal while editing the function:
- Click the Test button while editing the Function in the Azure Portal.
- HTTP Method: POST
- Request body: {"dateUtc":"2016-12-05T20:45:10Z"}
- Click the Run button
- The Output should show "200 OK" and show a JSON object with an "hours" property and a decimal number.
----------------------------
Usage Scenarios
----------------------------
- Azure Logic App to report on neglected cases in Dynamics 365 CRM:
http://mscrmrocks.blogspot.com/2017/01/azure-logic-app-to-report-on-neglected.html
----------------------------
Documentation References
----------------------------
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook
----------------------------
Keywords
----------------------------
dynamics 365 crm azure function azure functions c# webhook http trigger utc gmt datetime date time hours difference
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"webHookType": "genericJson",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false
}
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.Globalization;
using Newtonsoft.Json;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Function triggered");
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
if (data.dateUtc == null) {
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = "Please pass date as UTC in the input object"
});
}
try
{
DateTime dateProvided = DateTime.Parse($"{data.dateUtc}");
return req.CreateResponse(HttpStatusCode.OK, new {
hours = (DateTime.UtcNow - dateProvided).TotalHours
});
}
catch (Exception ex)
{
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = ex.Message
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment