Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. //example of how to move opportunities from one account to another
  2. string accountId = '1MPt0000000CaRgGAK';
  3. list<SObject> updateList = new list<Opportunity>();
  4. for (Opportunity o : [select Id from Opportunity where AccountId =:accountId]){
  5.  
  6. o.AccountId = '1MPt0000000CaRgGAK';
  7. updateList.add(o);
  8. }
  9. update updateList;
  10.  
  11. //example of how to delete all opportunities on certain accounts and then delete the accounts
  12. //(can't delete the Accounts without deleting the Opptys first)
  13. //specifically this was used to find and delete certain accounts with open or won opportunities
  14. //if using in execute anonymous, the code to delete may have to be run multiple times if there is too much data
  15. //if refactored into a button, bulkification is suggested
  16. set<Id> targetAcctIds = new set<Id>();
  17. for(Account ogAcct : [select Id, from Account where Name like '%Canad%' or Name like '%canad%']);{
  18. targetAcctIds.add(ogAcct.Id);
  19. }
  20. list<Opportunity> targetOpptys = [select Id,StageName from Opportunity where AccountId in: targetAcctIds];
  21. system.debug('$$Opportunities size: '+targetOpptys.size());
  22. for (Opportunity o : [select Id,StageName from Opportunity where AccountId in: targetAcctIds]){
  23. o.StageName = 'Lost';
  24. targetOpptys.add(o);
  25. }
  26. delete targetOpptys;
  27. list<Account> targetAccounts = [select Id from Account where Id in:targetAcctIds];
  28. system.debug('$$targetAccounts size: '+ targetAccounts.size());
  29. delete targetAccounts;
  30.  
  31. //example of how to delete all work orders on certain maintenance plans and then delete the maintenance plans (can't delete the MPs without deleting the WOs first)
  32. //specifically this was used to find and delete certain renewed maintenance plans
  33. //if using in execute anonymous, the code to delete the work orders will have to be run multiple times if there is too much data
  34. //if refactored into a button, bulkification is suggested.
  35. set<Id> targetMPIds = new set<Id>();
  36. for(MaintenancePlan ogMP : [select Id,MaintenancePlanNumber, Renewed_Maintenance_Plan__c from MaintenancePlan where MaintenancePlanNumber like '%0002' or MaintenancePlanNumber like '%0003' or MaintenancePlanNumber like '%0004' or MaintenancePlanNumber like '%0005' or MaintenancePlanNumber like '%0006' or MaintenancePlanNumber like '%0007' or MaintenancePlanNumber like '%0008' or MaintenancePlanNumber like '%0009' or MaintenancePlanNumber like '%0010' or MaintenancePlanNumber like '%0011' or MaintenancePlanNumber like '%0012' or MaintenancePlanNumber like '%0013' or MaintenancePlanNumber like '%0014' or MaintenancePlanNumber like '%0015' or MaintenancePlanNumber like '%0016' or MaintenancePlanNumber like '%0017' or MaintenancePlanNumber like '%0018']){
  37. targetMPIds.add(ogMP.Renewed_Maintenance_Plan__c);
  38. }
  39. list<WorkOrder> targetWorkOrders = [select Id,MaintenancePlanId from WorkOrder where MaintenancePlanId in: targetMPIds limit 1000];
  40. system.debug('$$target work orders size: '+targetWorkOrders.size());
  41. delete targetWorkOrders;
  42. list<MaintenancePlan> targetMaintenancePlans = [select Id from MaintenancePlan where Id in:targetMPIds];
  43. system.debug('$$targetMaintenancePlans size: '+targetMaintenancePlans.size());
  44. delete targetMaintenancePlans;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement