Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

(CRM2015) Plug-in - Update field marketing agreement on all Contacts with same email addres

(0) ShareShare
ReportReport
Posted on by

Hello,
I need to make plug-in what work like that:
When marketing agreement(bool) change(true<->false) on contact like "John Smith" with emailaddress1 = "john1@example.com" then plug-in change marketing agreement on all contacts with emailaddress1 = "john1@example.com"

How can I do this?

Thanks

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: (CRM2015) Plug-in - Update field marketing agreement on all Contacts with same email addres

    I resolve my problem using queryexpression and collections. Code below.

    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>(40, "Update", "contact", new Action(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("emailaddress1"); // get email address from contact
    
                    bool zgodaNaMarketing = _postEntity.GetAttributeValue("alt_zgodanamarketing"); //get marketing agreement before edition
                    bool zgodaNaMarketingPre = _preEntity.GetAttributeValue("alt_zgodanamarketing"); //get marketing agreement after edition
                    bool alt_ZgodanamarketingnewPre = _preEntity.GetAttributeValue("alt_zgodanamassmailing"); //get marketing agreement for massmailing before edition
                    bool alt_Zgodanamarketingnew_post = _postEntity.GetAttributeValue("alt_zgodanamassmailing"); //get marketing agreement for massmailing after edition
    
                    string alt_uzasadnieniePre = _preEntity.GetAttributeValue("alt_uzasadnienie"); //get justification of consent before edition
                    string alt_uzasadnienie = _postEntity.GetAttributeValue("alt_uzasadnienie"); //get justification of consent after edition
                    
    
                    if (contact.LogicalName != "contact")
                        return;
    
                    if (_postEntity.GetAttributeValue("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 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 contact { get; set; }
    
        }
    }

  • Community Member Profile Picture
    on at
    RE: (CRM2015) Plug-in - Update field marketing agreement on all Contacts with same email addres

    Thanks for reply, but I need how to set fields on all contact with same email address, can be in javascript too, but I dont think is possible without c# plug-in

  • Esteban Coto Alfaro Profile Picture
    on at
    RE: (CRM2015) Plug-in - Update field marketing agreement on all Contacts with same email addres

    Hello, you could create a function that would be triggered when an event occurs, in this case when the value of the field change.

    Here I attach an example: carldesouza.com/.../

    In "Form Properties" set the Event to "OnChange"

    Thanks!

    Community Support Team - Esteban

    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 91

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans