Guest User

Untitled

a guest
Oct 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. List<Sales_Account__c> salist = new list<Sales_Account__c>([Select Seller_Profile__c, Manager_Profile__c, Director_Profile__c,
  2. Executive_Profile__c, Director_is_Active__c, Seller_is_Active__c,
  3. Manager_is_Active__c, Executive_is_Active__c, Sold_To_Account__c,
  4. Sold_To_GrandParent__c,
  5. Sold_To_Parent__c, Manager__c, Director__c, Executive__c, Seller__c
  6. FROM Sales_Account__c LIMIT 1000]);
  7. for (Sales_Account__c sa : salist) {
  8. AccountIds.add(sa.Sold_To_Account__c);
  9. ParentAccountIds.add(sa.Sold_To_Parent__c);//parent
  10. GrandParentAccountIds.add(sa.Sold_To_GrandParent__c);//Grandparent
  11. UserIds.add(sa.Manager__c);
  12. UserIds.add(sa.Director__c);
  13. UserIds.add(sa.Executive__c);
  14. }
  15.  
  16. /* Map keyed on account Id + user id */
  17. Map<String, AccountTeamMember> accountTeamMap = new Map<String, AccountTeamMember>();
  18. for (AccountTeamMember atm : [SELECT Id, AccountId, UserId FROM AccountTeamMember
  19. WHERE User.IsActive = True AND AccountId in :AccountIds LIMIT 1]) {
  20. accountTeamMap.put((string)atm.AccountId + (string)atm.UserId, atm);
  21. system.debug('ATM'+accountTeamMap);
  22. }
  23.  
  24. for (Sales_Account__c sa : records) {
  25.  
  26. if (sa.Sold_To_Account__c !=null && sa.Seller__c !=null && sa.Seller_is_Active__c == 'Active' &&
  27. (sa.Seller_Profile__c == 'LYB Sales' || sa.Seller_Profile__c == 'LYB Business Admins' || sa.Seller_Profile__c == 'LYB Executive')){
  28. /* Check for existence of user in map. If not found, add */
  29. AccountTeamMember atm = accountTeamMap.get((string)sa.Sold_To_Account__c + (string)sa.Seller__c);
  30. AccountShare share = accountShareMap.get((string)sa.Sold_To_Account__c + (string)sa.Seller__c);
  31. if (atm == null && sa.Seller_is_Active__c == 'Active') {
  32. atm = new AccountTeamMember(AccountId = sa.Sold_To_Account__c, //Soldto
  33. UserId = sa.Seller__c,
  34. TeamMemberRole = 'Sales Rep');
  35. accountTeamMembersToAdd.add(atm);
  36. }
  37. if (share == null && sa.Seller_is_Active__c == 'Active'){
  38. share = new AccountShare(AccountId = sa.Sold_To_Account__c,
  39. UserOrGroupId = sa.Seller__c,
  40. AccountAccessLevel = 'Edit',
  41. OpportunityAccessLevel = 'None',
  42. CaseAccessLevel = 'None');
  43. accountShareToAdd.add(share);
  44. }
  45.  
  46. if (isUpdate) {
  47. if (sa.seller__c != oldmap.get(sa.Id).Seller__c && sa.Seller_is_Active__c == 'Active'){
  48. Sales_Account__c oldSa = oldMap.get(sa.Id);
  49. if (oldSa.Seller__c != null && oldsa.Seller_is_Active__c == 'Active') {
  50. atm = accountTeamMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Seller__c);/*comparing with MAP*/
  51. if (atm != null && sa.Seller_is_Active__c == 'Active') {
  52. system.debug('ATM:::::::::::::'+atm);
  53. accountTeamMembersToDelete.add(atm);/*Add the sellers to Delete ATM list*/ }
  54. share = accountShareMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Seller__c);
  55. if (share != null && sa.Seller_is_Active__c == 'Active') {
  56. accountShareToDelete.add(share);
  57. }}}}}
  58. if (isUpdate) {
  59.  
  60. if (sa.Executive__c != oldmap.get(sa.Id).Executive__c && sa.Executive_is_Active__c == 'Active'){
  61. /*old map seller and new seller */
  62. Sales_Account__c oldSa = oldMap.get(sa.Id);
  63.  
  64. if (oldSa.Executive__c != null && oldsa.Executive_is_Active__c == 'Active') {
  65. atmExecutivePAR = accountTeamMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Director__c);/*comparing with MAP*/
  66.  
  67. if (atmExecutivePAR != null && sa.Executive_is_Active__c == 'Active') {
  68. accountTeamMembersToDelete.add(atmExecutivePAR);/*Add the sellers to Delete ATM list*/
  69. }
  70. shareExecutivePAR = accountShareMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Director__c);
  71.  
  72. if (shareExecutivePAR!= null && sa.Executive_is_Active__c == 'Active') {
  73. accountShareToDelete.add(shareExecutivePAR);}}}
  74. /* send adds and deletes to db */
  75. if (accountTeamMembersToAdd.size() > 0){ //upsert to AccountShare must be done before Account Team
  76. accountShareToaddList.addall(accountShareToAdd); /*Adding the SET to a LIST to perform DML operations*/
  77. system.debug('accountShareToAddList'+accountShareToAddList);
  78. upsert accountShareToAddList;
  79. accountTeamMembersToAddList.addall(accountTeamMembersToAdd);
  80.  
  81. try {
  82. upsert accountTeamMembersToAddList;
  83.  
  84. system.debug('accountTeamMembersToAddList'+accountTeamMembersToAddList);
  85.  
  86. }
  87. catch(DMLException ex) {
  88. if(ex.getMessage().contains('error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []')) {
  89. upsert accountTeamMembersToAddList;
  90.  
  91. }
  92. }
  93. }
  94.  
  95. If (accountTeamMembersToDeleteList.size() > 0){
  96.  
  97. delete accountShareToDeleteList;/*creating a delete list for ATM*/
  98. delete accountTeamMembersToDeleteList;
  99. }
Add Comment
Please, Sign In to add comment