html code

Automate for Success: Elevate Your Salesforce Development with Task.json

How often do you find yourself Googling your preferred Salesforce DX Commands? Have you encountered situations in previous projects where you couldn’t easily recall or access the commands needed for deployment automation?

For quite some time, I’ve relied on Task.json within VSCode to:

  • Keep my frequently used commands readily available.
  • Automate a sequence of commonly employed commands.
  • Reutilize automated deployment scripts.

Consistently, I’ve implemented a standardized Task.json across all my projects, regularly updating it with any new commands that I come across.

In this blog post, I aim to share a portion of my Task.json and highlight ten compelling reasons for using it.

1. Enhanced Development Flow

The Task.json enables you to define and automate everyday development tasks like deploying metadata, conducting tests, generating code coverage reports, creating scratch orgs, and additional functionalities. This optimization streamlines your development process, ultimately saving time.

2. Uniform Development Environment

Using Task.json, you’re able to define tasks that establish uniform development environments across your team. This involves specifying the necessary Salesforce CLI commands, options, and parameters for specific development tasks, ensuring consistency and minimizing configuration discrepancies.

3. Creation and Administration of Scratch Orgs

Task.json allows you to outline tasks dedicated to creating and handling scratch orgs. You can detail the Salesforce CLI commands for creating, deleting, or modifying scratch orgs, facilitating the effortless setup and administration of isolated development environments. In many of my projects, following the creation of a scratch org, I execute a sequence of managed package deployments with just a single command.

4. Automated Deployment of Metadata

With Task.json, automating metadata deployment to Salesforce orgs is possible. You can define tasks tailored to deploying particular metadata components or complete packages, guaranteeing seamless and uniform deployments devoid of manual intervention.

5. Running Tests and Evaluating Code Coverage

Task.json empowers you to create tasks for conducting tests and producing code coverage reports. You can configure tasks to execute unit tests, integration tests, or designated test suites, delivering automated insights into the quality and coverage of your Salesforce code.

6. Validation of Source Code

Task.json allows you to outline tasks for validating your source code according to Salesforce’s best practices and coding standards. By integrating static analysis tools such as PMD, ESLint, or TSLint into these tasks, you can ensure high code quality and compliance with standards.

7. Apex Documentation

Task.json enables the bundling of multiple commands into singular commands, as highlighted in points 4, 5, and 6, in addition to facilitating Java-like document generation. ApexDox VS Code extension.

8. Integration of Continuous Integration and Deployment (CI/CD)

Task.json plays a pivotal role in integrating Salesforce development with CI/CD pipelines. It enables the definition of tasks executing Salesforce CLI commands for deploying code to various environments, enabling automated deployments and continuous integration. It’s important to note that Task.json functions only on systems where VSCode is installed. However, the same scripts can be utilized in platforms like Jenkins, Git pipelines, or Azure DevOps as they essentially operate as shell scripts.

Furthermore, Task.json can define tasks that streamline Git operations within your Salesforce projects. These tasks encompass committing changes, pushing to remote repositories, pulling changes, and resolving merge conflicts, ensuring seamless Git integration within your development workflow.

9. Productivity for Developers

Task.json boosts developer productivity by automating repetitive tasks and establishing a standardized workflow. This automation eradicates manual steps, minimizes errors, and empowers developers to concentrate on coding and constructing Salesforce applications.

10. Integration of Extensions

Task.json seamlessly integrates with multiple extensions within the VSCode ecosystem. Numerous extensions extend their functionality by defining and employing tasks through Task.json. This facilitates the smooth incorporation of tasks with linters, formatters, test runners, and various other development tools.

Example Task.json

In the provided example below, “Create Scratch Org & Install Package” combines multiple commands.

Another challenge to consider is when executing a sequence of commands with a lengthy wait time and one of the final commands requires input. The entire process halts until someone inputs the required parameter.To address this issue, I’ve created a command named “Input For Scratch Org & Package.” This command echoes all inputs, so subsequently, if any command requires the same parameter, I won’t be prompted to provide it again.

How to Run

  • Click Cmd +Shift + P in VSCodeSelect Tasks: Run TaskChoose a list of tasks defined in your task.json and follow the prompt

{
    "version": "2.0.0",
    "tasks": [
        {
            "label" : "Set Default DevHub Org - sf config set",
            "type" : "shell",
            "command" : "sf",
            "args":[
                "config",
                "set",
                "target-dev-hub=${input:devHub}" 
            ]
        }, 
        {
            "label" : "Build new version of Awesome Package",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:package:version:create",
                "-p",
                "Awesome app by JZ", 
                "-d",
                "force-app",
                "-k",
                "${input:packagePwd}",
                "-w",
                "80",
                "-v",
                "${input:devHub}",
                "-f",
                "config/project-scratch-def.json",
                "--releasenotesurl",
                "https://github.com/Jitendra-Zaa/repo/blob/master/README.md",
                "--codecoverage",
                "-e",
                "${input:desc}"
            ]
        },
        {
            "label": "Change default org",
            "type": "shell",
            "command": "sfdx",
            "args": [
                "force:config:set",
                "defaultusername=${input:scratchOrgAlias}"
            ]

        },
        {
            "label" : "Create Scatch Org",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:org:create",
                "--setdefaultusername",
                "-f",
                "config/project-scratch-def.json",
                "--setalias",
                "${input:scratchOrgAlias}",
                "-d",
                "7",
                "-v",
                "${input:devHub}"
            ]
        },
        {
            "label" : "Promote awesome package for release",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:package:version:promote",
                "--package",
                "${input:packageId}" , 
                "-v",
                "${input:devHub}"
            ]
        },
        {
            "label" : "Code Coverage of Aawesome package",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:package:version:report",
                "-p",
                "${input:packageId}" , 
                "--verbose",
                "-v",
                "${input:devHub}"
            ]
        },
        {
            "label" : "Assign permission set",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:user:permset:assign", 
                "--permsetname",
                "admin_permset",
                "--targetusername",
                "${input:scratchOrgAlias}" 
            ]
        },
        {
            "label" : "Install Awesome Package",
            "type" : "shell",
            "command" : "sfdx",
            "args":[
                "force:package:install",
                "-p",
                "${input:packageId}" ,
                "-w",
                "80",
                "-u",
                "${input:scratchOrgAlias}", 
                "-k",
                "${input:packagePwd}",
                "-r",
                "-t",
                "DeprecateOnly",
                "-a",
                "package",
                "-s",
                "AllUsers"
            ]
        } ,
        {
            "label" : "Input For Scratch Org & Package",
            "type" : "shell",
            "command" : "echo - Scratch Org ALias : ${input:scratchOrgAlias}, PackageId: ${input:packageId} , DevHubName : ${input:devHub} , Password - ${input:packagePwd}" 
        },
        {
            "label" : "Create Scratch Org & Install Package",
            "dependsOrder": "sequence",
            "dependsOn": [
                            "Input For Scratch Org & Package", 
                            "Create Scatch Org",
                            "Install Awesome Package"
                        ]
            
        } 
    ],
    "inputs":[
        {
            "id": "desc",
            "type":"promptString",
            "description" : "Descriptoin of Package",
            "default":"Version 1.0 of Package"
        }, 
        {
            "id": "devHub",
            "type":"promptString",
            "description" : "DevHub Name",
            "default":"jit29"
        },
        {
            "id": "packagePwd",
            "type":"promptString",
            "description" : "Password of Awesome Package",
            "default":"packagepwd"
        } ,
        {
            "id": "scratchOrgAlias",
            "type":"promptString",
            "description" : "Where to install package, same alias would be used if scratch org needed",
            "default":"scratch1"
        },
        {
            "id": "packageId",
            "type":"promptString",
            "description" : "Package Id of some cool app",
            "default":"04t5Y00xxxxxxx"
        }
        
    ] 
}