Managing large data sets and handling complex processes efficiently are key skills in Salesforce development. This helps improve performance and ensures a smooth experience for users. Our guide offers easy-to-follow tips if you’re new to Salesforce or looking to improve your skills.

In Lightning Web Components (LWC) development, being able to dynamically style components is really helpful. This means you can change how things look based on what users do, making your website or app more interactive and user-friendly. We’ll focus on how to change and set classes in LWC using JavaScript.

Typically, LWC uses a CSS file with the same name as the component for styling. For example, a ‘paginator’ component would use a paginator.css file. But sometimes, you need more flexible styling.

Remember, our Salesforce experts at Emizentech are always here to help with any questions. Let’s explore dynamic styling in LWC and boost your Salesforce development know-how.

How is it possible?

There are two ways in Salesforce which are used to call the batch class from another batch class are:

  • Using Queueable Apex
  • Using the Finish method of Batch class.

Using this way, you can create the chaining between the batches.

Note: If you call the batch class from the start or execute methods, then Salesforce throws the below error:
System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.

Also Read: How to Implement Salesforce SSO Using OAuth?

batch execute error

While calling batch from the start method there is an error:

execute method of batch

Same error while calling from the execute method of batch:

Here is the complete definition of an error message:complete definition of error message
Also Read: How To Connect Pipedrive and Salesforce Integration?

There Are Two Solutions for This Error:

1. Using Queueable Apex:

Queueable Apex
using Queueable Apex

2. Using the Finish Method of Batch Class:

Batch Class Example:

Finish Method of Batch Class
global class BatchAccountUpdate implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
 String query = 'Select Name from Account WHERE Name != null AND (Name = \'Sapna Company\' OR Name = \'Sapna\') ';
 global Database.QueryLocator start(Database.BatchableContext bc) {
 // collect the batches of records or objects to be passed to execute
 return Database.getQueryLocator(query);
 }
 
 global void execute(Database.BatchableContext bc, List<Account> records){
 // process each batch of records
 for(Account acc : records){
 acc.Name = acc.Name + ' - Updated';
 }
 update records;
 }
 
 global void finish(Database.BatchableContext bc){
 // execute any post-processing operations
 Note: We are calling Batch from Finish Method of Second batch.
 BatchUpdateAccountRelatedContacts b = new BatchUpdateAccountRelatedContacts();
 Database.executeBatch(b, 200);
 }
 }
hire salesforce developers

Calling from Schedule Class:

global class ScheduleBatchAccountUpdate implements Schedulable {
 global void execute(SchedulableContext sc) {
 BatchAccountUpdate b = new BatchAccountUpdate();
 database.executebatch(b);
 }
 }

Second Batch:

global class BatchUpdateAccountRelatedContacts implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
 String query = 'Select FirstName, LastName, Email, Status__c from Contact WHERE
 FirstName != null AND LastName != null AND AccountId != null';
 global Database.QueryLocator start(Database.BatchableContext bc) {
 // collect the batches of records or objects to be passed to execute
 Note: Calling Queueable job for calling the next batchfor chaining.
 System.enqueueJob(new ExecuteBatchQueueableJob());
 return Database.getQueryLocator(query);
 }
 global void execute(Database.BatchableContext bc, List<Contact> records){
 // process each batch of records
 for(Contact con : records){
 con.Status__c = 'Discovery Call Complete';
 }
 update records;
 }
 global void finish(Database.BatchableContext bc){
 // execute any post-processing operations
 }

Queueable Apex:

public class ExecuteBatchQueueableJob implements Queueable {
 public void execute(QueueableContext context) {
 try{
 BatchCalledThroughQueueable b = new BatchCalledThroughQueueable();
 Database.executeBatch(b, 200);
 }catch(Exception e){
 System.debug('@@@ Error Message : '+e.getMessage());
 }
 }
 }

Third Batch:

global class BatchCalledThroughQueueable implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
 String query = 'Select FirstName, LastName, Email, Status, Title from Lead WHERE FirstName != null AND LastName != null';
 global Database.QueryLocator start(Database.BatchableContext bc) {
 // collect the batches of records or objects to be passed to execute
 return Database.getQueryLocator(query);
 }
 global void execute(Database.BatchableContext bc, List<Lead> records){
 // process each batch of records
 for(Lead ld : records){
 ld.Title = ld.FirstName + ' - ' + ld.LastName;
 }
 update records;
 }
 global void finish(Database.BatchableContext bc){
 // execute any post-processing operations
 }
 }

Also Read: How to Create a Salesforce Lightning Map Component?

Conclusion

We hope this guide has provided you with valuable insights and practical steps to enhance your Salesforce development projects. If you have any questions or require more in-depth guidance, don’t hesitate to contact our Salesforce Consultants at Emizentech, one of the Best Salesforce Development Companies. Our team is dedicated to helping you achieve your development goals and ensure your Salesforce solutions are optimized for success. Contact us today for personalized assistance and expert solutions!

Avatar photo
Author

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.

whatsapp