Skip to content

Instantly share code, notes, and snippets.

@rtmalone
Created September 20, 2023 20:17
Show Gist options
  • Select an option

  • Save rtmalone/7cabea33eac67c27ef430dd7e4c9812f to your computer and use it in GitHub Desktop.

Select an option

Save rtmalone/7cabea33eac67c27ef430dd7e4c9812f to your computer and use it in GitHub Desktop.
Create Apex Class script
#!/bin/bash
# Function to extract sourceApiVersion from sfdx-project.json
get_source_api_version() {
local json_file="$1"
local version=$(jq -r '.sourceApiVersion' "$json_file")
echo "$version"
}
# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <path_to_sfdx-project.json> <apex_class_name>"
exit 1
fi
# Get the path to the sfdx-project.json file and class name from command-line arguments
sfdx_project_file="$1"
class_name="$2"
# Check if the API version is set
api_version=$(get_source_api_version "$sfdx_project_file")
if [ -z "$api_version" ]; then
echo "Error: sourceApiVersion not found in '$sfdx_project_file'."
exit 1
fi
# Create the Apex class with the specified API version
sfdx apex generate class -n "$class_name" --api-version "$api_version" -d force-app/main/default/classes
# Check the result and display an appropriate message
if [ $? -eq 0 ]; then
echo "Apex class '$class_name' created successfully with API version $api_version."
else
echo "Error: Failed to create Apex class."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment