What do you need help with?

Custom Validation Logic before deployments

Follow

Adding custom validation logic to your deployment process can serve many purposes. For example:

  • ensuring business approval has been obtained before a deployment goes to production
  • verifying only components from the repository are included in the deployment manifest

Each business will have different reasons and methods to ensure their release requirements are met before deployments are approved. 

Flosum now provides a framework for adding controls to the release process so you can test for specific conditions before allowing deployment to proceed. Using this framework you can embed your own validation logic which will be executed before deployment.

For every deployment or validation, Flosum creates a record in the Flosum Deployment Approval custom object (Flosum__Deployment_Approval__c). The fields of this object include: which org is the change being deployed/validated on, who is doing it, which deployment is being deployed/validated and the tests that will be executed. Using these fields, you can test certain conditions to validate if the requirements have been met.

This is done by adding a new before insert trigger to the Flosum__Deployment_Approval__c custom object. Before the deployment starts, Flosum will pass control to the trigger to execute your custom logic. Using this custom object's field values (see below for current fields) and your custom logic, you can test for specific conditions. Based on the outcome of the tests you can then choose whether to allow the process to proceed or throw an exception with the appropriate error message in the trigger, stopping the process. The message will be shown to the user when deployment cannot continue.

As seen in the trigger below, if you would like to stop the deployment, you can throw an exception in the trigger. This exception will be shown to the user on the deployment page. If you would like the user to continue with the deployment, you can just allow the completion of the trigger without any error or exception.

The Flosum__Deployment_Approval__c custom object makes Flosum information available for inspection through these fields:

Flosum__Org__c Lookup(Org)
Flosum__Patch__c Lookup(Patch)
Flosum__User__c Lookup(User)
Flosum__Run_Test_Param__c Text
Flosum__Specified_Tests__c Long Text Area

Here is an example of a trigger that could be used as a starting point. This trigger does not allow deployment on Production:

trigger DeploymentApprovalTrigger on Flosum__Deployment_Approval__c (before insert) {
  if(Trigger.isBefore && Trigger.IsInsert)
  {
    Map<String,String> orgNameIdMap = new Map<String,String>();
    for(Flosum__Organisation__c org : [SELECT Id,Name FROM Flosum__Organisation__c])
    {
      orgNameIdMap.put(org.Id,org.Name);
    }
    for(Flosum__Deployment_Approval__c aprroval : Trigger.New)
    {
      String orgName = orgNameIdMap.get(aprroval.Flosum__Org__c);
      if(orgName == 'Production')
      {
        aprroval.addError('You can not deploy to this organization.');
      }
    }
  }
}

When writing a deployment trigger, you will need to write a test class to satisfy Salesforce code coverage requirements. 

Please find some sample code below that can give you a start in writing your own test class. 


@isTest
private class FlosumDeploymentApprovalTriggerTest
{
  public static testMethod void deploymentApprovalTestMethod()
  {
      Flosum__Organisation__c org = new Flosum__Organisation__c();
      org.Name = 'test';
      org.Flosum__Username__c = 'test@testOrg.com';
      insert org;

      Flosum__Deployment_Approval__c depApproval = new Flosum__Deployment_Approval__c();
      depApproval.Flosum__Org__c = org.Id;
      insert depApproval;

      Flosum__Snapshot__c Snap = new Flosum__Snapshot__c();
      snap.Name = 'test snap';
      snap.Flosum__Organisation__c = org.Id;
      snap.Flosum__Is_Completed__c = true;
 snap.Flosum__Last_Modified_In_Days__c = 14;
      insert snap;

      Flosum__Patch__c patch = new Flosum__Patch__c();
      patch.Name = 'test patch';
      insert patch;

      System.assertNotEquals(null,org.Id);
  }
}

 

 

Print Friendly and PDF
Was this article helpful?
1 out of 1 found this helpful

Comments