Search code examples
c#quickbooksqbfc

Create new items in QuickBooks via C#


I have a C# tool that I wrote to sync orders from a DB to QB, and every step has been a giant pain.

I'm trying to create the line items that we have in our DB in QB so I can attach the invoice items correctly, but our system allows sales staff to enter custom words, so I can't just add all the possible line items to QB through the QB interface.

I have this piece of code here

IMsgSetRequest AddItemRequestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0);
AddItemRequestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

IItemServiceAdd itemAddRq = AddItemRequestMsgSet.AppendItemServiceAddRq();
itemAddRq.Name.SetValue(Item);
// itemAddRq.ORSalesPurchase.

IMsgSetResponse ItemAddResponseMsgSet = sessionManager.DoRequests(AddItemRequestMsgSet);
IResponse ItemAddResponse = ItemAddResponseMsgSet.ResponseList.GetAt(0);

But this is throwing an error that:

ORSalesPurchase: required field is missing

I haven't the slightest idea of what that means, and for the life of me I can't find anything about ORSalesPurchase or IItemServiceAdd elsewhere on the Internet, including their docs.

I can tell with Visual Studio's autocomplete that itemAddRq.ORSalesPurchase has a bunch of options, but I haven't been able to infer from that what QB is asking for.

QB is the worst

What are the required fields for adding a service item to QB?

And if someone can point me to where this is in their docs that would be extra wonderful:)


Stack trace:

System.Runtime.InteropServices.COMException (0x80040307): ItemServiceAdd ORSalesPurchase: required field is missing End of ItemServiceAdd

at QBFC12Lib.IQBSessionManager.DoRequests(IMsgSetRequest request)
at SterlingQBExport.Form1.CheckNewInvoices(Object source, ElapsedEventArgs e) in C:\Users\brian\Documents\Visual Studio 2015\Projects\SterlingQBExport\SterlingQBExport\Form1.cs:line 537


Solution

  • Based on an article I found while searching for ORSalesPurchase ...

    C# .NET Application with QuickBooks Desktop Edition

    This article is an introduction on how to integrate a .NET application with QuickBooks Desktop Edition using the QuickBooks Foundation Classes (QBFC) Library and C#.

    ... and the error message, it would seem that you are missing certain fields expected to be part of the requests ORSalesPurchase

    Using a sample from the article I would suspect it looks something like the following

    //Create the message set request object to hold our request
    IMsgSetRequest addItemRequestMsgSet = sessionManager.CreateMsgSetRequest("US", 8, 0);
    addItemRequestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
    
    IItemServiceAdd itemServiceAddRq = addItemRequestMsgSet.AppendItemServiceAddRq();
    itemServiceAddRq.Name.SetValue(Item.Name);
    itemServiceAddRq.ORSalesPurchase.SalesOrPurchase.Desc.SetValue(Item.Description);
    itemServiceAddRq.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.SetValue(Item.Price);
    itemServiceAddRq.ORSalesPurchase.SalesOrPurchase.AccountRef.FullName.SetValue("Some custom service description here");
    
    //...
    

    Now there was more which seems to infer that the fields set differ based on the items already having a quickbooks id where it was set using

    itemServiceModRq.ListID.SetValue(Item.QuickBooksID);
    

    Now, I am not entirely certain what the required fields are at this point, but this should be a good starting point to check if providing the fields above helps.