Query and delete review history in CRM

Source: Internet
Author: User

Using system;
Using system. LINQ;
Using Microsoft. xrm. SDK;
Using Microsoft. xrm. SDK. query;
Using Microsoft. CRM. SDK. messages;
Using Microsoft. xrm. SDK. messages;
Using system. Collections. Generic;

/// <Summary>
/// Review history
/// </Summary>
Public class audithistory
{
/// <Summary>
/// Retrieve all changes made to a specific attribute
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "target"> record object </param>
/// <Param name = "attrname"> Field name </param>
/// Special note: The field must be reviewed
Public void retrieveattributehistory (iorganizationservice service, entityreference target, string attrname)
{
Retrieveattributechangehistoryrequest request = new retrieveattributechangehistoryrequest ();
Request. attributelogicalname = attrname;
Request. Target = target;
Request. paginginfo = new paginginfo () {COUNT = 1000, pagenumber = 1 };
Retrieveattributechangehistoryresponse response = (retrieveattributechangehistoryresponse) service. Execute (request );
// Result
Auditdetailcollection = response. auditdetailcollection;
}

/// <Summary>
/// All changes made to a specific object
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "target"> Target record </param>
Public void retrieveentityhistory (iorganizationservice service, entityreference target)
{
Retrieverecordchangehistoryrequest request = new retrieverecordchangehistoryrequest ();
Request. Target = target;
Request. paginginfo = new paginginfo () {COUNT = 1000, pagenumber = 1 };
Retrieverecordchangehistoryresponse response = (retrieverecordchangehistoryresponse) service. Execute (request );
// Result
Auditdetailcollection = response. auditdetailcollection;
}

/// <Summary>
/// Delete all partitions that contain audit data created before the specified end date
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "enddate"> end date </param>
Public void deleteaudit (iorganizationservice service, datetime enddate)
{
Deleteauditdatarequest request = new deleteauditdatarequest ();
Request. enddate = enddate;
Deleteauditdataresponse response = (deleteauditdataresponse) service. Execute (request );
}

/// <Summary>
/// Delete audit records
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "auditdetailcollection"> record set </param>
Public void deleteaudit (iorganizationservice service, auditdetailcollection)
{
If (auditdetailcollection. auditdetails! = NULL & auditdetailcollection. auditdetails. Count> 0)
{
Executemultiple multiple = new executemultiple ();
Multiple. adddelete (auditdetailcollection. auditdetails. Select (A => A. auditrecord ));
Multiple. Execute (service );
}
}

 

/// <Summary>
/// Batch operation
/// </Summary>
Public class executemultiple
{
Private int Len = 1500;
Private list <executemultiplerequest> List = new list <executemultiplerequest> ();

Public executemultiple ()
{
New ();
}

Public void new ()
{
Executemultiplerequest multiple = new executemultiplerequest ()
{
Settings = new executemultiplesettings ()
{
Continueonerror = false,
Returnresponses = true
},
Requests = new organizationrequestcollection ()
};
List. Add (multiple );
}

Public void add (organizationrequest request)
{
Executemultiplerequest multiple = List. lastordefault ();
If (Multiple. Requests. Count> = Len)
{
New ();
Multiple = List. lastordefault ();
Multiple. Requests. Add (request );
}
Else
{
Multiple. Requests. Add (request );
}
}

Public void addcreate (entity en)
{
Createrequest request = new createrequest () {target = EN };
Add (request );
}

Public void addupdate (entity en)
{
Updaterequest request = new updaterequest () {target = EN };
Add (request );
}

Public void adddelete (entityreference ER)
{
Deleterequest request = new deleterequest () {target = ER };
Add (request );
}

Public void adddelete (entity en)
{
Adddelete (New entityreference () {logicalname = en. logicalname, id = en. ID });
}

Public void adddelete (entitycollection EC)
{
If (EC! = NULL & EC. Entities. Count> 0)
{
Adddelete (EC. Entities );
}
}

Public void adddelete (datacollection <entity> List)
{
If (list! = NULL & list. Count> 0)
{
Foreach (entity en in List)
{
Adddelete (en );
}
}
}

Public void adddelete (ienumerable <entity> List)
{
If (list! = NULL & list. Count ()> 0)
{
Foreach (entity en in List)
{
Adddelete (en );
}
}
}

Public void execute (iorganizationservice Service)
{
Foreach (executemultiplerequest request in List)
{
Service. Execute (request );
}
}
}

 



}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.