Answered by:
How to capture a record when it is deleting

Question
-
Hi All,
I have created a custom entity as account history and now I want to get all account records which are deleted should be captured and save it in account history entity.
Please help me how can we solve this issue.
Thanks
Tuesday, May 6, 2014 6:17 AM
Answers
-
Hi,
Use following code to create record in accoutHistory entity when the account is deleted
Note: create one datetime filed in accountHistory Entityt to store account CREATEDON value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
namespace CRM_Plugins
{
public class AccountHistory:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
EntityReference account = context.InputParameters["Target"] as EntityReference;
//register Preimage for Plugin Step
Entity preimage = (Entity)context.PreEntityImages["PreImage"];
//get the name attribute value
string accountname = preimage.GetAttributeValue<string>("name");
//get the CreatedOn field value
DateTime createdon = preimage.GetAttributeValue<DateTime>("createdon");
//following code used to create AccountHistory
//Note: here change your accountHistory entity schemaname and filed schemanames
//new_accounthistory accountHistory entity schemaname
Entity accounthistory = new Entity("new_accounthistory");
//accountHistory Name field schemaname
accounthistory["new_name"] = accountname;
//accountHistory entity datetime field schemaname
accounthistory["new_accountcreatedon"] = createdon;
service.Create(accounthistory);
}
}
}
Plugin Registrationsteps:
Message: DELETE
Primary Entity: account
Event Pipeline Execution: Post operation
Image: PreImage
Plugin step Registration:
Plugin Image Registration :
PreImage
- Proposed as answer by Gugan A Tuesday, May 6, 2014 8:08 AM
- Marked as answer by Microsoft Dynamics CRM Tuesday, May 6, 2014 11:08 AM
Tuesday, May 6, 2014 8:08 AM
All replies
-
Hi,
Create plugin for ACCOUNT entity DELETE message.
in that plugin when the account record is deleted create record in AccountHistory entity
Tuesday, May 6, 2014 6:52 AM -
will you please share the code
Tuesday, May 6, 2014 6:54 AM -
Hi,
In accountHistory entity what are the fields your capturing from account
i mean accounthistory entity contains all the field same as account or just account lookup
Tuesday, May 6, 2014 7:01 AM -
just account lookupTuesday, May 6, 2014 7:09 AM
-
After account deleted the account lookup in accountHistory entity will become Null. then how will you track accountHistory for particular accountTuesday, May 6, 2014 7:12 AM
-
just I need to maintain only 2 fields like name and created on . I am new to CRM don't mind if I am wrong
Tuesday, May 6, 2014 7:15 AM -
Hi,
Use following code to create record in accoutHistory entity when the account is deleted
Note: create one datetime filed in accountHistory Entityt to store account CREATEDON value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
namespace CRM_Plugins
{
public class AccountHistory:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
EntityReference account = context.InputParameters["Target"] as EntityReference;
//register Preimage for Plugin Step
Entity preimage = (Entity)context.PreEntityImages["PreImage"];
//get the name attribute value
string accountname = preimage.GetAttributeValue<string>("name");
//get the CreatedOn field value
DateTime createdon = preimage.GetAttributeValue<DateTime>("createdon");
//following code used to create AccountHistory
//Note: here change your accountHistory entity schemaname and filed schemanames
//new_accounthistory accountHistory entity schemaname
Entity accounthistory = new Entity("new_accounthistory");
//accountHistory Name field schemaname
accounthistory["new_name"] = accountname;
//accountHistory entity datetime field schemaname
accounthistory["new_accountcreatedon"] = createdon;
service.Create(accounthistory);
}
}
}
Plugin Registrationsteps:
Message: DELETE
Primary Entity: account
Event Pipeline Execution: Post operation
Image: PreImage
Plugin step Registration:
Plugin Image Registration :
PreImage
- Proposed as answer by Gugan A Tuesday, May 6, 2014 8:08 AM
- Marked as answer by Microsoft Dynamics CRM Tuesday, May 6, 2014 11:08 AM
Tuesday, May 6, 2014 8:08 AM -
Thank You Very much ...Tuesday, May 6, 2014 8:56 AM
-
Its ok , Mark as answer if its helpfulTuesday, May 6, 2014 9:27 AM