You need to sign in to do that
Don't have an account?
Synthia Beauvais
Batch Apex Trigger
How can I update the fields below on the Account object? I am not familiar with writing a Batch Trigger. Please see code below.
Number_of_Contacts__c
Number_of_Active_Contacts__c
Thanks in advnce!
Number_of_Contacts__c
Number_of_Active_Contacts__c
trigger NumberOfContacts on Account (before insert, before update) { if(trigger.isinsert) for(account a:trigger.new) a.Number_of_contacts__c = 0; else for(account a:[select id,(select id from contacts) from account where id in :trigger.new]) trigger.newmap.get(a.id).Number_of_contacts__c = a.contacts.size(); for(account b:[select id,(select id from contacts where Inactive__c = False) from account where id in :trigger.new]) trigger.newmap.get(b.id).Number_of_active_contacts__c = b.contacts.size(); }
Thanks in advnce!
You are mixing things up, Apex Trigger and Batch Apex are 2 different things.
When to use Batch Apex : As you might know that salesforce due to its multitenant architecture imposes certain governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records that are within Governor limits.
But sometimes, it is essential for an organisation to manage thousands of records every day. Insert/Update/Delete records when needed.
Therefore to handle such scenarios where we need to work with large amount of data Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
To implement Batch Apex, we have to create an global apex class which implements Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs.
The Database.Batchable interface contains three methods that must be implemented.
For more info on Batch Apex refer to this link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
Here is a Batch Apex code to achieve your desired functionality:
Now to execute this class Go to Developer Console --> Open Anonymous Window by pressing 'Ctrl + E' and paste the following code there:
and then Click on Execute button.
Or if you want this Batch class to run at regular intervals, you can schedule an Apex class to run at regular intervals, first write an Apex class that implements the Salesforce-provided interface Schedulable.
Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
This will schedule your batch class to execute daily at 11 am.
For more info on how to Schedule Batch apex refer to following link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
To monitor or stop the execution of the batch Apex job, from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.
Once the Status of batch is completed, you can test the records and verify it.
Hope this helps!
Please mark this question as Solved so that others can view it as a proper solution.
Thanks,
Apoorv
All Answers
Number_of_Contacts__c
Number_of_Active_Contacts__c
Your code resembles my code.
Thanks,
Synthia
You are mixing things up, Apex Trigger and Batch Apex are 2 different things.
When to use Batch Apex : As you might know that salesforce due to its multitenant architecture imposes certain governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records that are within Governor limits.
But sometimes, it is essential for an organisation to manage thousands of records every day. Insert/Update/Delete records when needed.
Therefore to handle such scenarios where we need to work with large amount of data Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
To implement Batch Apex, we have to create an global apex class which implements Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs.
The Database.Batchable interface contains three methods that must be implemented.
For more info on Batch Apex refer to this link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
Here is a Batch Apex code to achieve your desired functionality:
Now to execute this class Go to Developer Console --> Open Anonymous Window by pressing 'Ctrl + E' and paste the following code there:
and then Click on Execute button.
Or if you want this Batch class to run at regular intervals, you can schedule an Apex class to run at regular intervals, first write an Apex class that implements the Salesforce-provided interface Schedulable.
Now go to Developer Console and in Anonymous window(once in Developer console Press Ctrl + E) execute the following code:
This will schedule your batch class to execute daily at 11 am.
For more info on how to Schedule Batch apex refer to following link : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
To monitor or stop the execution of the batch Apex job, from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.
Once the Status of batch is completed, you can test the records and verify it.
Hope this helps!
Please mark this question as Solved so that others can view it as a proper solution.
Thanks,
Apoorv