0
votes

I am trying to make a plugin for CRM 2011 that will update some values in a contact entity when it has been disabled.

When a contact gets disabled: I want it to change 3 radio buttons to "Nei" (No in Norwegian). This will disable the access to my self-service portal i have for my customers. You can see a picture of my contact entity with the radio buttons in it here. I want to force all those radio buttons to "Nei" when contact is disabled.

I am a complete beginner with CRM plugin development, and a fairly new user of C#. So please keep it as simple as possible.

I have been reading through the manuals for weeks and i cant seem to get anywhere. (Well, Microsoft is not known for their well written manuals).

1
I replied to your question. One thing: if you say "Microsoft is not known for their well written manuals", at least show us some proof of your statement, otherwise is an insult to all the people that works (and worked) on MSDN Library.Guido Preite
@GuidoPreite Thanks a lot! I am not saying ALL MSDN documentation is bad. What i am saying is some of them could use more work. I can use this as an example. This documentation does not really help beginners like me understand what is going on there. And based on the rating of the entry: Not does it help a lot of other uses as well... I am not going to take this discussion futher here on StackOverflow.Alexander Johansen

1 Answers

1
votes

You need to register your plugin to both SetState and SetStateDynamicEntity messages, with Pre-Operation as execution stage. Take this code as an example:

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace TestPlugin
{
    public class UpdateBoolFields : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            try
            {
                if (context.InputParameters.Contains("EntityMoniker") &&
                   context.InputParameters["EntityMoniker"] is EntityReference)
                {
                    EntityReference targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
                    OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
                    if (state.Value == 1)// I'm not sure is 1 for deactivate
                    {
                        IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                        Entity contact = service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet(true));
                        contact["field1"] = false;
                        contact["field2"] = false;
                        contact["field3"] = false;
                        service.Update(contact);
                    }
                }

            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }
}