简体   繁体   English

Dynamics CRM插件-对于输入的发布值,请搜索订阅,如果找到,则将其添加到创建/更新的联系人中

[英]Dynamics CRM Plugin-For a input publication value, search for subscription, if found add it to the contact on create/update

The below code seems to work when a contact is created, for input value publication it searches and associates an existing subscription. 创建联系人时,以下代码似乎有效,对于输入值发布,它搜索并关联现有订阅。 But the same code wont work for update of a contact. 但是,相同的代码无法用于更新联系人。 Any opinions as to why this may be happening in my plugin? 关于为什么这可能在我的插件中发生任何意见?

if (!String.IsNullOrEmpty(publication))
{
    //Query for existing Publication
    var publicationQuery = new QueryByAttribute("msdynhcp_publicationnewsletter");
    //publicationQuery.ColumnSet.AllColumns = true;
    publicationQuery.Attributes.AddRange("msdynhcp_publicationname");
    publicationQuery.Values.AddRange(publication);
    publicationQuery.TopCount = 1;

    //Call Query
    EntityCollection publicationIds = organizationService.RetrieveMultiple(publicationQuery);

    if (publicationIds.Entities.Count > 0)
    {
        //Query for existing subscription
        var subscriptionQuery = new QueryExpression("msdynhcp_subscription");

        var linkedContact = new LinkEntity("msdynhcp_subscription", "contact", "msdynhcp_contactlookup", "contact" + "id", JoinOperator.Inner);
        linkedContact.LinkCriteria = new FilterExpression(LogicalOperator.And);
        linkedContact.LinkCriteria.AddCondition("contact" + "id", ConditionOperator.Equal, ContactId);
        subscriptionQuery.LinkEntities.Add(linkedContact);

        var linkedPublication = new LinkEntity("msdynhcp_subscription", "msdynhcp_publicationnewsletter", "msdynhcp_publicationlookup", "msdynhcp_publicationnewsletter" + "id", JoinOperator.Inner);
        linkedPublication.LinkCriteria = new FilterExpression(LogicalOperator.And);
        linkedPublication.LinkCriteria.AddCondition("msdynhcp_publicationnewsletter" + "id", ConditionOperator.Equal, publicationIds[0].Id);
        subscriptionQuery.LinkEntities.Add(linkedContact);

        subscriptionQuery.TopCount = 1;

        //Call Query
        EntityCollection subscriptionIDs = organizationService.RetrieveMultiple(subscriptionQuery);

        if (subscriptionIDs.Entities.Count == 0)
        {
            //New Subscription
            var crmSubscription = new Entity("msdynhcp_subscription");
            crmSubscription["msdynhcp_utcdatetimestamp"] = DateTime.UtcNow;
            crmSubscription["msdynhcp_enabledactive"] = true;

            //Call Create
            var id = organizationService.Create(crmSubscription);

            //Call Associate
            organizationService.Associate("msdynhcp_subscription", id, new Relationship("msdynhcp_contact_new_subscription_contactlookup"),
                new EntityReferenceCollection(new List<EntityReference>() { new EntityReference("contact", new Guid(ContactId)) }));
            //Call Associate
            organizationService.Associate("msdynhcp_subscription", id, new Relationship("msdynhcp_new_publicationnewsletter_new_subscription_publicationlookup"),
                new EntityReferenceCollection(new List<EntityReference>() { new EntityReference("msdynhcp_publicationnewsletter", publicationIds.Entities[0].Id) }));
        }
    }
}

In an effort to "teach a man to fish" rather then telling you what's wrong, why don't you just debug it yourself? 为了“教一个人去钓鱼”而不是告诉您出了什么问题,您为什么不自己调试呢? You can use the Plugin Registration tool to create a serialized version of the plugin context, and then debug that yourself . 您可以使用“插件注册”工具来创建插件上下文的序列化版本,然后自己进行调试

You did not post the whole plugin logic but if it works on create and does not work on update then most likely, you are expecting in your plugin an attribute that is simply not there. 您没有发布整个插件逻辑,但是如果它在create上起作用而在update上不起作用,那么很可能是在您的插件中期望一个根本不存在的属性。 When you create an entity, you have to fill all the required fields (at least if you do that on the web form) and all this attributes are there in your Target in InputParameters of the plugin. 创建实体时,必须填写所有必填字段(至少是在Web表单上填写的字段),并且所有这些属性都在插件的InputParameters中的Target中。 But when you are updating entity, only the attributes that are modified are in the Target from InputParameters (and of course some standard attributes like modifiedon, modifiedby etc.). 但是,当您更新实体时,只有被修改的属性才在InputParameters中的Target中(当然还有一些标准属性,例如Modifyon,modifyby等)。 So, most likely, your plugin checks if some attribute is null (this first check for publication looks suspicious, but I can't tell if that's it, as it's not the full code. To be sure that the attribute is there, get it from PostImage/PreImage not Target (see: https://msdn.microsoft.com/en-us/library/gg309673.aspx#Anchor_5 ) 因此,很可能您的插件会检查某个属性是否为null(首次检查publication可疑,但我无法确定是不是,因为它不是完整的代码。要确保该属性在那里,请获取它来自PostImage / PreImage而不是目标(请参阅: https ://msdn.microsoft.com/zh-cn/library/gg309673.aspx#Anchor_5)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM