Copado Records Cleanup

Updated 4 months ago by David Venegas

Copado creates several records that keep track of Copado activity or are auxiliary to specific functionalities. Some of these records can be safely removed at a frequent basis using the Copado Scheduled Jobs or with automated Salesforce processes like Workflows, Triggers or Process Builder Flows. 

This article goes over deleting records at a frequent basis using the Copado Scheduled Jobs 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 interrupting any Copado functionality.

Frequently Deleting Records

  1. Create a Deployment record where the Destination Org is the Org where Copado is installed. Since there won't be metadata or data deployment, the Source Org can be any Org. You can name the deployment "Copado Records Cleanup Deployment".
  2. Add a deployment step of type "Apex" 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 as "Copado Records Cleanup"
    2. Click the Copado Lookup 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 Save
  5. Click "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]) {
              promotedUserStoryIds.add(promotedUserStory.User_Story__c);
          }
           
          // 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]) {
           snapshotCommitIds.add(userStoryCommit.Snapshot_Commit__c);
          }
          delete [SELECT Id FROM Git_Org_Commit__c WHERE Id IN :snapshotCommitIds];
          

How did we do?