.NET C#调用发票API时发票行跟踪类别OptionId为空GUID问题
Troubleshooting Xero .NET API Invoice Issues
Hey there, let's work through the two issues you're hitting with the Xero .NET API—empty GUIDs for tracking category OptionIds and only getting one result when using UpdatedDateUTC filtering.
Issue 1: Tracking Category OptionId is Empty GUID
First, let's dig into why the OptionId on your invoice lines is coming through as an empty GUID:
- Verify Xero Backend Data: Start by checking the invoice directly in the Xero web interface. If the invoice line doesn't have a tracking category option selected, the API will return an empty GUID here—that's expected behavior. If you do see a selected option in Xero, move to the next steps.
- Inspect Raw API Response: Use a tool like Fiddler or Visual Studio's built-in network debugging to capture the raw JSON response from Xero. Check if the
OptionIdfield in the response is actually empty, or if the .NET library is deserializing it incorrectly. For example, if the API returns an empty string instead of a GUID, the model might default to an empty GUID. - Check Library Version: Outdated versions of the Xero .NET SDK can have deserialization bugs. Make sure you're using the latest stable version of the
Xero.Api.Corepackage—upgrade via NuGet and test again. - Validate Business Logic: If your workflow requires non-empty
OptionIds, ensure that invoices are being created/updated with valid tracking category options. You can also add a filter to your query to exclude invoice lines with emptyOptionIds if needed.
Issue 2: Only One Result Returned After Setting UpdatedDateUTC
Looking at your code snippet, there are a few potential culprits here:
var query = api.Invoices.Page(page); page++; if (lastUpdatedTime.HasValue) { query = query.ModifiedSince(lastUpdatedTime.Value.AsUtc()); } invoices = query.OrderBy("UpdatedDateUTC").Find();
- Pagination Logic Flaw: Your code increments
pagebefore callingFind(), which means you're requesting pageNbut then immediately moving toN+1before fetching the data. Ifpagestarts at 1, you'll first request page 1, increment to 2, then fetch page 1's data—but if you only callFind()once, you'll only get that single page's results. To get all matching invoices, you need to loop through pages until no more data is returned:var allInvoices = new List<Invoice>(); int currentPage = 1; while (true) { var query = api.Invoices.Page(currentPage); if (lastUpdatedTime.HasValue) { query = query.ModifiedSince(lastUpdatedTime.Value.AsUtc()); } var pageResults = query.OrderBy("UpdatedDateUTC").Find(); if (!pageResults.Any()) break; allInvoices.AddRange(pageResults); currentPage++; } invoices = allInvoices; - Check ModifiedSince Time Format: Ensure
lastUpdatedTime.Value.AsUtc()generates a valid ISO 8601 UTC timestamp (e.g.,2024-05-20T14:30:00Z). If the timestamp format is incorrect, Xero's API might return unexpected results. Debug the value of this parameter to confirm it's formatted correctly. - Validate OrderBy Field: Double-check that
UpdatedDateUTCis the correct sort field name for the Xero API. Sometimes API field names are case-sensitive—tryUpdatedDateUtc(lowercase 'c') if the original doesn't work. - Check Total Matching Records: Use the
Count()method to see how many invoices actually match yourModifiedSincefilter:
Ifint totalCount = api.Invoices.ModifiedSince(lastUpdatedTime.Value.AsUtc()).Count();totalCountis 1, then the issue is with your data, not the code. If it's higher, the pagination fix above should resolve the problem.
General Debugging Tips
- Use Visual Studio's debugger to inspect the
queryobject before callingFind()—check that all parameters (page number, ModifiedSince timestamp, sort order) are set correctly. - Test the same query directly in Xero's API explorer (via their developer portal) to see if the response matches what your .NET code is receiving. This helps isolate whether the issue is with the SDK or the API itself.
内容的提问来源于stack exchange,提问作者Rob
相关产品推荐
相关产品推荐

