插件/Javascript - 更新所有具有相同电子邮件地址的联系人的自定义字段 agr1 和 agr2

Posted

技术标签:

【中文标题】插件/Javascript - 更新所有具有相同电子邮件地址的联系人的自定义字段 agr1 和 agr2【英文标题】:Plugin/Javascript - Update custom fields agr1 and agr2 on all Contacts with same email addres 【发布时间】:2021-12-07 22:03:08 【问题描述】:

我需要使插件或 javascript 像这样工作: 1.当 agr1(bool) 更改 (truefalse) 时,像“John Smith”这样的联系人,emailaddress1 = “john1@example.com”,然后 plugin/js 更改所有联系人的 agr1 字段,所有联系人的 emailaddress1 = “john1@example. com"(重复邮件)。

    我在表单上获得了 2 个协议 (bool(yes/no)):例如,agr1 和 agr2,我创建插件,当我将 agr1 从 true 更改为 false 时更改 agr2=false,当我将 agr2 从更改时将 agr1 更改为 true false 到 true - 我想在创建新联系人时在表单上执行此操作 - 我该如何执行此操作?

我的 example2 代码(它在我创建时不是在现有联系人上工作):

namespace IfZgodaChangeMassmailingChange

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Crm.Sdk;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Query;
    using System.Linq.Expressions;
    using System.Activities;
    using System.Runtime.Serialization;
    using System.Collections.ObjectModel;
    using System.Collections;
    using System.Reflection;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Messages;


    public class IfZgodaChangeMassmailingChange : IfZgodaChangeSetZgoda2.Plugin
    
        public IfZgodaChangeMassmailingChange()
            : base(typeof(IfZgodaChangeMassmailingChange))
        
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "contact", new Action<LocalPluginContext>(ExecutePostKontaktUpdate)));
        


        protected void ExecutePostKontaktUpdate(LocalPluginContext localContext)
        

            if (localContext == null)
            
                throw new ArgumentNullException("localContext");
            

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            ITracingService tracingService = localContext.TracingService;
            OrganizationServiceContext _crmOrgContext = new OrganizationServiceContext(service);
            tracingService.Trace("ExecutePostFakturaUpdate Plugin: Verifying the client is not offline.");

            if (context.IsExecutingOffline || context.IsOfflinePlayback)
                return;

            if (context.Depth > 1)
                return;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            
                Entity entity = (Entity)context.InputParameters["Target"];
                Entity _postEntity = (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity) ? context.PostEntityImages["PostImage"] : null;
                Entity _preEntity = (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity) ? context.PreEntityImages["PreImage"] : null;

                if (entity.LogicalName != "contact")
                    return;

                try
                

                    if (context.MessageName == "Update")
                    

                        bool agr1pre= _preEntity.GetAttributeValue<bool>("agr1");
                        bool agr1post= _postEntity.GetAttributeValue<bool>("agr1");

                        bool agr2pre= _preEntity.GetAttributeValue<bool>("agr2");
                        bool agr2post= _postEntity.GetAttributeValue<bool>("agr2");




                        if (agr1pre == true && agr1post == false)
                        
                            entity.Attributes["agr2"] = false;
                            service.Update(entity);
                          
                           
                        
                       
                        
                        else if (agr2pre== false && agr2post== true)
                        
                            entity.Attributes["agr1"] = true;
                            service.Update(entity);
                        
                        

                    

                
                    
                catch (FaultException<OrganizationServiceFault> e)
                
                    tracingService.Trace("Exception: 0", e.ToString());
                    throw;
                
                catch (Exception e)
                
                    tracingService.Trace("Exception: 0", e.ToString());
                    throw;
                

            

        

        [RequiredArgument]
        [Input("contact")]
        [ReferenceTarget("contact")]
        public InArgument<EntityReference> contact  get; set; 

    

谢谢

【问题讨论】:

这个问题不是关于使用 Visual Studio,所以我已经为你删除了那个标签。 【参考方案1】:

我通过下面的代码解决了我的问题。

这段代码有什么作用? -> 更新所有具有相同邮件地址的联系人的自定义字段

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace IfZgodaChangeSetZgoda2

    public class SpojnoscZgod : Plugin
    
        public SpojnoscZgod()
            : base(typeof(SpojnoscZgod))
        
            base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "contact", new Action<LocalPluginContext>(ExecutePostKontaktUpdate)));
        
        protected void ExecutePostKontaktUpdate(LocalPluginContext localContext)
        

            if (localContext == null)
            
                throw new ArgumentNullException("localContext");
            

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            ITracingService tracingService = localContext.TracingService;
            OrganizationServiceContext _crmOrgContext = new OrganizationServiceContext(service);
            tracingService.Trace("ExecutePostFakturaUpdate Plugin: Verifying the client is not offline.");

            if (context.IsExecutingOffline || context.IsOfflinePlayback)
                return;

            if (context.Depth > 1)
                return;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            
                Entity contact = (Entity)context.InputParameters["Target"];
                Entity _postEntity = (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity) ? context.PostEntityImages["PostImage"] : null;
                Entity _preEntity = (context.PreEntityImages.Contains("PreImage") && context.PreEntityImages["PreImage"] is Entity) ? context.PreEntityImages["PreImage"] : null;

                string email = _postEntity.GetAttributeValue<string>("emailaddress1"); // get email address from contact

                bool zgodaNaMarketing = _postEntity.GetAttributeValue<bool>("alt_zgodanamarketing"); //get marketing agreement before edition
                bool zgodaNaMarketingPre = _preEntity.GetAttributeValue<bool>("alt_zgodanamarketing"); //get marketing agreement after edition
                bool alt_ZgodanamarketingnewPre = _preEntity.GetAttributeValue<bool>("alt_zgodanamassmailing"); //get marketing agreement for massmailing before edition
                bool alt_Zgodanamarketingnew_post = _postEntity.GetAttributeValue<bool>("alt_zgodanamassmailing"); //get marketing agreement for massmailing after edition

                string alt_uzasadnieniePre = _preEntity.GetAttributeValue<string>("alt_uzasadnienie"); //get justification of consent before edition
                string alt_uzasadnienie = _postEntity.GetAttributeValue<string>("alt_uzasadnienie"); //get justification of consent after edition
                

                if (contact.LogicalName != "contact") 
                    return;

                if (_postEntity.GetAttributeValue<string>("emailaddress1") == null)  return;   //if you delete mailaddress from contact add this line for doing nothing
 
                    try
                



                    QueryExpression contactQuery = new QueryExpression("contact");    // new query expression
                    contactQuery.ColumnSet = new ColumnSet(true);                   // new column set                             
                    contactQuery.Criteria = new FilterExpression();             // new filter for request
                    contactQuery.Criteria.AddCondition("emailaddress1".ToString(), ConditionOperator.Equal, email.ToString());  // get all contacts with same emailaddress1
                    EntityCollection retrievedContacts = service.RetrieveMultiple(contactQuery);  // retrieve with above criteria


                    foreach (Entity contacts in retrievedContacts.Entities)  // loop for change in all contacts 1 by 1 
                    

                        
                        if (zgodaNaMarketingPre == true && zgodaNaMarketing == false) 
                        
                            contacts.Attributes["alt_zgodanamarketing"] = false;
                            contacts.Attributes["alt_zgodanamassmailing"] = false;
                            contacts.Attributes["alt_uzasadnienie"] = alt_uzasadnienie;
                            service.Update(contacts);  

                        
                        else if (alt_ZgodanamarketingnewPre == false && alt_Zgodanamarketingnew_post == true)
                        
                            contacts.Attributes["alt_zgodanamarketing"] = true;
                            contacts.Attributes["alt_zgodanamassmailing"] = true;
                            contacts.Attributes["alt_uzasadnienie"] = alt_uzasadnienie;
                            service.Update(contacts);
                            
                        
                        else
                        
                            contacts.Attributes["alt_zgodanamarketing"] = zgodaNaMarketing;
                            contacts.Attributes["alt_zgodanamassmailing"] = alt_Zgodanamarketingnew_post;
                            contacts.Attributes["alt_uzasadnienie"] = alt_uzasadnienie;                            
                            service.Update(contacts);
                            
                        
                    

                



                catch (FaultException<OrganizationServiceFault> e)
                
                    tracingService.Trace("Exception: 0", e.ToString());
                    throw;
                
                catch (Exception e)
                
                    tracingService.Trace("Exception: 0", e.ToString());
                    throw;
                

                
            

        

        [RequiredArgument]
        [Input("contact")]
        [ReferenceTarget("contact")]
        public InArgument<EntityReference> contact  get; set; 

    

【讨论】:

以上是关于插件/Javascript - 更新所有具有相同电子邮件地址的联系人的自定义字段 agr1 和 agr2的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 使所有元素/列/容器/框具有相同的高度

Parse.com 云代码 - 无法弄清楚为啥 javascript 变量在所有数组项中具有相同的值

javascript 循环遍历具有相同类的所有元素并更改子的位置

“流”的所有声明必须具有相同的修饰符 api-ai-javascript - Dialogflow

如果json对象键在javascript中具有所有相同的值,如何检查并返回true?

如何通过 JavaScript 访问一些具有相同名称的变量? [复制]