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
}
}
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
}
}
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
ReplyDeleteMaxMunus 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/