function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandesh Vishwakarma 9Sandesh Vishwakarma 9 

Hello everyone , please help me out with a scenario here in apex trigger and batch apex.

Scenario : Anytime an account is created or updated a BATCH class should be called where a HTTP POST callout will initiate and it will send the all field values of that account record to that external webApplication.

Here is what I've tried
Trigger :

trigger AccountTrigger on Account (after insert, after update) {
    // Create a list to hold the accounts that have been inserted or updated
    List<Account> accounts = new List<Account>();
    
    // Check if the trigger was fired due to an insert
    if (Trigger.isInsert) {
        // Add all of the newly inserted accounts to the list
        accounts.addAll(Trigger.new);
    }
    
    // Check if the trigger was fired due to an update
    if (Trigger.isUpdate) {
        // Add all of the updated accounts to the list
        accounts.addAll(Trigger.new);
    }
    
    // Check if there are any accounts in the list
    if (!accounts.isEmpty()) {
        // Call the batch class, passing the list of accounts as a parameter
        BatchClass.sendAccountsToExternalWebsite(accounts);
    }
}


--------------------------------------------------------------------------------------------
Please someone help me out in writing batch apex class for this.
Thank you
Prateek Prasoon 25Prateek Prasoon 25
The batch class can be triggered by using a trigger on the Account object that is fired whenever a new account record is created or an existing account record is updated. The trigger can be written in Apex and should call the batch class to initiate the process.
Here's a sample code for the trigger:
trigger can be written in Apex and should call the batch class to initiate the process.
Here's a sample code for the trigger:

trigger AccountTrigger on Account (after insert, after update) { if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) { List<Id> accountIds = new List<Id>(); for (Account acc : Trigger.new) { accountIds.add(acc.Id); } if (!accountIds.isEmpty()) { AccountBatch batch = new AccountBatch('SELECT Id, Name, BillingCity, ... FROM Account WHERE Id IN :accountIds', 'https://yourwebapplication.com/api/account'); Database.executeBatch(batch, 200); } } }
You can write a batch class in Apex, which is the programming language used to develop custom solutions on the Salesforce platform. The batch class can be triggered whenever an account record is created or updated. In the batch class, you can make a HTTP POST callout to the external web application, passing all the field values of the account as parameters in the request body.
sample code that illustrates this process

global class AccountBatch implements Database.Batchable<sObject>, Database.Stateful,Database.AllowsCallouts{ global final String query; global final String webServiceURL; global AccountBatch(String q, String url) { query = q; webServiceURL = url; } global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> scope) { List<Account> accountsToUpdate = new List<Account>(); for (Account acc : scope) { try { // Create a JSON object with the account field values String requestBody = '{"Name":"' + acc.Name + '", "BillingCity":"' + acc.BillingCity + '", ...}'; // Make a HTTP POST callout to the web service Http http = new Http(); HttpRequest request = new HttpRequest(); request.setMethod('POST'); request.setEndpoint(webServiceURL); request.setHeader('Content-Type', 'application/json'); request.setBody(requestBody); HttpResponse response = http.send(request); // Check the response status and update the account record accordingly if (response.getStatusCode() == 200) { acc.Callout_Status__c = 'Success'; } else { acc.Callout_Status__c = 'Failed'; } accountsToUpdate.add(acc); } catch (Exception ex) { acc.Callout_Status__c = 'Error: ' + ex.getMessage(); accountsToUpdate.add(acc); } } // Update the account records with the callout status if (!accountsToUpdate.isEmpty()) { update accountsToUpdate; } } global void finish(Database.BatchableContext BC) { } }
 if you find my answer helpful mark it as the best answer.