Alternate Keys for Integration
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
Lesson: Mastering Alternate Keys for Dataverse Integrations
Introduction: The Foundation of Data Integrity
When working with Dataverse—the underlying data platform for Power Apps and Dynamics 365—the primary method for identifying a specific record is the Globally Unique Identifier (GUID). While the GUID is perfect for internal system operations, it poses a significant challenge when integrating with external systems. External databases, legacy ERPs, or flat-file exports rarely store your Dataverse GUIDs. Instead, they rely on "natural keys," such as an email address, a customer ID, a product SKU, or an employee number.
This is where Alternate Keys become essential. An Alternate Key is a feature in Dataverse that allows you to define a unique business key (one or more columns) that the system can use to reference a record without needing the internal GUID. By implementing Alternate Keys, you bridge the gap between external data silos and the Dataverse ecosystem. This lesson covers how to define, implement, and optimize Alternate Keys to ensure your integrations are reliable, performant, and maintainable.
Understanding the Role of Alternate Keys
In any integration scenario, the most common operation is the "Upsert" (Update or Insert). Without an Alternate Key, an integration process typically follows a three-step dance: query the system to see if the record exists, retrieve the GUID if it does, and then perform the update or create. This process is inefficient, prone to race conditions, and adds significant latency to high-volume data transfers.
Alternate Keys change this pattern by allowing you to perform an Upsert operation directly. You send the record to Dataverse along with your external business key, and the platform handles the logic of checking for an existing record. If the key matches, the system updates the existing record; if not, it creates a new one. This reduces the number of API calls, minimizes network overhead, and simplifies your integration code significantly.
Callout: GUID vs. Alternate Key Think of a GUID as a Social Security Number or a Passport ID—it is the system's way of tracking you uniquely, but it is often unknown to the outside world. An Alternate Key is like your username or email address. It is a piece of information that you own and use across different platforms. In integration, we use the Alternate Key as a "lookup" to find the GUID, allowing us to interact with the system using human-readable or business-relevant identifiers.
Defining and Configuring Alternate Keys
Before you can use an Alternate Key, you must explicitly define it in the Dataverse environment. You can create these through the Power Apps maker portal or via the Web API/SDK.
Steps to Create an Alternate Key in the Portal:
- Navigate to the Table: Open your solution in the Power Apps maker portal and select the table you wish to modify.
- Access Keys: Under the "Keys" tab, click on "New key."
- Define Columns: Choose the column or columns that will make up the key. For example, if you are syncing "Employees," you might select the "EmployeeID" column.
- Enforce Uniqueness: When you save, Dataverse will create a system index. This ensures that no two records in the table can share the same value for that column.
- Wait for Creation: Dataverse will run a background job to build the index. This can take a few minutes depending on the size of your existing data.
Best Practices for Key Selection:
- Stability: Choose columns that rarely change. If an "EmployeeID" is permanent, it is a great candidate. If a "DepartmentName" might be updated, it is a poor candidate for an Alternate Key.
- Cardinality: Ensure the key is truly unique. If you choose a column that might have duplicates, the index creation will fail.
- Data Quality: If you plan to use an email address as an alternate key, ensure you have strict validation rules on the form to prevent bad data from blocking your integrations.
Tip: Composite Keys You are not limited to a single column. You can create a composite key using up to five columns. For example, an "OrderLineItem" might be uniquely identified by the combination of an "OrderID" and a "LineNumber." By selecting both, you create a composite index that ensures uniqueness across the pair.
Integrating with Alternate Keys: Practical Examples
Once the key is defined, you can start using it in your API requests. The most common way to do this is through the OData endpoint.
Using the Web API for Upserts
The Web API allows you to use the PATCH method with the alternate key directly in the URL. This is the most efficient way to perform an upsert.
Example: Upserting a Contact
Imagine you have an Alternate Key defined on the Contact table called new_externalid.
PATCH [Organization URI]/api/data/v9.2/contacts(new_externalid='EXT-12345')
Content-Type: application/json
{
"firstname": "Jane",
"lastname": "Doe",
"emailaddress1": "[email protected]"
}
Explanation:
contacts(new_externalid='EXT-12345'): This syntax tells Dataverse to look for a contact where thenew_externalidfield equalsEXT-12345.- If the record exists, it updates the fields provided in the JSON body.
- If the record does not exist, it creates a new record and assigns the value
EXT-12345to thenew_externalidfield.
Using the C# SDK (OrganizationService)
If you are writing a C# plugin or a custom service, you use the Entity class with the KeyAttributes property.
Entity contact = new Entity("contact");
contact.KeyAttributes.Add("new_externalid", "EXT-12345");
contact["firstname"] = "Jane";
contact["lastname"] = "Doe";
// Upsert operation
UpsertRequest request = new UpsertRequest()
{
Target = contact
};
service.Execute(request);
Why this is superior:
Using the UpsertRequest prevents the "Record Already Exists" error that you would normally encounter if you simply tried to create a record that is already present. It handles the logic internally, making your code cleaner and more resilient to timing issues.
Advanced Scenarios and Technical Considerations
Handling Lookups with Alternate Keys
One of the most powerful aspects of Alternate Keys is the ability to reference related records without knowing their GUIDs. If you are importing an "Order" and you need to link it to a "Customer," you can use the customer's alternate key instead of the GUID.
Example: Setting a Lookup Field
When creating a record, you can use the @odata.bind notation to link a record using its alternate key:
{
"name": "New Order 001",
"[email protected]": "/accounts(accountnumber='ACC-999')"
}
In this case, Dataverse will automatically resolve the accountnumber 'ACC-999' to the internal GUID and associate the records correctly. This eliminates the need to perform a separate "Lookup" query to find the customer's GUID before submitting the order.
Performance Implications
While Alternate Keys are great for integration, they do impose a cost. Every time you insert or update a record, Dataverse must update the unique index associated with the key. If you have many alternate keys on a single table, it can slow down bulk data imports.
Warning: Performance Overload Do not create an alternate key for every possible combination of fields. Over-indexing a table can lead to significant degradation in performance during high-volume operations. Only create keys that are strictly required for your integration workflows.
Common Pitfalls and How to Avoid Them
Even experienced developers can run into issues with Alternate Keys if they do not account for the nuances of the platform.
1. The "Case Sensitivity" Trap
By default, the collation of your Dataverse environment may treat keys as case-insensitive. However, if your external system treats 'ABC' and 'abc' as different keys, you will run into data integrity issues. Always ensure that the data being sent to Dataverse is normalized (e.g., all uppercase) before it reaches the API.
2. Null Values in Keys
Dataverse handles null values in unique indexes in a specific way. If a column in your alternate key is null, the system might not treat the record as a duplicate of another record with a null value in the same column.
- The Problem: If you have an alternate key on
(Email, Phone), and both are null, you might accidentally create multiple records that all have null keys. - The Fix: Use business rules or plugin validation to ensure that the fields forming your alternate key are mandatory.
3. Key Index Latency
When you create a new Alternate Key, the system initiates a background process to create the SQL index. Your integration will not work until this process is 100% complete. Do not attempt to run your integration immediately after publishing the key. Check the "Keys" tab in the solution explorer to ensure the status is "Active."
4. Changing Key Definitions
If you decide to change an alternate key (for example, switching from EmployeeID to Email), you must delete the old key and create a new one. Be aware that this will trigger a full re-indexing of the table, which can lock the table for heavy operations during the process.
Comparison of Integration Identification Methods
| Feature | GUID (Primary Key) | Alternate Key |
|---|---|---|
| Origin | System Generated | User/Business Defined |
| Usage | Internal System Logic | External Integrations |
| Readability | Low (Hexadecimal) | High (Business Data) |
| Uniqueness | Guaranteed by Platform | Enforced via Index |
| Upsert Support | Not natively (requires logic) | Yes (Native) |
| Setup Effort | None (Built-in) | Manual Configuration |
Best Practices Checklist for Integration Developers
To ensure your integrations remain stable as your organization grows, follow these industry-standard practices:
- Document Your Keys: Maintain a clear registry of which alternate keys are used by which integration. This prevents different teams from accidentally deleting or modifying keys that are critical for other systems.
- Use Descriptive Names: When creating keys, use naming conventions that describe their purpose, such as
ak_contact_emailorak_product_sku. - Monitor for Errors: Implement logging in your integration layer that captures 409 (Conflict) errors. If you receive a 409, it means your attempt to create a record failed because of a key violation. This is a clear indicator that your data synchronization is out of sync.
- Validate Data Before Sending: Perform your own data validation in your middle-tier (Azure Functions, Logic Apps, etc.) before sending the request to Dataverse. It is much cheaper to catch a validation error in your own code than to have the Dataverse API return an error.
- Handle Partial Failures: In bulk integration scenarios, one bad key record shouldn't stop the entire batch. Use batch processing or asynchronous patterns to handle errors gracefully without failing the entire payload.
Callout: The "Upsert" Pattern An Upsert pattern is the gold standard for data synchronization. By using
UpsertRequestorPATCHwith an alternate key, you remove the "check-then-act" cycle. This makes your integration "idempotent"—meaning you can run the same integration process multiple times with the same data, and the final state of the database will be the same regardless of how many times it was processed. This is the hallmark of a resilient integration.
Troubleshooting Common Errors
Error: "A record with these values already exists"
This error occurs when you attempt to insert a record, but the Alternate Key values you provided are already present in the table.
- Resolution: Switch your integration logic from a
POST(create) request to aPATCH(update) request using the Alternate Key in the URL.
Error: "The specified key is not active"
This occurs if the index creation process is still running.
- Resolution: Wait for the system job to complete. You can verify this in the Power Apps portal under the Table/Keys section.
Error: "Invalid Key Attribute"
This happens if you are trying to use a field as part of an Alternate Key that does not support indexing (such as certain types of calculated fields or multi-select options).
- Resolution: Verify the field type. Only fields that support indexing (like Text, Integer, or Whole Number) can be used in Alternate Keys.
Conclusion: Building for the Long Term
Alternate Keys are more than just a configuration setting; they are a critical component of a robust data architecture. By mapping your external business identifiers directly to the Dataverse schema, you eliminate the friction that causes data silos and integration failures. As you design your integrations, always prioritize the use of these keys over manual GUID lookups.
By following the steps outlined in this lesson—selecting stable keys, enforcing uniqueness, and adopting the Upsert pattern—you will significantly reduce the complexity of your integration code. You will also improve the performance of your data synchronization by reducing the number of round-trips to the server.
Key Takeaways
- GUIDs are for the system, Alternate Keys are for the business. Use Alternate Keys to allow external systems to talk to Dataverse using their own language (e.g., SKU, Email, ID).
- Upserts are efficient. Using Alternate Keys enables true Upsert functionality, which is the most efficient way to handle data synchronization between systems.
- Composite keys provide flexibility. If a single field isn't unique enough, combine multiple fields to create a unique identifier for your records.
- Index management matters. Be mindful of performance—don't create unnecessary keys, as they add overhead to every write operation on the table.
- Always normalize data. Ensure that the data coming from external systems matches the format expected by your Dataverse keys to avoid case-sensitivity or formatting issues.
- Use the correct API pattern. Whether it is the
PATCHmethod in the Web API or theUpsertRequestin the SDK, make sure your code is leveraging the native capabilities of the platform rather than manual lookup logic. - Monitor and maintain. Treat your Alternate Keys as part of your system's infrastructure. Keep them documented, monitored for errors, and aligned with the evolving needs of your business integrations.
By mastering these concepts, you ensure that your integrations are not just functional, but also scalable and easy to maintain as your data needs evolve. Treat these keys as the "connective tissue" of your integration strategy, and you will find that Dataverse becomes a much more accessible and reliable hub for all your enterprise data.
Continue the course
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons