Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<Sales_Account__c> salist = new list<Sales_Account__c>([Select Seller_Profile__c, Manager_Profile__c, Director_Profile__c,
- Executive_Profile__c, Director_is_Active__c, Seller_is_Active__c,
- Manager_is_Active__c, Executive_is_Active__c, Sold_To_Account__c,
- Sold_To_GrandParent__c,
- Sold_To_Parent__c, Manager__c, Director__c, Executive__c, Seller__c
- FROM Sales_Account__c LIMIT 1000]);
- for (Sales_Account__c sa : salist) {
- AccountIds.add(sa.Sold_To_Account__c);
- ParentAccountIds.add(sa.Sold_To_Parent__c);//parent
- GrandParentAccountIds.add(sa.Sold_To_GrandParent__c);//Grandparent
- UserIds.add(sa.Manager__c);
- UserIds.add(sa.Director__c);
- UserIds.add(sa.Executive__c);
- }
- /* Map keyed on account Id + user id */
- Map<String, AccountTeamMember> accountTeamMap = new Map<String, AccountTeamMember>();
- for (AccountTeamMember atm : [SELECT Id, AccountId, UserId FROM AccountTeamMember
- WHERE User.IsActive = True AND AccountId in :AccountIds LIMIT 1]) {
- accountTeamMap.put((string)atm.AccountId + (string)atm.UserId, atm);
- system.debug('ATM'+accountTeamMap);
- }
- for (Sales_Account__c sa : records) {
- if (sa.Sold_To_Account__c !=null && sa.Seller__c !=null && sa.Seller_is_Active__c == 'Active' &&
- (sa.Seller_Profile__c == 'LYB Sales' || sa.Seller_Profile__c == 'LYB Business Admins' || sa.Seller_Profile__c == 'LYB Executive')){
- /* Check for existence of user in map. If not found, add */
- AccountTeamMember atm = accountTeamMap.get((string)sa.Sold_To_Account__c + (string)sa.Seller__c);
- AccountShare share = accountShareMap.get((string)sa.Sold_To_Account__c + (string)sa.Seller__c);
- if (atm == null && sa.Seller_is_Active__c == 'Active') {
- atm = new AccountTeamMember(AccountId = sa.Sold_To_Account__c, //Soldto
- UserId = sa.Seller__c,
- TeamMemberRole = 'Sales Rep');
- accountTeamMembersToAdd.add(atm);
- }
- if (share == null && sa.Seller_is_Active__c == 'Active'){
- share = new AccountShare(AccountId = sa.Sold_To_Account__c,
- UserOrGroupId = sa.Seller__c,
- AccountAccessLevel = 'Edit',
- OpportunityAccessLevel = 'None',
- CaseAccessLevel = 'None');
- accountShareToAdd.add(share);
- }
- if (isUpdate) {
- if (sa.seller__c != oldmap.get(sa.Id).Seller__c && sa.Seller_is_Active__c == 'Active'){
- Sales_Account__c oldSa = oldMap.get(sa.Id);
- if (oldSa.Seller__c != null && oldsa.Seller_is_Active__c == 'Active') {
- atm = accountTeamMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Seller__c);/*comparing with MAP*/
- if (atm != null && sa.Seller_is_Active__c == 'Active') {
- system.debug('ATM:::::::::::::'+atm);
- accountTeamMembersToDelete.add(atm);/*Add the sellers to Delete ATM list*/ }
- share = accountShareMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Seller__c);
- if (share != null && sa.Seller_is_Active__c == 'Active') {
- accountShareToDelete.add(share);
- }}}}}
- if (isUpdate) {
- if (sa.Executive__c != oldmap.get(sa.Id).Executive__c && sa.Executive_is_Active__c == 'Active'){
- /*old map seller and new seller */
- Sales_Account__c oldSa = oldMap.get(sa.Id);
- if (oldSa.Executive__c != null && oldsa.Executive_is_Active__c == 'Active') {
- atmExecutivePAR = accountTeamMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Director__c);/*comparing with MAP*/
- if (atmExecutivePAR != null && sa.Executive_is_Active__c == 'Active') {
- accountTeamMembersToDelete.add(atmExecutivePAR);/*Add the sellers to Delete ATM list*/
- }
- shareExecutivePAR = accountShareMap.get((string)oldSa.Sold_To_Account__c + (string)oldSa.Director__c);
- if (shareExecutivePAR!= null && sa.Executive_is_Active__c == 'Active') {
- accountShareToDelete.add(shareExecutivePAR);}}}
- /* send adds and deletes to db */
- if (accountTeamMembersToAdd.size() > 0){ //upsert to AccountShare must be done before Account Team
- accountShareToaddList.addall(accountShareToAdd); /*Adding the SET to a LIST to perform DML operations*/
- system.debug('accountShareToAddList'+accountShareToAddList);
- upsert accountShareToAddList;
- accountTeamMembersToAddList.addall(accountTeamMembersToAdd);
- try {
- upsert accountTeamMembersToAddList;
- system.debug('accountTeamMembersToAddList'+accountTeamMembersToAddList);
- }
- catch(DMLException ex) {
- if(ex.getMessage().contains('error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []')) {
- upsert accountTeamMembersToAddList;
- }
- }
- }
- If (accountTeamMembersToDeleteList.size() > 0){
- delete accountShareToDeleteList;/*creating a delete list for ATM*/
- delete accountTeamMembersToDeleteList;
- }
Add Comment
Please, Sign In to add comment