Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. global class BatchCalcFirstInvoice implements Database.Batchable<sObject>,Schedulable {
  2.  
  3.  
  4.  
  5. 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';
  6.  
  7.  
  8. global BatchCalcFirstInvoice() {
  9.  
  10. }
  11.  
  12. global Database.QueryLocator start(Database.BatchableContext BC) {
  13. return Database.getQueryLocator(query);
  14. }
  15.  
  16. global void execute(Database.BatchableContext BC, List<sObject> scope) {
  17. List<Account> toUpdate = New List <Account> ();
  18. for(Account A: (List<Account>) scope) {
  19. if(!A.Invoices__r.isEmpty()){
  20. A.First_Invoice__c = A.Invoices__r[0].Date_Invoice__c;
  21. toUpdate.add(A);
  22. }
  23. if(A.Invoices__r.isEmpty()){
  24. A.First_Invoice__c=null;
  25. toUpdate.add(A);
  26. }
  27. }
  28. update toUpdate;
  29. }
  30.  
  31. global void finish(Database.BatchableContext BC) {
  32.  
  33. }
  34. public void execute(SchedulableContext context) {
  35. Database.executeBatch(this);
  36. }
  37.  
  38. }
  39.  
  40. string conServiceProvided = '%CURRENCY ACCOUNTS%';
  41.  
  42. 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';
  43.  
  44. global BatchFirstInvoiceasProductClient() {
  45.  
  46. }
  47.  
  48. global Database.QueryLocator start(Database.BatchableContext BC) {
  49. return Database.getQueryLocator(query);
  50. }
  51.  
  52. global void execute(Database.BatchableContext BC, List<sObject> scope) {
  53. List<Account> toUpdate = New List <Account> ();
  54. for(Account A: (List<Account>) scope) {
  55. if(!A.Invoices__r.isEmpty()){
  56. A.First_Invoice_as_Product_Client__c = A.Invoices__r[0].Date_Invoice__c;
  57. toUpdate.add(A);
  58. }
  59. }
  60. update toUpdate;
  61. }
  62. global void finish(Database.BatchableContext bc){
  63.  
  64. }
  65.  
  66. global void execute(SchedulableContext sc) {
  67. BatchFirstInvoiceasProductClient batchable = new BatchFirstInvoiceasProductClient();
  68. Database.executeBatch(batchable, 50);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement