使用插件创建的实体没有有效的 Entity.Id 值
Created Entity using plugin has no valid Entity.Id Value
我成功地执行了创建实体的语句,没有任何错误,但是实体的 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);
}
}
创建案例(行):
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.
}
帮手class:
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;
}
}
您不应使用同一个对象来检查创建的记录或 guid 是否可用。
在你的代码下面一行 return 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中验证。
我成功地执行了创建实体的语句,没有任何错误,但是实体的 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);
}
}
创建案例(行):
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.
}
帮手class:
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;
}
}
您不应使用同一个对象来检查创建的记录或 guid 是否可用。 在你的代码下面一行 return 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中验证。