使用插件创建的实体没有有效的Entity.Id值

问题描述 投票:0回答:1

我已经成功执行了语句以成功创建一个实体,没有任何错误,但是该实体的id保留为默认值00000000-0000-0000-0000-000000000000。在CRM中,创建的实体也没有出现。

我没有在实体中包括所有字段,仅输入了1个字段进行测试。

是否由于某些必填字段未填写而导致创建实体的上述误报?

下面是我的代码段:

调用CreateCase(行)

foreach (DataRow row in caseTable.Rows)
{
    Entity caseEntity = helper.GetCasebyID((string)row[Excel.ID]);

    if (caseEntity == null)
    {
        caseEntity = CreateCase(row);
    }
}

CreateCase(row):

private Entity CreateCase(DataRow row)
{
    Entity caseEntity = new Entity("new_case");

    if (!(string.IsNullOrEmpty(row[Excel.sourcename].ToString().Trim())))
    {
        caseEntity.Attributes[Case.sourcename] = row[Excel.sourcename];
    }
    helper.GetOrganizationService().Create(caseEntity);
    Logger.Info(caseEntity.LogicalName + " " + caseEntity.Id + " created. ");
    //output: case 00000000-0000-0000-0000-000000000000 created. 
}

助手类:

public class Helper
{
    private OrganizationServiceProxy _proxy;
    private IOrganizationService _org;

    public IOrganizationService GetOrganizationService()
    {
        if (_org == null)
        {
            var credential = new ClientCredentials();
            credential.Windows.ClientCredential.Domain = ConfigurationManager.AppSettings["Domain"];
            credential.Windows.ClientCredential.UserName = ConfigurationManager.AppSettings["Username"];
            credential.Windows.ClientCredential.Password = Decrypt(ConfigurationManager.AppSettings["Password"]);
            Uri organizationUri = new Uri(ConfigurationManager.AppSettings["OrganizationUri"]);
            Uri homeRealmUri = null;
            _proxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credential, null);
            _proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            _org = new OrganizationService(_proxy);
        }
        return _org;
    }
}
entity crm microsoft-dynamics
1个回答
0
投票

您不应使用同一对象来检查创建的记录或guid是否可用。代码下面的行返回Guid数据类型,而不是对象的子集。

helper.GetOrganizationService().Create(caseEntity);

此外,当您创建记录时,请尝试添加至少主名属性值。您的情况应为new_name

所以您应该将代码设为

Guid newlyCreatedCaseRecord = helper.GetOrganizationService().Create(caseEntity);
 Logger.Info(caseEntity.LogicalName + " " + newlyCreatedCaseRecord  + " created. ");

您还可以从数据库中获取新创建的记录作为实体对象。

    Guid newlyCreatedCaseRecord = helper.GetOrganizationService().Create(caseEntity);
        Entity newlyCaseEntity = helper.GetOrganizationService().Retrieve("new_case",newlyCreatedCaseRecord , new ColumnSet(true));
Logger.Info(newlyCaseEntity .LogicalName + " " + newlyCaseEntity.Id + " created. ");

注意:代码中可能有错别字,请尝试在Visual Studio中进行验证。

© www.soinside.com 2019 - 2024. All rights reserved.