Search code examples
quickbooksupdatingquickbooks-onlineinventory

QuickBooks update item qty doesn't work


I'm trying to update Item.QtyOnHand throught the IPP .NET SDK for QuickBooks v3.0. I had selected the

I track quantity on hand for this product

checkbox, for products that i want to be updated. But i always get an error:

Detail=Business Validation Error: An inventory cost-of-goods-sold account is required if you are tracking inventory quantities for this product.

How to update qty on hand? My code:

    var oauthValidator = new OAuthRequestValidator(RestProfile.OAuthAccessToken, RestProfile.OAuthAccessTokenSecret, this.ConsumerProfile.ConsumerKey, this.ConsumerProfile.ConsumerSecret);
    var serviceContext = new ServiceContext(this.RestProfile.AppToken, this.RestProfile.CompanyId, IntuitServicesType.QBO, oauthValidator);
    var dataService = new DataService( serviceContext );
    var queryService = new QueryService<Item>( serviceContext );
    var items = queryService.Where(x=>x.Type == ItemTypeEnum.Inventory).ToList();
    var batch = dataService.CreateNewBatch();

    foreach( var item in items )
    {
        batch.Add(new Item()
        {
            Name = item.Name,
            Id = item.Id,
            QtyOnHand = item.QtyOnHand+1,
            QtyOnHandSpecified = true
        }, item.Id, OperationEnum.update);
    }

    batch.Execute();

i always get this error:

{
    "BatchItemResponse" : [{
            "Fault" : {
                "Error" : [{
                        "Message" : "A business validation error has occurred while processing your request",
                        "Detail" : "Business Validation Error: An inventory cost-of-goods-sold account is required if you are tracking inventory quantities for this product.",
                        "code" : "6000",
                        "element" : ""
                    }
                ],
                "type" : "ValidationFault"
            },
            "bId" : "20"
        }, {
            "Fault" : {
                "Error" : [{
                        "Message" : "A business validation error has occurred while processing your request",
                        "Detail" : "Business Validation Error: An inventory cost-of-goods-sold account is required if you are tracking inventory quantities for this product.",
                        "code" : "6000",
                        "element" : ""
                    }
                ],
                "type" : "ValidationFault"
            },
            "bId" : "23"
        }, {
            "Fault" : {
                "Error" : [{
                        "Message" : "A business validation error has occurred while processing your request",
                        "Detail" : "Business Validation Error: An inventory cost-of-goods-sold account is required if you are tracking inventory quantities for this product.",
                        "code" : "6000",
                        "element" : ""
                    }
                ],
                "type" : "ValidationFault"
            },
            "bId" : "21"
        }, {
            "Fault" : {
                "Error" : [{
                        "Message" : "A business validation error has occurred while processing your request",
                        "Detail" : "Business Validation Error: An inventory cost-of-goods-sold account is required if you are tracking inventory quantities for this product.",
                        "code" : "6000",
                        "element" : ""
                    }
                ],
                "type" : "ValidationFault"
            },
            "bId" : "22"
        }
    ],
    "time" : "2014-10-09T12:42:48.715-07:00"
}

Solution

  • The issue was in bad request. To avoid these kind of errors i added AccountRef to request. Example:

    var accReference = accounts["Cost of Goods Sold"];
                var expenseAccountRef = new ReferenceType { type = accReference.AccountType.ToString(), name = accReference.Name, Value = accReference.Id };
                foreach( var item in items )
                {
                    batch.Add( new Item()
                    {
                        Name = item.Name,
                        Id = item.Id,
                        SyncToken = item.SyncToken,
                        QtyOnHand = item.QtyOnHand + 1,
                        QtyOnHandSpecified = true,
                        ExpenseAccountRef = expenseAccountRef,
    
                    }, item.Id, OperationEnum.update );
                }