Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. List<Account> accounts = [SELECT Id, Account_Number__c FROM Account ORDER BY CreatedDate ASC];
  2. List<Account> accountsUpdate = new List<Account>();
  3. Integer HOW_MANY_OBJECTS_AT_ONCE = 1100;
  4. Integer counter, endCounter;
  5. String templateOfField = '';
  6. Integer indexOfNOTFilledAccountNumberField = 0;
  7. for (Account account : accounts) {
  8. if (String.isBlank(account.Account_Number__c)) {
  9. indexOfNOTFilledAccountNumberField = accounts.indexOf(account);
  10. break;
  11. }
  12. }
  13. counter = indexOfNOTFilledAccountNumberField;
  14. endCounter = indexOfNOTFilledAccountNumberField + HOW_MANY_OBJECTS_AT_ONCE - 1;
  15.  
  16. for (; counter < accounts.size(); counter++) {
  17. templateOfField = 'A-';
  18.  
  19. if(counter < 1000) {
  20. if (counter < 10) {
  21. templateOfField += '000';
  22. } else if (counter < 100) {
  23. templateOfField += '00';
  24. } else {
  25. templateOfField += '0';
  26. }
  27. }
  28.  
  29. if (String.isBlank(accounts.get(counter).Account_Number__c)) {
  30. accounts.get(counter).Account_Number__c = templateOfField + String.valueOf(counter);
  31. accountsUpdate.add(accounts.get(counter));
  32. }
  33.  
  34. if (accountsUpdate.size() == endCounter) {
  35. break;
  36. }
  37. }
  38.  
  39. System.debug('accounts.size() = ' + accounts.size());
  40.  
  41. if (accountsUpdate.size() > 0) {
  42. System.debug('indexOfNOTFilledAccountNumberField = ' + indexOfNOTFilledAccountNumberField);
  43. System.debug('accountsUpdate.size() = ' + accountsUpdate.size());
  44. update accountsUpdate;
  45. } else {
  46. System.debug('*****UPDATE FINISHED*****');
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement