I need to make plugin or javascript what work like that: 1.When agr1(bool) change(true<->false) on contact like "John Smith" with emailaddress1 = "[email protected]" then plugin/js change agr1 field on all contacts with emailaddress1 = "[email protected]"(duplicates mails).
- I got 2 agreement (bool(yes/no)) on form: agr1 and agr2 for example, I create plugin what change agr2=false when I change agr1 from true to false and change agr1 to true when I channge agr2 from false to true - I want to do this on form when I create new Contact - how can I do this?
My code for example2(its work on exist contact not when I create):
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; }
}
}
Thanks
I resolve my problem by code below.
What this code do? -> update custom fields on all contacts with same mail address