Deleting workflow logs using the SDK

To delete workflow logs using the SDK use the RetrieveMultiple and Delete messages.

Set up a query expression that defines which workflow logs to delete. For example, to delete workflow logs older than a certain date:

// set the columns to be returned
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { "createdon", "description" };
// Create the on or before condition
ConditionExpression dateCondition = new ConditionExpression();
dateCondition.AttributeName = "createdon";
// Changed from OnOrBefore to LessThan so that time can be specified as well as date
dateCondition.Operator = ConditionOperator.LessThan;
dateCondition.Values = new object[1];
// Note date format mm/dd/yyyy
dateCondition.Values[0] = "03/18/2008 03:00";
/* ConditionExpression dateCondition = new ConditionExpression();
dateCondition.AttributeName = "description";
dateCondition.Operator = ConditionOperator.Equal;
dateCondition.Values = new string[] { "Phone call is closed" };
*/
// Create the filter
FilterExpression dateFilter = new FilterExpression();
dateFilter.FilterOperator = LogicalOperator.And;
dateFilter.Conditions = new ConditionExpression[] { dateCondition};
// Put everything together in an expression
QueryExpression qryExpression = new QueryExpression();
qryExpression.EntityName = EntityName.workflowlog.ToString();
qryExpression.Criteria = dateFilter;
qryExpression.ColumnSet = cols;

Then run the query and iterate through each log and call the delete message:

// Return all records
qryExpression.Distinct = false;
// Create the request.
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
// Set the request properties.
retrieve.Query = qryExpression;
// Execute the query
BusinessEntityCollection logsResultSet = service.RetrieveMultiple(qryExpression);
int number_logs ;
number_logs = 0;
// Iterate through each log
foreach (workflowlog aLog in logsResultSet.BusinessEntities)
{
// Access only columns included in the column set of the query expression
// NOTE: All other columns will be null EXCEPT for the entities ID, which is always returned
number_logs = number_logs + 1;
// Console.WriteLine("Workflow Log's ID: " + aLog.workflowlogid.Value+"Started On: " + aLog.createdon.date);
// Console.WriteLine("Workflow Description: " + aLog.description);
// Create the target object for the request.
TargetDeleteWorkflowLog target = new TargetDeleteWorkflowLog();
// EntityId is the GUID of the record being deleted.
target.EntityId = aLog.workflowlogid.Value;
// Create the request object.
DeleteRequest delete = new DeleteRequest();
delete.Target = target;
// Execute the request.
DeleteResponse deleted = (DeleteResponse)service.Execute(delete);
// Console.WriteLine("has been deleted.");
}

Leave a Comment

Your email address will not be published.