hardis:project:generate:bypass
Description
Command Behavior
Generates custom permissions and permission sets to bypass specified Salesforce automations (Flows, Triggers, and Validation Rules) for specific sObjects.
This command provides a controlled mechanism to temporarily or permanently disable automations for certain sObjects, which is invaluable for:
- Data Loading: Bypassing validation rules or triggers during large data imports.
- Troubleshooting: Isolating automation issues by temporarily disabling them.
- Development: Allowing developers to work on specific sObjects without triggering complex automations.
Key functionalities:
- sObject Selection: You can specify a comma-separated list of sObjects to bypass (e.g.,
Account,Contact). If omitted, an interactive prompt will allow you to select from available sObjects. - Automation Type Selection: Choose which types of automations to bypass:
Flow,Trigger, orVR(Validation Rules). If omitted, an interactive prompt will guide your selection. - Automatic Bypass Application: Optionally, the command can automatically inject bypass logic into Validation Rules and Triggers. This involves modifying the Apex code for Triggers and the XML for Validation Rules.
- Metadata Source: You can choose to retrieve the metadata elements (Validation Rules, Triggers) from the org (
--metadata-source org) or use local files (--metadata-source local). Retrieving from the org is recommended for accuracy. - Custom Permission and Permission Set Generation: For each selected sObject and automation type, it generates:
- A Custom Permission (e.g.,
BypassAccountFlows) that acts as the bypass switch. - A Permission Set (e.g.,
BypassAccountFlows) that grants the generated Custom Permission.
- A Custom Permission (e.g.,
- Credits Inclusion: By default, generated XML files include a comment indicating they were generated by sfdx-hardis. This can be skipped using
--skip-credits.
Technical explanations
The command's technical implementation involves:
- SOQL Queries (Tooling API): It queries
EntityDefinitionto list customizable sObjects andValidationRuleandApexTriggerto find existing automations. - Interactive Prompts: Uses the
promptslibrary to guide the user through selecting sObjects, automation types, and bypass application options. - XML Generation: Dynamically generates XML content for Custom Permissions and Permission Sets, including descriptions and labels that clearly indicate their purpose.
- File System Operations: Uses
fs-extrato create directories and write the generated Custom Permission and Permission Set XML files. - Metadata Retrieval (for Bypass Application): If
apply-to-vrsorapply-to-triggersis used andmetadata-sourceisorg, it retrieves the relevant Validation Rule or Apex Trigger metadata from the org usingsf project retrieve start. - XML/Apex Code Modification:
- For Validation Rules, it modifies the
errorConditionFormulain the XML to include a check for the bypass Custom Permission. - For Apex Triggers, it injects an
ifstatement at the beginning of the trigger body to check for the bypass Custom Permission.
- For Validation Rules, it modifies the
parseXmlFileandwriteXmlFile: Used for reading and writing XML metadata files.execCommand: Used for executing Salesforce CLI commands, particularly for metadata retrieval.- Error Handling: Includes checks for invalid sObject or automation selections and provides informative error messages.
Parameters
| Name | Type | Description | Default | Required | Options |
|---|---|---|---|---|---|
| apply-to-triggers | boolean | Apply bypass to Triggers | |||
| apply-to-vrs | boolean | Apply bypass to Validation Rules | |||
| automations -a |
option | Comma-separated automations to bypass: Flow, Trigger, VR | |||
| flags-dir | option | undefined | |||
| json | boolean | Format output as json. | |||
| metadata-source -r |
option | Source of metadata elements to apply bypass to. Options: 'org' or 'local'. | |||
| objects -s |
option | Comma-separated list of sObjects to bypass (e.g., Account,Contact,Opportunity). If omitted, you will be prompted to select. | |||
| skip-credits -k |
boolean | Omit the "Generated by" line in the XML files | |||
| skipauth | boolean | Skip authentication check when a default username is required | |||
| target-org -o |
option | undefined | |||
| websocket | option | Websocket host:port for VsCode SFDX Hardis UI integration |
Examples
$ sf hardis:project:generate:bypass
$ sf hardis:project:generate:bypass --sObjects Account,Contact,Opportunity
$ sf hardis:project:generate:bypass --automations Flow,Trigger,VR
$ sf hardis:project:generate:bypass --sObjects Account,Opportunity --automations Flow,Trigger
$ sf hardis:project:generate:bypass --skipCredits
$ sf hardis:project:generate:bypass --apply-to-vrs
$ sf hardis:project:generate:bypass --apply-to-triggers
$ sf hardis:project:generate:bypass --metadata-source org