简体   繁体   English

如何使用ssis的脚本组件将票证编号(查找字段)插入MS Dynamics CRM定价批准产品(自定义实体)

[英]How to insert Ticket Number(lookup field) to MS Dynamics CRM Pricing Approval Products(Custom entity) using ssis's script component

I need to create or update Ticket Number lookup field in Pricing Approval Products. 我需要在“定价批准产品”中创建或更新“票证编号”查找字段。 This ticket number(lookup field) is linked to Ticket(text field) in another entity named as Pricing Approval Tickets. 该票证号(查找字段)链接到另一个名为“定价批准票证”的实体中的票证(文本字段)。

This is my code in script component. 这是我脚本组件中的代码。

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;
}

When I start the SSIS package, Its run without error. 当我启动SSIS程序包时,其运行没有错误。 But somehow there is no data inserted. 但是不知何故没有插入​​数据。

在此处输入图片说明

I have no problem to create a normal text field. 创建普通的文本字段没有问题。 But when it come to lookup field, internet solutions suggest to use EntityReference as per my code, but I have NO idea what is wrong. 但是,当涉及到查找字段时,Internet解决方案建议按照我的代码使用EntityReference ,但是我不知道哪里出了问题。 So I am a bit lost here. 所以我在这里有点迷路。

Lookup is a relationship column (Foreign key) of related entity. 查找是相关实体的关系列(外键)。 You should check if there is a master record available to map the key or create the record of that type to map. 您应该检查是否有主记录可用于映射键或创建该类型的记录以进行映射。

ColumnSet is not needed in first method while creating entity record. 创建实体记录时,第一种方法不需要ColumnSet。 But you should map the columns from source file/table Rows. 但是您应该映射源文件/表行中的列。 ColumnSet is useful like in second method while retrieving dataset. 检索数据集时,ColumnSet很有用,就像第二种方法一样。

For loop is unnecessary in 2nd method. 在第二种方法中,不需要循环。

Just put the missing pieces in your snippet for you to understand, this can be improvised. 只需将缺少的片段放在您的代码段中以供您了解,这是即兴的。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ssis脚本组件更新Dynamics CRM中定价批准产品(自定义实体)中的“名称”字段 - How to update Name field in Pricing Approval Products(Custom Entity) in Dynamics CRM using ssis script component 如何将国家/地区数据插入MS Dynamics CRM Lead实体 - How to insert Country data to the MS Dynamics CRM Lead entity 如何使用C#在MS Dynamics CRM中为案例的自定义字段设置/获取值 - how to set/get value to the custom field of a case in MS Dynamics CRM using C# 从实体的数字字段中选择最大数量(Dynamics CRM 2016) - Select Maximum number from a numeric field in an entity (Dynamics CRM 2016) 如何在 MS Dynamics CRM 中获取帐户实体的关联潜在客户记录 - How to fetch the associated Lead Records for an account entity in MS Dynamics CRM 实体框架和 MS Dynamics CRM 插件 - Entity Framework and MS Dynamics CRM Plugin MS Dynamics 365 CRM 在线 - 转储实体 - MS Dynamics 365 CRM online - dump entity 如何通过API在CRM Dynamics中创建自定义实体本身 - How to create custom entity itself in CRM Dynamics via API 使用c#和asp.net在ms crm4中检索单个实体的nvarchar字段 - Retrieve a single entity's nvarchar field in ms crm4 using c# and asp.net 如何使用 C# 在 CRM 中的新实体中设置查找字段的值 - How to set value of lookup field in a new entity in CRM using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM