Search code examples
c#asp.netasp.net-mvcquickbooksquickbooks-online

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


I have successfully connected my ASP.NET MVC5 C# project to QuickBooks Online through Oauth, and the "IPP .NET SDK for QuickBooks V3" NuGet Package.

I can add new customers, and everything works fine. However, if I try to add a customer that already exists, it throws the following error: "ValidationException was thrown".

My question is, what is the best way to check if the customer already exists in QuickBooks before trying to add them, so as to avoid the exception?

This is the code I have that adds the new customer to 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);

Editted to add my solution with the answer from @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");
}

Solution

  • Query for the customer to see if they exist before adding them.

    What you query for (Name, Email, etc.) is dependent on how you want to implement your application/what your customer wants.

    Per the docs:

    Your code should look something like this:

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

    You can refer to the object reference to see which fields you can filter by: