QBO SDK C# - 异步批处理创建多个客户

时间:2013-06-21 07:14:17

标签: c# quickbooks intuit-partner-platform

我想插入多个客户对象我按照此链接https://developer.intuit.com/docs/0025_quickbooksapi/0055_devkits/0100_ipp_.net_devkit/0300_asynchronous_calls/2_batch_process但是收到以下错误 - "您一次只能添加或编辑一个名称。请再试一次。"


发布我的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Web;
using Intuit.Ipp.Core;
using Intuit.Ipp.Security;
using Intuit.Ipp.Services;
using HelloIntuitAnywhere.Utilities;
using Intuit.Ipp.Data.Qbo;
using Intuit.Ipp.Data.Extensions;
using System.Linq;
using System.IO;
using Intuit.Ipp.Data;

namespace HelloIntuitAnywhere
{
    public partial class BatchRequest : System.Web.UI.Page
    {
        public delegate IntuitBatchResponse DelegateBatchCompleted(IntuitBatchRequest batchRequest);
        protected void Page_Load(object sender, EventArgs e)
        {

            if (HttpContext.Current.Session.Keys.Count > 0)
            {
                String realmId = HttpContext.Current.Session["realm"].ToString();
                String accessToken = HttpContext.Current.Session["accessToken"].ToString();
                String accessTokenSecret = HttpContext.Current.Session["accessTokenSecret"].ToString();
                String consumerKey = ConfigurationManager.AppSettings["consumerKey"].ToString(CultureInfo.InvariantCulture);
                String consumerSecret = ConfigurationManager.AppSettings["consumerSecret"].ToString(CultureInfo.InvariantCulture);
                IntuitServicesType intuitServiceType = (IntuitServicesType)HttpContext.Current.Session["intuitServiceType"];

                OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret);
                ServiceContext context = new ServiceContext(oauthValidator, realmId, intuitServiceType);
                DataServices commonService = new DataServices(context);

                try
                {
                    switch (intuitServiceType)
                    {
                        case IntuitServicesType.QBO:

                            IntuitBatchRequest batchRequest = new IntuitBatchRequest();
                            batchRequest.BatchItem = new BatchItemRequest[10];

                            for (int i = 0; i < 10; i++)
                            {
                                Item itm1 = CreateItem("it8" + i.ToString());
                                string guid = Guid.NewGuid().ToString("N");
                                guid = guid.Substring(0, 30);
                                guid = i.ToString();
                                BatchItemRequest itm1BatchItem = new BatchItemRequest();
                                itm1BatchItem.bId = guid;
                                itm1BatchItem.Item = itm1;
                                itm1BatchItem.operation = OperationEnum.create;
                                itm1BatchItem.operationSpecified = true;
                                batchRequest.BatchItem[i] = itm1BatchItem;
                            }



                            //IntuitBatchResponse batchResponse = commonService.ExecuteBatch<IntuitBatchRequest>(batchRequest);
                            try
                            {
                                DelegateBatchCompleted delegateBatch = new DelegateBatchCompleted(commonService.ExecuteBatch<IntuitBatchRequest>);
                                IAsyncResult result = delegateBatch.BeginInvoke(batchRequest, new AsyncCallback(CallbackMethod), delegateBatch);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }


                            break;

                        default:
                            throw new Exception("Data Source not defined.");
                    }


                }
                catch
                {
                    throw;
                }
            }
        }

        private Intuit.Ipp.Data.Qbo.Item CreateItem(string str)
        {
            Intuit.Ipp.Data.Qbo.Item item = new Intuit.Ipp.Data.Qbo.Item();

            item.Desc = str + "Test Desc";
            item.Name = str + "Test Name";
            item.Taxable = true;
            item.TaxableSpecified = true;
            item.ExternalKey = new Intuit.Ipp.Data.Qbo.IdType() { idDomain = Intuit.Ipp.Data.Qbo.idDomainEnum.QBO, Value = str + "123" };
            item.UnitPrice = new Intuit.Ipp.Data.Qbo.Money() { Amount = 6, AmountSpecified = true, CurrencyCode = Intuit.Ipp.Data.Qbo.currencyCode.AED, CurrencyCodeSpecified = false };

            item.IncomeAccountRef = new Intuit.Ipp.Data.Qbo.AccountRef() { AccountId = new Intuit.Ipp.Data.Qbo.IdType() { idDomain = Intuit.Ipp.Data.Qbo.idDomainEnum.QBO, Value = "1" }, AccountType = Intuit.Ipp.Data.Qbo.AccountTypeEnum.Asset, AccountTypeSpecified = true };

            item.Type = Intuit.Ipp.Data.Qbo.ItemTypeEnum.Assembly;

            return item;
        }

        static void CallbackMethod(IAsyncResult result)
        {
            DelegateBatchCompleted delegateBatch = (DelegateBatchCompleted)result.AsyncState;
            IntuitBatchResponse batchResponse = delegateBatch.EndInvoke(result);
        }


    }
}

1 个答案:

答案 0 :(得分:1)

错误消息非常明确:Quickbooks API一次只允许一个客户进行编辑。

他们可能会锁定Customer表,因为业务层或基础数据模型不支持并发编辑。

鉴于这种限制,我无法找到解决问题的方法。

它们允许您异步调用同步方法,因此在方法完成之前不必阻塞,但这并不意味着您可以同时进行两次客户编辑。但是,您可以在编辑完成时执行其他工作。