我需要使用 ssis 更新定价批准产品中的名称字段。这是我在脚本组件中的代码。
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Guid AppProductLookup = new Guid();
AppProductLookup = getAppProductId(Row.Name, ref organizationservice);
Entity AppProductEnt = new Entity("new_ticketproduct");
ColumnSet columns = new ColumnSet(true);
columns = new ColumnSet(new String[] { "new_name"});
AppProductEnt = organizationservice.Retrieve(AppProductEnt.LogicalName, AppProductLookup, columns);
AppProductEnt["new_name"] = Row.Name;
organizationservice.Update(AppProductEnt);
}
public Guid getAppProductId(string name, ref IOrganizationService service)
{
Guid AppProductId = Guid.Empty;
QueryExpression AppProduct = new QueryExpression { EntityName = "new_ticketproduct", ColumnSet = new ColumnSet(true) };
AppProduct.Criteria.AddCondition("new_name", ConditionOperator.Equal, name);
EntityCollection AppProductRetrieve = service.RetrieveMultiple(AppProduct);
if (AppProductRetrieve != null && AppProductRetrieve.Entities.Count > 0)
{
AppProductId = AppProductRetrieve.Entities[0].GetAttributeValue<Guid>("new_ticketproductid");
}
return AppProductId;
}
但我遇到了一个例外:实体引用不能有 id 和 key 属性为空。但是我在 crm 中的姓名字段是文本字段。我也使用检查`
if (AppProductLookup != Guid.Empty)
{
AppProductEnt = organizationservice.Retrieve(AppProductEnt.LogicalName, AppProductLookup, columns);
AppProductEnt["new_name"] = Row.Name;
organizationservice.Update(AppProductEnt);
}
else
{
//AppProductEnt["new_name"] = Row.Name;
//organizationservice.Create(AppProductEnt);
}
我的断点跳过这段代码if (AppProductLookup != Guid.Empty)
。因此我没有错误,但这不是我想要的。我不知道出了什么问题。所以我在这里有点迷失了。