// Script to populate Organization members into dealer territories: // We need to add OrgIds in the below set in below manner to add users of those dealer Accounts to their dealer territories. // Run the below query to get the OrgIds of territories to get the orgids of Accounts in Organization Membership records eligible for dealer territories // select count(id),Account__r.OrganizationId__c from Organization_Membership__c where Account__r.Sub_Type__c = 'DOMESTIC DEALER' AND Account__r.Status__c = 'ACTIVATED' AND Account__r.OrganizationId__c != null group by Account__r.OrganizationId__c order by count(id) desc List sOrgIds = new List(); for(Account acct: [SELECT Id, OrganizationId__c, createddate from account where recordtype.name = 'Bobcat Dealer Account' and organization_type__c='DEALER' and sub_type__c='DOMESTIC DEALER' and Status__c='ACTIVATED' and createddate = LAST_WEEK]){ sOrgIds.add(acct.OrganizationId__c); } List lOrgMemberships = new List([Select Account__c,Contact__c,Account__r.OrganizationId__c from Organization_Membership__c where Account__r.isPartner = true AND Account__r.Sub_Type__c = 'DOMESTIC DEALER' AND Account__r.Status__c = 'ACTIVATED' AND Account__r.OrganizationId__c != null AND Account__r.OrganizationId__c IN: sOrgIds]); Map mContactIdToUserId = new Map(); Map> mOrgIdToTerritory = new Map>(); Set sContactId = new Set(); List lAssociationsToInsert = new List(); Set sExistingAssociations = new Set(); for(Organization_Membership__c oMembership: lOrgMemberships){ if(oMembership.Contact__c != null){ sContactId.add(oMembership.Contact__c); } } for(User oUser : [Select Id,contactId from User where contactId IN: sContactId AND isActive = true AND Profile.Name = 'Dealer User']){ //should we add another condition Profile.Name = 'Dealer User' mContactIdToUserId.put(oUser.contactId , oUser.Id); } for(Territory2 oTerritory: [Select Id,Org_Id__c from Territory2 where Org_Id__c IN: sOrgIds]){ if(!mOrgIdToTerritory.containsKey(oTerritory.Org_Id__c)){ mOrgIdToTerritory.put(oTerritory.Org_Id__c,new Set()); } mOrgIdToTerritory.get(oTerritory.Org_Id__c).add(oTerritory.Id); } for(UserTerritory2Association oAssociation: [select UserId,Territory2Id from UserTerritory2Association where Territory2.Org_Id__c IN: sOrgIds]){ sExistingAssociations.add(String.valueOf(oAssociation.userId) + '|' + oAssociation.Territory2Id); } for(Organization_Membership__c oMembership: lOrgMemberships){ if(mContactIdToUserId.containsKey(oMembership.Contact__c) && !mOrgIdToTerritory.keySet().isEmpty() && mOrgIdToTerritory.containsKey(oMembership.Account__r.OrganizationId__c)){ for(Id oTerritoryId: mOrgIdToTerritory.get(oMembership.Account__r.OrganizationId__c)){ if(!sExistingAssociations.contains(String.valueOf(mContactIdToUserId.get(oMembership.Contact__c)) + '|' + String.valueOf(oTerritoryId))) { UserTerritory2Association oAssociation = new UserTerritory2Association(); oAssociation.UserId = mContactIdToUserId.get(oMembership.Contact__c); oAssociation.Territory2Id = oTerritoryId; lAssociationsToInsert.add(oAssociation); } } } } System.debug('******************* lAssociationsToInsert size ' + lAssociationsToInsert.size()); if(lAssociationsToInsert.size()>0){ System.debug('******************* lAssociationsToInsert size ' + lAssociationsToInsert.size()); System.debug('******************* lAssociationsToInsert ' + lAssociationsToInsert); insert lAssociationsToInsert; }