简体   繁体   English

日期之间所有帐户的 MS Dynamics 审计历史记录

[英]MS Dynamics audit history for all accounts between dates

I have this code which will extract audit history details of the specific Guid mentioned (see second commented line):我有这段代码,它将提取提到的特定 Guid 的审计历史详细信息(请参阅第二条注释行):

//Create a new RetrieveAttributeChangeHistoryRequest  
RetrieveAttributeChangeHistoryRequest req = new RetrieveAttributeChangeHistoryRequest();

//Set the target Entity......... Needs to be modified so ALL account records are returned instead of the specific one  mentioned              
req.Target = new EntityReference("accounts", new Guid("468f8db5-4f98-eb11-57ee-0006ffc2587a"));

//Set the attribute you want to retrieve specifically........ needs to be modified so that only records between two dates are returned              
req.AttributeLogicalName = "credit_limit";

//Execute the request against the OrgService

RetrieveAttributeChangeHistoryResponse resp = (RetrieveAttributeChangeHistoryResponse)_service.Execute(req);

AuditDetailCollection details = resp.AuditDetailCollection;

foreach (var detail in details.AuditDetails)
{
    if (detail.GetType() == typeof(AttributeAuditDetail))
    {
         AttributeAuditDetail attributeDetail = (AttributeAuditDetail)detail;

     }
}

I need to modify this so that it loops through ALL account records that are created between two dates (ex: April 1st to April 7th inclusively) instead of the one specific Guid provided.我需要修改它,以便它循环遍历在两个日期(例如:4 月 1 日至 4 月 7 日)之间创建的所有帐户记录,而不是提供的一个特定 Guid。 There are no samples provided by Microsoft that can do this. Microsoft 没有提供可以执行此操作的示例。

Your help is appreciated.感谢您的帮助。

Try below code shall work, maybe typo from my side but shall work尝试下面的代码应该可以工作,也许是我这边的错字,但应该可以

// Define Condition Values
var query_createdon = "2020-07-15T00:00:00+02:00";
var query_createdon1 = "2021-02-12T00:00:00+01:00";

// Instantiate QueryExpression query
var query = new QueryExpression("account");

// Add columns to query.ColumnSet
query.ColumnSet.AddColumns("createdon", "name", "accountid");

// Define filter query.Criteria
query.Criteria.AddCondition("createdon", ConditionOperator.GreaterThan, query_createdon);
query.Criteria.AddCondition("createdon", ConditionOperator.LessThan, query_createdon1);

EntityCollection _accounts = _service.RetrieveMultiple(query);

foreach(var _account in _accounts.Entities){
//Create a new RetrieveAttributeChangeHistoryRequest  
RetrieveAttributeChangeHistoryRequest req = new RetrieveAttributeChangeHistoryRequest();

//Set the target Entity......... Needs to be modified so ALL account records are returned instead of the specific one  mentioned              
req.Target = new EntityReference("accounts", _account.Id);

//Set the attribute you want to retrieve specifically........ needs to be modified so that only records between two dates are returned              
req.AttributeLogicalName = "credit_limit";

//Execute the request against the OrgService

RetrieveAttributeChangeHistoryResponse resp = (RetrieveAttributeChangeHistoryResponse)_service.Execute(req);

AuditDetailCollection details = resp.AuditDetailCollection;

foreach (var detail in details.AuditDetails)
{
    if (detail.GetType() == typeof(AttributeAuditDetail))
    {
         AttributeAuditDetail attributeDetail = (AttributeAuditDetail)detail;

         var recordID = "(no value)";
         var recordName = "(no value)";
         var changedBy = "(no value)";

         if (attributeDetail.OldValue.Contains("credit_limit"))
///          need to set the value of recordID in here but I don't know how
///          need to set the value of recordName in here but I don't know how
///          need to set the value of changedBy in here but I don't know how
             Console.WriteLine("Record ID: "+recordID);    /// returns (no value) because I don't know how to get the recordID value
             Console.WriteLine("Record Name: "+recordName);    /// returns (no value) because I don't know how to get the recordName value
             Console.WriteLine("Changed By: "+changedBy);    /// returns (no value) because I don't know how to get the changed by value
     }
}

}

In short what you need is two dates in between and get all the accounts.简而言之,您需要的是介于两者之间的两个日期并获取所有帐户。 Once you have all those accounts you need to loop through and then get audit history of each those accounts.拥有所有这些帐户后,您需要遍历并获取每个帐户的审计历史记录。

Note: If you are fetching more than 5K records you might want to look into paging because bydefault crm retrives only 5k records注意:如果您要获取超过 5K 条记录,您可能需要查看分页,因为默认情况下 crm 仅检索 5k 条记录

// Define Condition Values
var query_createdon = "2020-07-15T00:00:00+02:00";
var query_createdon1 = "2021-02-12T00:00:00+01:00";

// Instantiate QueryExpression query
var query = new QueryExpression("account");

// Add columns to query.ColumnSet
query.ColumnSet.AddColumns("createdon", "name", "accountid");

// Define filter query.Criteria
query.Criteria.AddCondition("createdon", ConditionOperator.GreaterThan, query_createdon);
query.Criteria.AddCondition("createdon", ConditionOperator.LessThan, query_createdon1);

EntityCollection _accounts = _service.RetrieveMultiple(query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM