Implementing Azure Cosmos DB Stored Procedure Triggers
HOTSPOT
You have a web service that is used to pay for food deliveries. The web service uses Azure Cosmos DB as the data store.
You plan to add a new feature that allows users to set a tip amount. The new feature requires that a property named tip on the document in Azure Cosmos DB must be present and contain a numeric value.
There are many existing websites and mobile apps that use the web service that will not be updated to set the tip property for some time.
How should you complete the trigger?
NOTE: Each correct selection is worth one point.

Community Insight
The question tests the ability to use JavaScript within a Cosmos DB stored procedure to check for property existence and modify the request body before persistence.
Learn how to implement a pre-trigger in Azure Cosmos DB stored procedures to enforce data integrity by setting default values for missing properties.
Learners often confuse trigger types or fail to understand that the `body` of the request must be explicitly updated using `setBody` to persist changes.
Community Discussion (2 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The solution requires identifying the correct JavaScript context and methods within an Azure Cosmos DB stored procedure. First, you must access the current operation's request object usinggetContext.getRequest. Next, you need to validate the document payload (typically accessed via request.getBody or directly as i in some templates) to check if the 'tip' property exists. The standard way to check for a property in a JSON-like object is if (!('tip' in i)). If the property is missing, you set a default value (e.g., 0). Finally, because the request object is immutable by default, you must call r.setBody(i) to update the request with the modified document so that the change is persisted to the database.Why Other Candidate Answers Are Wrong
Alternative selections might involve incorrect API calls likeget.Context which is syntactically invalid in this context, or failing to call setBody, which results in the modification being ignored. Some might incorrectly attempt to throw an error instead of providing a default, which would break existing clients that don't send the tip property.Community Comment Notes
As Ja13 noted, the correct sequence involves getting the request context, checking for the key, and setting the body. MrTeacher confirmed thatgetContext.getRequest retrieves the operation object and r.setBody(i) overwrites the request body to ensure the new value is saved. Exam Strategy
Review the Azure Cosmos DB stored procedure API documentation, specifically focusing on how to manipulate the request object and when to call setBody to persist changes in triggers.
Frequently Asked Questions
Why do I need to call setBody in a Cosmos DB trigger?
The request object is immutable after retrieval. You must explicitly call setBody to push your modified document back into the execution pipeline for storage.
How do I check if a property exists in a Cosmos JS stored procedure?
Use the native JavaScript 'in' operator: if ('propertyName' in documentObject). This returns true if the property exists, regardless of its value.