Activate/ Deactivate a record using C# in Dynamics CRM 2011/ 2013/ 2015

In this blog I am going to show how we could Activate/ Deactivate a record in Dynamics CRM using c#.  For this we cannot simply activate or deactivate records by simply setting statecode and statuscode in the early bound entity records. To do this, we must use SetStateRequest and SetStateResponse from Microsoft.Crm.Sdk.Proxy assembly. Add all required updated dll referances in your project.
               
Below is the methods separately for Activating & Deactivating a record::


public static void DeactivateRecord(Guid recordId, OrganizationServiceProxy organizationService)
        {

            var EntityName = organizationService.Retrieve("EntityName", recordId, new ColumnSet(new[] { "statecode", "statuscode" }));
            try
            {
                if (EntityName != null)
                {
                    SetStateRequest objSetStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference("EntityName", recordId),
                        State = new OptionSetValue(1),
                        Status = new OptionSetValue(2),
                    };
                    organizationService.Execute(objSetStateRequest);
                }
            }
            catch (TimeoutException ex)
   {
//Exception Code
   }
        }

       //Activate a record
        public static void ActivateRecord(Guid recordId, OrganizationServiceProxy organizationService)
        {

            var entEntityName = organizationService.Retrieve("EntityName", recordId, new ColumnSet(new[] { "statecode", "statuscode" }));
            try
            {
                if (entEntityName != null)
                {
                    SetStateRequest objSetStateRequest = new SetStateRequest()
                    {
                        EntityMoniker = new EntityReference("EntityName", recordId),
                        State = new OptionSetValue(0),
                        Status = new OptionSetValue(1),
                    };
                    organizationService.Execute(objSetStateRequest);
                }
            }
            catch (TimeoutException ex)
   {
//Exception Code
   }
        }

Comments

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Microsoft CRM, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Microsoft CRM. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    MaxMunus
    E-mail: nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023
    http://www.maxmunus.com/



    ReplyDelete

Post a Comment

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image