如何在通过 QuickBooks Online API 添加客户之前检查客户是否存在?

How to check if a customer exists before adding them through the QuickBooks Online API?

我已经通过 Oauth 和 "IPP .NET SDK for QuickBooks V3" NuGet 包成功地将我的 ASP.NET MVC5 C# 项目连接到 QuickBooks Online。

我可以添加新客户,一切正常。但是,如果我尝试添加一个已经存在的客户,它会抛出以下错误:"ValidationException was thrown".

我的问题是,在尝试添加之前检查客户是否已存在于 QuickBooks 中以避免异常的最佳方法是什么?

这是我将新客户添加到 QuickBooks 的代码:

OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
ServiceContext context = new ServiceContext(realmId, intuitServicesType, oauthValidator);
DataService dataService = new DataService(context);

var customer = new Customer();

customer.GivenName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.FirstName.ToLower());
customer.FamilyName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.LastName.ToLower());
customer.BillAddr = new PhysicalAddress()
{
    Line1 = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.Address.ToLower()),
    City = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.City.ToLower()),
    CountrySubDivisionCode = submission.State,
    PostalCode = submission.ZipCode
};

customer.PrimaryEmailAddr = new EmailAddress() { Address = submission.EmailAddress.ToLower(), Default = true };
customer.PrimaryPhone = new TelephoneNumber() { FreeFormNumber = submission.Phone };

dataService.Add(customer);

编辑以添加我的解决方案和@Keith Palmer

的答案
OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
ServiceContext context = new ServiceContext(realmId, intuitServicesType, oauthValidator);

// Check if the customer already exists in QuickBooks
QueryService<Customer> customerQueryService = new QueryService<Customer>(context);
int customerCount = customerQueryService.Where(c => c.GivenName == submission.FirstName && c.FamilyName == submission.LastName).Count();
if (customerCount == 0)
{
    // If not, then add the new customer.
    DataService dataService = new DataService(context);

    var customer = new Customer();

    customer.GivenName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.FirstName.ToLower());
    customer.FamilyName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.LastName.ToLower());
    customer.BillAddr = new PhysicalAddress()
    {
        Line1 = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.Address.ToLower()),
        City = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(submission.City.ToLower()),
        CountrySubDivisionCode = submission.State,
        PostalCode = submission.ZipCode
    };
    customer.PrimaryEmailAddr = new EmailAddress() { Address = submission.EmailAddress.ToLower(), Default = true };
    customer.PrimaryPhone = new TelephoneNumber() { FreeFormNumber = submission.Phone };

    dataService.Add(customer);
}
else
{
    TempData["Warning"] = "The customer already exists in QuickBooks.";
    return RedirectToAction("Estimates", "Admin");
}

在添加之前查询客户以查看它们是否存在。

您查询的内容(姓名、电子邮件等)取决于您希望如何实现您的 application/what 客户的需求。

根据文档:

您的代码应如下所示:

IEnumerable customers = invoiceQueryService.Where(c => c.Balance > 1000);

您可以参考对象参考以查看您可以根据哪些字段进行筛选: