0

I am trying to change all the Business Unit references I got after importing a solution to the ones in the Acceptance environment.

QueryExpression ViewQuery = new QueryExpression("savedquery");
String[] viewArrayFields = { "name", "fetchxml" };

ViewQuery.ColumnSet = new ColumnSet(viewArrayFields);


ViewQuery.PageInfo = new PagingInfo();
ViewQuery.PageInfo.Count = 5000;
ViewQuery.PageInfo.PageNumber = 1;
ViewQuery.PageInfo.ReturnTotalRecordCount = true;

EntityCollection retrievedViews = service.RetrieveMultiple(ViewQuery);
//iterate though the values and print the right one for the current user
int oldValues = 0;
int accValuesUpdated = 0;
int prodValuesUpdated = 0;
int total = 0;
foreach (var entity in retrievedViews.Entities)
{
    total++;
    if (!entity.Contains("fetchxml"))
    { }
    else
    {
        string fetchXML = entity.Attributes["fetchxml"].ToString();

        for (int i = 0; i < guidDictionnary.Count; i++)
        {
            var entry = guidDictionnary.ElementAt(i);

            if (fetchXML.Contains(entry.Key.ToString().ToUpperInvariant()))
            {
                Console.WriteLine(entity.Attributes["name"].ToString());
                oldValues++;
                if (destinationEnv.Equals("acc"))
                {
                    accValuesUpdated++;
                    Console.WriteLine();
                    Console.WriteLine("BEFORE:");
                    Console.WriteLine();
                    Console.WriteLine(entity.Attributes["fetchxml"].ToString());
                    string query = entity.Attributes["fetchxml"].ToString();
                    query = query.Replace(entry.Key.ToString().ToUpperInvariant(), entry.Value.AccGuid.ToString().ToUpperInvariant());
                    entity.Attributes["fetchxml"] = query;
                    Console.WriteLine();
                    Console.WriteLine("AFTER:");
                    Console.WriteLine();
                    Console.WriteLine(entity.Attributes["fetchxml"].ToString());
                }
                else
                {
                    prodValuesUpdated++;
                    string query = entity.Attributes["fetchxml"].ToString();
                    query = query.Replace(entry.Key.ToString().ToUpperInvariant(), entry.Value.ProdGuid.ToString().ToUpperInvariant());
                    entity.Attributes["fetchxml"] = query;
                }
                service.Update(entity);
            }

        }
    }

}
Console.WriteLine("{0} values to be updated. {1} shall be mapped to acceptance, {2} to prod. Total = {3} : {4}", oldValues, accValuesUpdated, prodValuesUpdated, total, retrievedViews.Entities.Count);

I see that the new value is corrected, but it does not get saved. I get no error while updating the record and publishing the changes in CRM does not help.

Any hint?

4

1 回答 1

2

根据您的评论,听起来您将实体保存为的值就是您希望它成为的值。我猜您的问题是不发布您的更改。如果您不发布它,它仍然会给您我相信的 FetchXml 的旧值。

尝试调用此方法:

    PublishEntity(service, "savedquery");

    private void PublishEntity(IOrganizationService service, string logicalName)
    {
        service.Execute(new PublishXmlRequest()
        {
            ParameterXml = "<importexportxml>"
          + "    <entities>"
          + "        <entity>" + logicalName + "</entity>"
          + "    </entities>"
          + "</importexportxml>"
        });
    }
于 2013-10-17T12:40:23.407 回答