Cosmos DB trigger is not working after upgrading to isolated process model

lakshmi 651 Reputation points
2024-06-14T18:51:01.9+00:00

We have migrated the azure function code to Isolated process model by reffering the documents from MS and Github,

 [Function("log-available-agents")]
 public async Task Run(
     [CosmosDBTrigger(
         databaseName: "%CosmosDatabaseId%",
         containerName: "xyz",
         Connection = "CosmosDbConnection",
         LeaseContainerName = "leases",
         CreateLeaseContainerIfNotExists = true)] IReadOnlyList<TodoItems> input,
     FunctionContext context)
 {
     var logger = context.GetLogger<CosmosDbDataTrigger>();
     var tableClient = _tableServiceClient.GetTableClient("agentLogs");

     foreach (var input in inputs)
     {
         try
         {
             string jsonContent = JsonConvert.SerializeObject(input);
             var logenity = JsonConvert.DeserializeObject<logenity>(jsonContent);
             TodoItems item = JsonConvert.DeserializeObject<TodoItems>(jsonContent);
             logenity.PartitionKey = agentDetailSourceEntity.EmailId;
             agentLogElogenityntity.AgentLimit = agentDetailSourceEntity.AgentLimit;

             if (item.IsRejectionLimitReached)
             {


In this cosmos db trigger , trigger is getting triggered whenever a new item is added or deleted form the cosmos db table. but the input which we get doesnt contain all the data which is there in the cosmos db. It only contains the id, conversation id .. and but not the entity specific data. We have defined few many properties in 'todeoitems' entity which is in comso db table and there all these properties are updated correctly, but when comes to trigger its not showing full properties data and not even the Etag , its getting null

How can we resolve this issue.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,610 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. hossein jalilian 5,095 Reputation points
    2024-06-14T21:22:33.3266667+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    If you need access to the entire document, you have a couple of options:

    • Instead of using CosmosDBTrigger, which provides a simplified document, you can switch to using the CosmosDB input binding. This allows you to specify the SQL query to retrieve the document. Here’s a basic example:
        [Function("log-available-agents")]
      

    public async Task Run( [CosmosDB( databaseName: "%CosmosDatabaseId%", collectionName: "xyz", ConnectionStringSetting = "CosmosDbConnection", SqlQuery = "SELECT * FROM c")] IEnumerable<TodoItems> input, FunctionContext context) { foreach (var item in input) { // Process each TodoItems document } }

      
    - If switching to `CosmosDB` binding is not feasible, you can enrich the data received from `CosmosDBTrigger` by querying Cosmos DB again within your function to fetch the complete document using the document ID retrieved from the trigger.
    
      ```csharp
      [Function("log-available-agents")]
    public async Task Run(
        [CosmosDBTrigger(
            databaseName: "%CosmosDatabaseId%",
            collectionName: "xyz",
            Connection = "CosmosDbConnection",
            LeaseContainerName = "leases",
            CreateLeaseContainerIfNotExists = true)] IReadOnlyList<Document> input,
        FunctionContext context)
    {
        var cosmosClient = new CosmosClient("connectionString");
        var container = cosmosClient.GetContainer("databaseId", "xyz");
    
        foreach (var doc in input)
        {
            var id = doc.GetPropertyValue<string>("id");
            var partitionKey = doc.GetPropertyValue<string>("partitionKey");
    
            var response = await container.ReadItemAsync<TodoItems>(id, new PartitionKey(partitionKey));
            var todoItem = response.Resource;
    
            // Now `todoItem` should contain the complete document with all properties
        }
    }
      
    

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful

    0 comments No comments