0
votes

I am trying to trigger azure function using Service Bus Queue trigger, which works fine. What I also want to do is use Cosmos Db input binding on the same function. The function gets triggered with the specific document and gets result with the input binding for simple query like:

Select * from c

But with a WHERE clause the same query does not return anything, though the condition is right and data is there in the DB against the contract Id passed in from the trigger:

Select * from c WHERE c.contractId = {contractId}

Following is the code of the Azure Function

#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void Run(IReadOnlyList<Document> input, ILogger log, IEnumerable<dynamic> documents)
{
    if (input != null && input.Count > 0)
    {
        log.LogInformation("Documents modified " + input.Count);
        log.LogInformation("First document Id " + input[0]);
    }
}

function.json

 {
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "tripend",
      "connection": "mobiiot_RootManageSharedAccessKey_SERVICEBUS"
    },
    {
      "type": "cosmosDB",
      "name": "documents",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "connectionStringSetting": "mobiiot_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "SELECT * from c where c.contractId= {contractId}"
    }
  ]
}

Trigger Data coming into Azure Function:

    {"vin":"WP0ZZZ99ZJS167001","milage":780.3333,"contractId":"19277",
"lat":51.47404,"lon":-0.45299000000000006,"noOfHardBreaks":0,"fuelConsumptionRate":22,
"speed":96,"status":"droppedOff","EventProcessedUtcTime":"2018-12-10T09:14:51.6474889Z",
"PartitionId":0,"EventEnqueuedUtcTime":"2018-12-10T09:14:51.5350000Z",
"IoTHub":{"MessageId":null,"CorrelationId":null,"ConnectionDeviceId":"WP0ZZZ99ZJS167001",
"ConnectionDeviceGenerationId":"636795108399273130",
"EnqueuedTime":"2018-12-10T09:14:51.5470000Z","StreamId":null}}
1
What do you want to retrieve using input binding? Your code seems to contain only the cosmosdb trigger. Take a look at input binding examples.Jerry Liu
Thanks Jerry, I have updated the question a bitHaider
Could you share function.json and what's the data type of contractId, String or others e.g Number?Jerry Liu

1 Answers

0
votes

It seems your code doesn't match function.json. To set placeholder like {contractId}, we need to define a custom type to deserialize the JSON, so that function code could find contractId among the coming data.

Have a try at code below.

#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void  Run(QueueItem myQueueItem, ILogger log, IEnumerable<dynamic> documents)
{      
    foreach(var doc in documents)
    {
        log.LogInformation((string)doc.id);
    }
}
public class QueueItem
{
    public string contractId { get; set; }
}