MSCRM 4使用InitializeFromRequest将标准实体转换为自定义实体

时间:2010-06-10 17:07:24

标签: c# dynamics-crm dynamics-crm-4

我正在尝试使用以下代码使用InitializeFromRequest将订单和orderdetail行转换为自定义实体及其子行:

public void Convert(Guid FromEntityId, string FromEntityName, string ToEntityName)
{
    try
    {
        // Set up the CRM Service.
        CrmService _service = GetCrmService();

        InitializeFromRequest req = new InitializeFromRequest();
        req.EntityMoniker = new Moniker(); // this is the very thing that does the job.

        req.EntityMoniker.Id = FromEntityId;
        req.EntityMoniker.Name = FromEntityName;
        req.TargetEntityName = ToEntityName;  //contact for our example req.

        req.TargetFieldType = TargetFieldType.ValidForCreate;
        InitializeFromResponse rps = (InitializeFromResponse)_service.Execute(req);
        //now the lead is converted to a contact, and you can see it in contacts.
        Guid entityId = _service.Create(rps.Entity);
        lblMsg.Text = "Done ID:" + entityId.ToString();
    }            
    catch (System.Web.Services.Protocols.SoapException se)
    {
        lblMsg.Text = "soap:" + se.Detail.InnerText;
    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.Message;
    }
}

现在我能够创建一个自定义实体,但是尽管我在实现中设置了映射字段,但所有属性都是空的。

我错过了什么或做错了什么?

1 个答案:

答案 0 :(得分:0)

将标准实体转换为自定义实体时,需要设置ReturnDynamicEntities = true。