Wednesday, January 13, 2021

How To Create Daily Bulk Delete Jobs in Dataverse/CDS/Power Apps/CRM As A Different User

UPDATE!

The first statement is a lie!  The UI for Dataverse/CDS/Power Apps/CRM bulk delete jobs does allow for creating a reoccurring daily Bulk Delete Job (Thanks Oliver Flint).   Even though it looks like a dropdown, you can type whatever number you’d like.  As such, this post is still helpful if you want to create a duplicate bulk delete job in multiple environments, or if you want to create it with someone else like an App User as the owner.

Original Post:

The UI for Dataverse/CDS/Power Apps/CRM bulk delete jobs does not allow for creating a reoccurring daily Bulk Delete Job.  The smallest value to choose from is weekly, which means if you want to run something daily, you’d have to create 7 jobs, one for each day of the week. Ew!  But, this can be set programatically via the SDK, and here is how (Please note, this is just code, it can be compiled and run anywhere.  When you run from the XTB though, you can either login with an Application User, or impersonate it if you have impersonation rights, which would set the owner of the bulk delete record, and help prevent any issues when a user leaves, but owns all of the Bulk Delete Jobs):

  1. Open the XrmToolBox, and connect to the environment (Bonus, connect with an application user to create the Bulk Delete Job as an application user, so that it isn’t owned by a person that leaves the company or has permissions removed.)
  2. Install the Code Now XrmToolBox Plugin if not already installed, and open it.
  3. If the logged in user is the XTB is the desired user, great, if you’d like the bulk delete request to be owned by a different user, push the Impersonate button at the top of the XTB and select the appropriate user.  (Testing has shown that impersonating the System user will not work to set the owner as system.  An Application User will be required)
  4. Copy and paste the following code into the window:
public static void CodeNow()
{
    var bulkDeleteRequest = new Microsoft.Crm.Sdk.Messages.BulkDeleteRequest
    {
        JobName = "Daily 3am Delete Job",
        QuerySet = new [] {
            new QueryExpression {
                ColumnSet = new ColumnSet("acme_tableid", "acme_tablename", "createdon"),
                EntityName = "acme_table",
                Criteria = {
                    Filters = {
                        new FilterExpression {
                            FilterOperator = LogicalOperator.And,
                            Conditions = {
                                new ConditionExpression("acme_delete_me", ConditionOperator.Equal, true)
                            }
                        }
                    }
                }
            }
        },
        StartDateTime = new DateTime(2021, 1, 8, 8, 0, 0, DateTimeKind.Utc),
        RecurrencePattern = "FREQ=DAILY;INTERVAL=1;",
        ToRecipients = new Guid[] { },
        CCRecipients = new Guid[] { },
        SendEmailNotification = false
    };

    Service.Execute(bulkDeleteRequest);
}

 

Update the following values

  1. Update the JobName to what ever your preference is.
  2. QuerySet is a collection of QueryExpressions.  Add at least one (I don’t know what happens if you add two, my guess is that all records returned from all Query Expressions will get deleted.  My guess is it is expecting to always have just the Primary Id of the table, the Primary Name Column, and the “Created On” column.
  3. Update the StartDateTime a future date to start. It's format is new DateTime(YYYY, MM, DD, ... ). Please note, the time is UTC, so the current value is Jan. 8, 2021 at 3am EST (Not EDT). 
  4. Update RecurrencePattern to your liking.  FREQ=DAILY;INTERVAL=1; means it will be ran every day.  (Not sure of other FREQ values could be, but you could use the FetchXmlBuilder to query for other existing values)
  5. Update ToRecipients and CCRecipients to what I believe are the SystemUser Ids that would be notified when the job runs.  (I’ve never used it, so don’t quote me on this one)
  6. Update SendEmailNotification to true to send out emails to the To and CC Recipients if desired.

Run the Code in Code Now… err… now!

Verify that the record was created correctly by navigating to the Bulk Delete Jobs:

  1. Open Advanced Settings by clicking the gearbox icon in the top right-hand corner
  2. Navigate to the "Data Management" area
  3. Click "Bulk Record Deletion"
  4. Select Recurring Bulk Delete System Jobs
  5. Verify that the job is created with the correct Owner and the Next Run values, and that it is in a "Waiting" status reason.
  6. You can verify the history of this job by using the Completed Bulk Deletion System Jobs view once the next run time has passed.

2 comments:

Parul Pathak said...

This site helps to clear your all query. uniraj ba 3rd year result
shekhauni ba final result This is really worth reading. nice informative article.

Mansi Devgan said...

Therefore dissertation web-sites as a result of online to set-up safe and sound ostensibly taped in the website.

UOK BCom Result
UOK BCom 1st Year Result
UOK BCom 2nd Year Result
UOK BCom 3rd Year Result