Gearset can help you break down Apex into individual lines and deploy them to the target. Using the precision deployment of Apex code you are now able to deploy individual line changes to Apex.
How to use precision deployment
To be able to use precision deployments of Apex, you need to ensure that the Apex class is a
changed object in the comparison.
Choose the Apex class from the comparison grid. The green changes on the left indicate inclusions; select these to add them to the package. The red changes on the right indicate deletions; select these to remove them from the package.
Now you can use the preview changes tab to double-check that the Apex is how you're expecting it, and you can double-check for any common mistakes.
The preview selections tab in the comparison allows users to easily see what the selected Apex will look like, before you deploy those changes.
Unlike in precision deployments of XML, Gearset does not link changes together, so you need to take care when deploying changes, if there is a new line in the source that replaces one in the target, make sure to select both sides, being sure to check. This ensures that duplication of Apex does not occur.
In our experience we have also seen differences in formatting between source and target. If it is a git to org comparison (or vice versa) ensure that the changes are selected on both source and target, so this duplication does not occur.
Since Apex code is only validated during deployment to Salesforce orgs, when deploying to a Git branch, you have the ability to commit faulty Apex code. This can lead to validation errors when you eventually deploy it to an org.
In this example, we have changed an integer value, and forgotten to select the deletion as well as the inclusion.
Here if we check the preview selections, notice that the value has been duplicated.
If a line you're excluding from your selection has no trailing comma, the Apex will not validate as expected.
In this example Dev A has added
Belgium to the list but their teammate (Dev B) has added
Portugal before Dev A could deploy. Unfortunately Dev A now can’t deploy only
Belgium because it would leave a trailing comma, which is considered invalid Apex.
To fix this Dev A, reorders the changes so that their change is the last on the list and can therefore deploy only their addition.