Monday, 9 October 2023

Passing Parameter Id to Batch Class

Hello everyone, 

In this post I want to show you, how to pass parameters to batch apex class. This is very useful when you want to test or execute batch on parameter based records. 

Passing parameter to batch apex using constructor

Here is the example that helps you to pass parameter in batch apex.

Using Class Constructor


global class TestBatch implements Database.batchable<sObject>, Database.Stateful<Sobject>{
    global String query;
    
     
    global TestBatch(){ query = 'SELECT ID, Name FROM Account where Name!= null'; } global TestBatch(Id recordId){ query = 'SELECT ID, Name FROM Account where Name != null and id =\''+recordId+'\''; }
     //Method to get the data to be proceesed 
    global database.Querylocator Start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
    }
 
 
    //Method to execute the batch
    global void execute(Database.BatchableContext bc, Sobject[] scope){
        // Execute method logic
    }
 
    //Method to be called after the excute
    global void finish(Database.BatchableContext bc){
        // Finish method logic
    }
}


To Execute:
To Run One Record:
TestBatch objcls=new TestBatch('--Acc Id--');
database.executebatch(objcls,1);


To Run normal batch/all records:
TestBatch objcls=new TestBatch('--Acc Id--');
database.executebatch(objcls,1);


Tuesday, 1 December 2020

 

How to setup Visual Studio Code for Salesforce


Step1: 

Install the Salesforce CLI https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm

While installing check box should be true for set path, its automatically set the path.

Open comand promt type: sfdx 

sfdx update - it updates the version If any latest


Step 2:

Install Visual Studio Code 

Open vs code: Click on extensions icon on left side : search Salesforce Extension Pack and install


Step 3:

Create a project in VS Code

cntl+shift+p-> Create a project with manifest

Again select: Create a project with manifest -> Standard --> Project name

New project is created,


Step 4:

Connect with Salesforce

In vs code command palette : Authorize an Org

Select Production/Sandbox

Give alias name and enter -> It takes you to salesforce login page

login - allow access

In VS code - open package.xml under your project-manifest 

Right click on package.xml -> click on Retrive source in Manifest from Org

Add components to package.xml whatever compoenents you need like objects....


Step 5:

Deploy changes to source org

There are two ways we can do

1) Right click on Code that you have worked -> click on Deploy this source to org

2) Deploy on Save -> Open command palette --> Open User settings --> Search of Deploy on Save --> Check the box Push or Deploy on Save (fsdeploy package)


Step 6:

Create apex class/components/triggers

In vs code command palette : create apex class/components/triggers.......