Skip to content

Commit

Permalink
feat: add universal andFinally method (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschach committed Feb 27, 2023
2 parents a286275 + b4ce9d8 commit 0988732
Show file tree
Hide file tree
Showing 28 changed files with 559 additions and 1,395 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
# Install PMD
- name: "Install PMD"
run: |
PMD_VERSION=`cat pmd/pmd-version.txt`
PMD_VERSION=$(curl -s https://api.github.com/repos/pmd/pmd/releases/latest | grep '.tag_name' | sed 's:.*/::' | sed 's:",::')
wget https://github.com/pmd/pmd/releases/download/pmd_releases%2F$PMD_VERSION/pmd-bin-$PMD_VERSION.zip
unzip pmd-bin-$PMD_VERSION.zip -d ~
mv ~/pmd-bin-$PMD_VERSION ~/pmd
Expand Down
41 changes: 0 additions & 41 deletions .github/workflows/code-coverage.yml

This file was deleted.

40 changes: 11 additions & 29 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,20 @@ jobs:

scratch-org-test:
runs-on: ubuntu-latest
needs: format-lint-lwc-tests
if: ${{ github.actor != 'dependabot[bot]' }}
steps:
# Checkout the source code
- name: "Checkout source code"
uses: actions/checkout@v3

# Run PMD scan
- name: "Run PMD scan"
uses: pmd/pmd-github-action@v1
id: pmd
with:
sourcePath: "force-app"
rulesets: "pmd/ruleset.xml"
analyzeModifiedFilesOnly: false

# Check for PMD violations
- name: "Check for PMD violations"
if: steps.pmd.outputs.violations != 0
run: exit 1

# Install Salesforce CLI
- name: "Install Salesforce CLI"
run: |
wget https://developer.salesforce.com/media/salesforce-cli/sfdx/channels/stable/sfdx-linux-x64.tar.xz
mkdir ~/sfdx
tar xJf sfdx-linux-x64.tar.xz -C ~/sfdx --strip-components 1
echo "$HOME/sfdx/bin" >> $GITHUB_PATH
~/sfdx/bin/sfdx version
npm install sfdx-cli --global
nodeInstallPath=$(npm config get prefix)
echo "$nodeInstallPath/bin" >> $GITHUB_PATH
sfdx --version
npm install @salesforce/cli --global
# Store secret for dev hub
- name: "Populate auth file with SALESFORCE_AUTH_URL secret"
Expand All @@ -88,31 +73,28 @@ jobs:
# Authenticate dev hub
- name: "Authenticate Dev Hub"
run: sfdx auth:sfdxurl:store -f ./SALESFORCE_AUTH_URL.txt -a devhub -d
run: sfdx auth:sfdxurl:store --sfdxurlfile ./SALESFORCE_AUTH_URL.txt --setalias devhub --setdefaultdevhubusername

# Create scratch org
- name: "Create scratch org"
run: sfdx force:org:create -f config/project-scratch-def.json -a scratch-org -s -d 1
run: sf org create scratch --definition-file config/project-scratch-def.json --alias ScratchOrg --set-default --duration-days 1 --wait 20

# Deploy source to scratch org
- name: "Push source to scratch org"
run: sfdx force:source:push
run: sf deploy metadata

# Run Apex tests in scratch org
- name: "Run Apex tests"
run: sfdx force:apex:test:run -c -r human -d ./tests/apex -w 20

# Delete temporary test file that Codecov is unable to parse
- name: "Delete coverage file (temporary step)"
run: rm ./tests/apex/test-result-707*-codecoverage.json
run: sf apex run test --test-level RunLocalTests --code-coverage --result-format human --output-dir ./tests/apex --wait 20

# Upload code coverage data
- name: "Upload code coverage for Apex to Codecov.io"
uses: codecov/codecov-action@v3
with:
flags: Apex
token: ${{ secrets.CODECOV_TOKEN }}

# Housekeeping
- name: "Delete scratch org"
if: always()
run: sfdx force:org:delete -p -u scratch-org
run: sf org delete scratch --no-prompt --target-org ScratchOrg
10 changes: 1 addition & 9 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

VERSION=$(curl -s https://api.github.com/repos/pmd/pmd/releases/latest | grep '.tag_name' | sed 's:.*/::' | sed 's:",::')
echo $VERSION

printf '%s' "$VERSION" > pmd/pmd-version.txt
git add pmd/pmd-version.txt

npm run updateHighlight
npm run precommit
git add docs/*.html
npm run precommit
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"files": "*.html",
"options": { "useTabs": false, "tabWidth": 2 }
"options": { "parser": "html", "useTabs": false, "tabWidth": 2 }
},
{
"files": "*.css",
Expand Down
26 changes: 14 additions & 12 deletions DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

1. Set up your environment. Follow the steps in the [Quick Start: Lightning Web Components](https://trailhead.salesforce.com/content/learn/projects/quick-start-lightning-web-components/) Trailhead project. The steps include:

- Enable Dev Hub in your org or Trailhead Playground
- Install [Visual Studio Code](https://developer.salesforce.com/tools/vscode/en/getting-started/install/#visual-studio-code)
- Install [Salesforce CLI](https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_intro.htm)
- Install the [Visual Studio Code Salesforce extensions](https://developer.salesforce.com/tools/vscode/en/getting-started/install/#salesforce-extensions-for-visual-studio-code)
<ul>
<li>Enable Dev Hub in your org or Trailhead Playground</li>
<li>Install <a target="_blank" href="https://developer.salesforce.com/tools/vscode/en/getting-started/install/#visual-studio-code">Visual Studio Code</a></li>
<li>Install <a target="_blank" href="https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_intro.htm">Salesforce CLI</a></li>
<li>Install the <a target="_blank" href="https://developer.salesforce.com/tools/vscode/en/getting-started/install/#salesforce-extensions-for-visual-studio-code">Visual Studio Code Salesforce extensions</a></li>
</ul>

1. If you haven't already done so, authorize your hub org and provide it with an alias (**myhuborg** in the command below):

```
```plaintext
sfdx auth:web:login --setdefaultdevhubusername --setalias myhuborg
```

1. Clone the duplicatehandling repository:

```
```plaintext
git clone https://github.com/dschach/salesforce-trigger-framework
cd salesforce-trigger-framework
```
Expand All @@ -32,20 +34,20 @@

1. Create a scratch org and provide it with an alias (**triggerHandler** in the command below):

```
sfdx force:org:create -s -f config/project-scratch-def.json -a triggerHandler
```plaintext
sf org create scratch --set-default --definition-file config/project-scratch-def.json --alias triggerHandler
```

This will create a new scratch org and install all metadata in this repository

2. Push the app to your scratch org:

```
sfdx force:source:push
```plaintext
sf deploy metadata
```

3. Open the scratch org:

```
sfdx force:org:open
```plaintext
sf org open
```
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@

# Salesforce Trigger Framework

- [Salesforce Trigger Framework](#salesforce-trigger-framework)
- [Credit](#credit)
- [Documentation](#documentation)
- [Overview](#overview)
- [Usage](#usage)
- [Trigger Handler](#trigger-handler)
- [Trigger](#trigger)
- [Cool Stuff](#cool-stuff)
- [Bypass API](#bypass-api)
- [Check Bypass Status](#check-bypass-status)
- [Global Bypass](#global-bypass)
- [Set Bypass](#set-bypass)
- [Max Loop Count](#max-loop-count)
- [Debug Statements](#debug-statements)
- [Universal Action](#universal-action)
- [Overridable Methods](#overridable-methods)

## Credit

Based on Kevin O'Hara's famous framework [sfdc-trigger-framework](https://github.com/kevinohara80/sfdc-trigger-framework)

## Documentation

[Class Documentation](https://dschach.github.io/salesforce-trigger-framework/index.html)
[Class Documentation](https://dschach.github.io/salesforce-trigger-framework/TriggerHandler.html)

## Overview

Expand Down Expand Up @@ -123,7 +140,6 @@ public class OpportunityTriggerHandler extends TriggerHandler {
acc.Name = 'With Trigger';
update acc; /* will invoke the AccountTriggerHandler */
}
}
Expand Down Expand Up @@ -212,7 +228,7 @@ There are two methods that will show additional information.

`TriggerHandler.showLimits()` will debug Apex query and DML limits when the trigger handler has completed.

`TriggerHandler.showDebug()` will show trigger entry and exit, but only during Apex testing. This is to ensure org performance.
`TriggerHandler.showDebug()` will show trigger entry and exit in debug logs. Use sparingly, as it will decrease org performance.

To use one or both of these, add them to the trigger:
```apex
Expand All @@ -224,6 +240,10 @@ AccountTriggerHandler.showDebug(false);
```
or just put them in your Apex code before and after DML statements.

### Universal Action

Version 1.1 adds a new method: `andFinally()` which can contain actions that should happen in every trigger context, after all other actions. Credit to James Simone for this idea. As he writes, "runs after every context - can be used to easily implement something like apex-rollup!" ([The Joys of Apex](https://www.jamessimone.net/blog/joys-of-apex/lightweight-trigger-handler/))

## Overridable Methods

Here are all of the methods that you can override. All of the context possibilities are supported.
Expand All @@ -235,3 +255,4 @@ Here are all of the methods that you can override. All of the context possibilit
- `afterUpdate()`
- `afterDelete()`
- `afterUndelete()`
- `andFinally()`
17 changes: 9 additions & 8 deletions apexdox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ engine:
title: Salesforce Trigger Handler code documentation
subtitle: Documentation for <a target="_blank" rel="noopener noreferrer" href="https://github.com/dschach/salesforce-trigger-framework">Trigger Framework</a>
sortOrder: alpha
cleanDir: false # keep prettier config file
cleanDir: true # keep prettier config file
showTOCSnippets: true
scope:
- public
- private
- protected
- testMethod
homePagePath: "${workspaceFolder}/doc-assets/homePage.html"
#pages:
# - "${workspaceFolder}/documentation/Funny.html"
pages:
- "${workspaceFolder}/doc-assets/files/deploy.html"
assets:
- "${workspaceFolder}/doc-assets/logo.png"
- "${workspaceFolder}/doc-assets/favicon.png"
- "${workspaceFolder}/doc-assets/icon.png"
- "${workspaceFolder}/doc-assets/highlight.css"
- "${workspaceFolder}/doc-assets/styling.css"
- "${workspaceFolder}/doc-assets/index.css"
- "${workspaceFolder}/doc-assets/highlight.js"
- "${workspaceFolder}/doc-assets/icon.png"
- "${workspaceFolder}/doc-assets/index.css"
- "${workspaceFolder}/doc-assets/index.js"
- "${workspaceFolder}/doc-assets/logo.png"
- "${workspaceFolder}/doc-assets/styling.css"
#- "${workspaceFolder}/doc-assets/files/sampletriggerhandlerhome.html"
port: 9090
docblock:
alignItems: true
spacious: true
omitDescriptionTag: true
omitDescriptionTag: false
65 changes: 65 additions & 0 deletions doc-assets/files/deploy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<link href="assets/styling.css" rel="stylesheet" />
<h1 id="installing-the-app-using-a-scratch-org">Installing the app using a Scratch Org</h1>
<ol>
<li>
Set up your environment. Follow the steps in the
<a href="https://trailhead.salesforce.com/content/learn/projects/quick-start-lightning-web-components/">Quick Start: Lightning Web Components</a> Trailhead project. The steps
include:
</li>
</ol>
<ul>
<li>Enable Dev Hub in your org or Trailhead Playground</li>
<li>Install <a target="_blank" href="https://developer.salesforce.com/tools/vscode/en/getting-started/install/#visual-studio-code">Visual Studio Code</a></li>
<li>Install <a target="_blank" href="https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_intro.htm">Salesforce CLI</a></li>
<li>
Install the
<a target="_blank" href="https://developer.salesforce.com/tools/vscode/en/getting-started/install/#salesforce-extensions-for-visual-studio-code"
>Visual Studio Code Salesforce extensions</a
>
</li>
</ul>

<ol>
<li>
<p>If you haven&#39;t already done so, authorize your hub org and provide it with an alias (<strong>myhuborg</strong> in the command below):</p>
<pre><code class="language-plaintext">sfdx auth:web:login --setdefaultdevhubusername --setalias myhuborg
</code></pre>
</li>
<li>
<p>Clone the duplicatehandling repository:</p>
<pre><code class="language-plaintext">git clone https://github.com/dschach/salesforce-trigger-framework
cd salesforce-trigger-framework
</code></pre>
</li>
<li>
<p>Create your scratch org</p>
<ul>
<li>
<p>Using the included script From the VSCode command line, run <a href="scripts/orginit.sh">orginit script</a></p>
<pre><code class="language-plaintext">. scripts/orginit.sh
</code></pre>
</li>
<li>
<p>Using CLI manually</p>
<ol>
<li>
<p>Create a scratch org and provide it with an alias (<strong>triggerHandler</strong> in the command below):</p>
<pre><code class="language-plaintext">sf org create scratch --set-default --definition-file config/project-scratch-def.json --alias triggerHandler
</code></pre>
<p>This will create a new scratch org and install all metadata in this repository</p>
</li>
<li>
<p>Push the app to your scratch org:</p>
<pre><code class="language-plaintext">sf deploy metadata
</code></pre>
</li>
<li>
<p>Open the scratch org:</p>
<pre><code class="language-plaintext">sf org open
</code></pre>
</li>
</ol>
</li>
</ul>
</li>
</ol>
File renamed without changes.

0 comments on commit 0988732

Please sign in to comment.