locked
regarding plug-in assign and update record RRS feed

  • Question

  • hello everyone

       i had some requirement that when user assign the record to another user ,when it assign i want to update some attribute in it....

    please send me some sample plug-in for assign and update operation..

    please help me...


    ms crm

    Saturday, March 17, 2012 4:36 AM

Answers

  • Hi Chandhan,

    Here is a tested sample plugin code that might assist you. Hope this helps...............

    The plugin step is set up as follows:

    And the Plugin Code is as follow:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    
    namespace crm2011plugin
    {
        public class accountassign : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target")  && context.InputParameters["Target"] is EntityReference)
                {
                    // Obtain the target entity from the input parmameters.
                    EntityReference entity = (EntityReference)context.InputParameters["Target"];
                    // Verify that the target entity represents an account.
                    if (entity.LogicalName == "account" && context.MessageName == "Assign")
                    {
                        Guid accountid = new Guid(entity.Id.ToString());
                        Entity account = new Entity("account");
    
                        account.Id = accountid;
                        account.LogicalName = entity.LogicalName;
                        account["description"] = "Test Assigning Plugin....";
    
                        service.Update(account);
                    }
                }
            }
        }
    }
    
    


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Tuesday, March 20, 2012 2:43 AM

All replies

  • Hi Chandhan,

    I can suggest a couple of ways to archive the update when a record is assigned. Hope this helps.

    1. Using Workflow, this is the easier and fastest way without any coding (but it depends on what your exact requirements are)

    2 Using Code that triggers on the Assign message. Here is an example as listed in the SDK that might give you a start.

    // =====================================================================
    //  This file is part of the Microsoft CRM SDK Code Samples.
    //
    //  Copyright (C) Microsoft Corporation.  All rights reserved.
    //
    //  This source code is intended only as a supplement to Microsoft
    //  Development Tools and/or on-line documentation.  See these other
    //  materials for detailed information regarding Microsoft code samples.
    //
    //  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    //  PARTICULAR PURPOSE.
    // =====================================================================
    
    //<snippetAccountNumberPlugin>
    using System;
    
    // Microsoft Dynamics CRM namespace(s)
    using Microsoft.Xrm.Sdk;
    
    namespace Microsoft.Crm.Sdk.Samples
    {
    	public class AccountNumberPlugin: IPlugin
    	{
    		/// <summary>
            /// A plugin that auto generates an account number when an
            /// account is created.
    		/// </summary>
            /// <remarks>Register this plug-in on the Create message, account entity,
            /// and pre-event stage.
            /// </remarks>
            //<snippetAccountNumberPlugin2>
            public void Execute(IServiceProvider serviceProvider)
    		{
                // Obtain the execution context from the service provider.
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.
    			if (context.InputParameters.Contains("Target") &&
    				context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
    				Entity entity = (Entity)context.InputParameters["Target"];
                    //</snippetAccountNumberPlugin2>
    
                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName == "account") 
                    {
                        // An accountnumber attribute should not already exist because
                        // it is system generated.
    					if (entity.Attributes.Contains("accountnumber") == false)
    					{
                            // Create a new accountnumber attribute, set its value, and add
                            // the attribute to the entity's attribute collection.
    						Random rndgen = new Random();
                            entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
    					}
    					else 
    					{
    						// Throw an error, because account numbers must be system generated.
                            // Throwing an InvalidPluginExecutionException will cause the error message
                            // to be displayed in a dialog of the Web application.
                            throw new InvalidPluginExecutionException("The account number can only be set by the system.");
    					}					
    				}
    			}
    		}
    	}
    }
    //</snippetAccountNumberPlugin>
    


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Saturday, March 17, 2012 5:08 AM
  • hello eric

     this can be done through workflow but its an a asynchronous operation so am looking for plug-in to perform operation faster

    so when record is assign it must update some attribute into it.


    ms crm

    Monday, March 19, 2012 4:49 AM
  • Hi Chandhan,

    Here is a tested sample plugin code that might assist you. Hope this helps...............

    The plugin step is set up as follows:

    And the Plugin Code is as follow:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    
    namespace crm2011plugin
    {
        public class accountassign : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target")  && context.InputParameters["Target"] is EntityReference)
                {
                    // Obtain the target entity from the input parmameters.
                    EntityReference entity = (EntityReference)context.InputParameters["Target"];
                    // Verify that the target entity represents an account.
                    if (entity.LogicalName == "account" && context.MessageName == "Assign")
                    {
                        Guid accountid = new Guid(entity.Id.ToString());
                        Entity account = new Entity("account");
    
                        account.Id = accountid;
                        account.LogicalName = entity.LogicalName;
                        account["description"] = "Test Assigning Plugin....";
    
                        service.Update(account);
                    }
                }
            }
        }
    }
    
    


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    Tuesday, March 20, 2012 2:43 AM
  • hey dude thanks ur clear my query thanks

    ms crm

    Tuesday, March 20, 2012 4:29 AM