Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class BatchCalcFirstInvoice implements Database.Batchable<sObject>,Schedulable {
- String query = ' SELECT id,(SELECT Date_Invoice__c FROm Invoices__r WHERE Date_iNVOICE__C!=null ORDER BY Date_Invoice__c ASC LIMIT 1),First_Invoice__c FROM Account';
- global BatchCalcFirstInvoice() {
- }
- global Database.QueryLocator start(Database.BatchableContext BC) {
- return Database.getQueryLocator(query);
- }
- global void execute(Database.BatchableContext BC, List<sObject> scope) {
- List<Account> toUpdate = New List <Account> ();
- for(Account A: (List<Account>) scope) {
- if(!A.Invoices__r.isEmpty()){
- A.First_Invoice__c = A.Invoices__r[0].Date_Invoice__c;
- toUpdate.add(A);
- }
- if(A.Invoices__r.isEmpty()){
- A.First_Invoice__c=null;
- toUpdate.add(A);
- }
- }
- update toUpdate;
- }
- global void finish(Database.BatchableContext BC) {
- }
- public void execute(SchedulableContext context) {
- Database.executeBatch(this);
- }
- }
- string conServiceProvided = '%CURRENCY ACCOUNTS%';
- String query = 'SELECT Id,(SELECT id, Contract_Service_Provided__c, Date_Invoice__c From Invoices__r WHERE Contract_Service_Provided__c != null and (Not Contract_Service_Provided__c LIKE : conServiceProvided) ORDER BY Date_Invoice__c ASC LIMIT 1) FROM Account';
- global BatchFirstInvoiceasProductClient() {
- }
- global Database.QueryLocator start(Database.BatchableContext BC) {
- return Database.getQueryLocator(query);
- }
- global void execute(Database.BatchableContext BC, List<sObject> scope) {
- List<Account> toUpdate = New List <Account> ();
- for(Account A: (List<Account>) scope) {
- if(!A.Invoices__r.isEmpty()){
- A.First_Invoice_as_Product_Client__c = A.Invoices__r[0].Date_Invoice__c;
- toUpdate.add(A);
- }
- }
- update toUpdate;
- }
- global void finish(Database.BatchableContext bc){
- }
- global void execute(SchedulableContext sc) {
- BatchFirstInvoiceasProductClient batchable = new BatchFirstInvoiceasProductClient();
- Database.executeBatch(batchable, 50);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement