1

我需要在定价批准产品中创建或更新票号查找字段。此票号(查找字段)链接到另一个名为定价批准票的实体中的票(文本字段)。

这是我在脚本组件中的代码。

public override void Input0_ProcessInputRow(Input0Buffer Row)
{

    Entity ApprovalProductEnt = new Entity("new_ticketproduct");
    ColumnSet columns = new ColumnSet(true);
    columns = new ColumnSet(new String[] { "new_ticket",
        "new_productgroup",
        "new_producttype",
        "new_productitem" });

    Guid TicketNumberId = new Guid();
    TicketNumberId = getTicketNumber(Row.OppportunityID, ref organizationservice);

    //update
    if (TicketNumberId != Guid.Empty)
    {
        ApprovalProductEnt["new_ticket"] = new EntityReference("new_pricingapprovalticket", TicketNumberId);
    }
    else
    //create
    if (TicketNumberId != Guid.Empty)
    {
        ApprovalProductEnt["new_ticket"] = new EntityReference("new_pricingapprovalticket", TicketNumberId);
    }
}

public Guid getTicketNumber(string ticketnumber, ref IOrganizationService service)
{
    Guid TicketNumberGuid = Guid.Empty;
    QueryExpression TicketNumberQuery = new QueryExpression { EntityName = "new_pricingapprovalticket", ColumnSet = new ColumnSet(true) };
    TicketNumberQuery.Criteria.AddCondition("new_ticketnumber", ConditionOperator.Equal, ticketnumber);
    EntityCollection TicketNumberQueryRetrieve = service.RetrieveMultiple(TicketNumberQuery);

    for (var i = 0; i <TicketNumberQueryRetrieve.Entities.Count; i++)
    {
        TicketNumberGuid = TicketNumberQueryRetrieve.Entities[0].GetAttributeValue<Guid>("new_ticket");
    }

    return TicketNumberGuid;
}

当我启动 SSIS 包时,它运行没有错误。但不知何故,没有插入数据。

在此处输入图像描述

创建普通文本字段没有问题。但是当涉及到查找字段时,互联网解决方案建议根据我的代码使用EntityReference,但我不知道出了什么问题。所以我在这里有点迷失了。

4

1 回答 1

0

查找是相关实体的关系列(外键)。您应该检查是否有可用于映射键的主记录或创建该类型的记录以进行映射。

创建实体记录时,第一种方法不需要 ColumnSet。但是您应该映射源文件/表行中的列。ColumnSet 在检索数据集时很有用,就像在第二种方法中一样。

第二种方法不需要 for 循环。

只需将缺失的部分放在您的片段中以便您理解,这可以是即兴创作的。

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Entity ApprovalProductEnt = new Entity("new_ticketproduct");

    ApprovalProductEnt["new_productgroup"] = Row.ProductGroup;
    ApprovalProductEnt["new_producttype"] = Row.ProductType;
    ApprovalProductEnt["new_productitem"] = Row.ProductItem;

    Guid TicketNumberId = new Guid();
    TicketNumberId = getTicketNumber(Row.OppportunityID, ref organizationservice);

    //update
    if (TicketNumberId != Guid.Empty)
    {
        ApprovalProductEnt["new_ticket"] = new EntityReference("new_pricingapprovalticket", TicketNumberId);
    }
    else
    //create
    {
        Entity TicketEnt = new Entity("new_pricingapprovalticket");
        TicketEnt["new_name"] = Row.OppportunityID;
        TicketNumberId = organizationservice.Create(TicketEnt);

        ApprovalProductEnt["new_ticket"] = new EntityReference("new_pricingapprovalticket", TicketNumberId);
    }

   organizationservice.Create(ApprovalProductEnt);
}


public Guid getTicketNumber(string ticketnumber, ref IOrganizationService service)
{
    Guid TicketNumberGuid = Guid.Empty;
    QueryExpression TicketNumberQuery = new QueryExpression { EntityName = "new_pricingapprovalticket", ColumnSet = new ColumnSet(true) };
    TicketNumberQuery.Criteria.AddCondition("new_ticketnumber", ConditionOperator.Equal, ticketnumber);
    EntityCollection TicketNumberQueryRetrieve = service.RetrieveMultiple(TicketNumberQuery);

if (TicketNumberQueryRetrieve.Entities.Count > 0)
    TicketNumberGuid = TicketNumberQueryRetrieve.Entities[0].GetAttributeValue<Guid>("new_ticket");

return TicketNumberGuid;
}
于 2017-09-30T23:23:04.257 回答