Copado Records Cleanup

Updated 2 weeks ago by Copado Solutions

Copado creates several records that keep track of Copado's activity or are auxiliary to specific functionalities. Some of these records can be safely removed on a frequent basis using Copado's scheduled jobs or with automated Salesforce processes like workflows, triggers or process builder flows. 

This article goes over deleting records and also provides example code snippets for deleting records that are relevant during a short period of time (e.g. when a user story is Work in Progress) but that can be later deleted without disrupting any Copado functionality.

Deleting Records on a Frequent Basis

  1. Create a Deployment record where the destination org is the org where Copado is installed. Since there won't be any metadata or data deployment, the source org can be any org. You can name the deployment 'Copado Records Cleanup Deployment'.
  2. Add an Apex deployment step for each of the following code snippets that you would like to include in the cleanup:
    1. // Copado Notification records are created whenever the Copado Heroku Backend sends a notification to the Copado application on Salesforce. delete [SELECT Id From copado__Copado_Notification__c WHERE CreatedDate > LAST_WEEK ];
    2. // Copado Deployment History records are created whenever changes happen in any Deployment record (e.g. creating or editing a step, changing the step order). delete [SELECT Id From copado__Deployment_History__c WHERE CreatedDate > LAST_90_DAYS AND copado__Deployment__r.copado__Status__c = 'Completed Successfully'];
    3. // User Story Metadata records are created in user stories to track potential conflicts across multiple user stories. Once the user story has been promoted to all environments these records are no longer relevant. delete [SELECT Id FROM copado__User_Story_Metadata__c WHERE copado__User_Story__r.copado__Status__c = 'Completed'];
    4. // User Story Commit and related Snapshot Commit records are created whenever a commit is done in a user story. Deleting these records won't affect commit history since it remains intact in the Git Repository. The User story Commit is a junction object therefore the User Story Commit is deleted automatically by simply deleting the related Snapshot Commit. Set<Id> snapshotCommits = new Set<Id>(); for(copado__User_Story_Commit__c userStoryCommit : [SELECT Id, copado__Snapshot_Commit__c FROM copado__User_Story_Commit__c WHERE copado__User_Story__r.copado__Status__c = 'Completed']) { snapshotCommits.add(userStoryCommit.copado__Snapshot_Commit__c); } delete [SELECT Id FROM copado__Git_Org_Commit__c WHERE Id IN :snapshotCommits];
  3. Save the deployment.
  4. Open the Scheduled Jobs tab and create a new record.
    1. Name the scheduled job 'Copado Records Cleanup'.
    2. Click on Copado Look up Webhook and select Execute a Deployment.
    3. In the deployment lookup, select the deployment created in step 1.
    4. Select your user to run the scheduled job and click on Save.
  5. Click on Schedule and select the desired frequency for the cleanup deployment to be executed (e.g. every Friday)

Example: Deleting User Story Related Records After Successful Promotion to Production

Id promotionId; // Get the promotion record Id from the Trigger implementation. The deletion criteria should include the following: Promotion__r.Status__c = 'Completed Successfully' AND Promotion__r.Destination_Environment__r.Name = 'Production'
// Get the list of User Story Ids from the Promoted User Story records. The Promoted User Story object is a junction object between the Promotion and the User Stories being promoted.
Set<Id> promotedUserStoryIds = new Set<id>();
for(Promoted_User_Story__c promotedUserStory : [SELECT Id, User_Story__c FROM Promoted_User_Story__c WHERE Promotion__c = :promotionId]) {

// Delete the User Story Metadata records related to the promoted User Stories
delete [SELECT Id FROM User_Story_Metadata__c WHERE User_Story__c IN :promotedUserStoryIds];

// Delete User Story Commits and their related Snapshot Commit records. The User story Commit is a junction object therefore the User Story Commit is deleted automatically by simply deleting the related Snapshot Commit.
Set<Id> snapshotCommitIds = new Set<Id>();
for(User_Story_Commit__c userStoryCommit : [SELECT Id, Snapshot_Commit__c FROM User_Story_Commit__c WHERE User_Story__c IN :promotedUserStoryIds]) {
delete [SELECT Id FROM Git_Org_Commit__c WHERE Id IN :snapshotCommitIds];

How did we do?