ExecuteMultipleResponse;如何从休息中读取和存储Guids

时间:2015-07-21 10:01:33

标签: c# list dynamics-crm-2011 dynamics-crm crm

我正在使用ExecuteMultipleResponse方法使用SSIS一次插入10个帐户记录。

    List<Entity> _Accounts = new List<Entity>();
//  Check the batch size and process
public override void InputAccount_ProcessInput(InputAccountBuffer Buffer)
{
    //List<int> personIDs = new List<int>();

    int index = 0;


    while (Buffer.NextRow())
    {
        _Accounts.Add(InputAccountFromBuffer(Buffer));
        //personIDs.Add(int.Parse(Buffer.sPersonID));
        index++;

        if (index == 10)
        {
            ImportBatch();
            index = 0;
        }
    }
    ImportBatch();
}

private void ImportBatch()
{
    if (_Accounts.Count > 0)
    {
        var multipleRequest = new ExecuteMultipleRequest()
        {
            Settings = new ExecuteMultipleSettings()
            {
                ContinueOnError = true,
                ReturnResponses = true
            },
            Requests = new OrganizationRequestCollection()
        };

        foreach (var profContact in _Accounts)
        {
            CreateRequest reqCreate = new CreateRequest();
            reqCreate.Target = profContact;
            reqCreate.Parameters.Add("SuppressDuplicateDetection", false);
            multipleRequest.Requests.Add(reqCreate);
        }

        ExecuteMultipleResponse multipleResponses = (ExecuteMultipleResponse)organizationservice.Execute(multipleRequest);

        var responses = (ExecuteMultipleResponseItemCollection)multipleResponses.Results["Responses"];

        foreach (var response in responses)
            {
                if (response.Fault != null)
                    {
                    // A fault has occurred, handle it here
                    }
                    else
                    {
                       // THIS IS WHERE I KNOW THE GUID VALUE EXIST.
                    }
            }


        //IEnumerator f = multipleResponses.Responses.GetEnumerator();


        _Accounts.Clear();
    }

}

上面的代码工作正常,但是,我现在需要从响应List中读取和存储Guids。此信息对于包中的下一步至关重要。我知道,如果我创建单曲,我可以简单地说,

Guid newRecord = _service.Create(account);

我甚至设法开始检查响应是否有“故障”,如果没有故障,那么响应中应该存在Guid值。

在QuickWatch中运行response.Response.Results.Values向我展示了guid,但我找不到直接读取它并将其存储为Guid的方法。 enter image description here

1 个答案:

答案 0 :(得分:4)

创建记录的guid应存储在OrganizationResponse中,ExecuteMultipleResponseItem可以在string id = response.Response.Results["id"].ToString() 内找到 尝试以下方法将guid作为字符串:

Guid guid = new Guid(id);

如果它按预期工作,你还应该能够实例化一个guid,如果需要的话:

In the below SQl you're doing some row number functions and then ordering by the highest row number. After that you do another row number function by the highest row_number it gets in the cte.

With myCte
as
(
Select Customer,Code,Row_Number() OVER (PARTITION BY Customer Order By Code) rn
from myTable

)
select Customer,Code,
Case when rn2 = 1 then 0
else 1
end as myArbitraryNumber
 from
(
select Customer, Code, rn, Row_Number() OVER (Partition by Customer,rn Order by rn DESC) rn2 
from myCte
)