function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vijay ZutshiVijay Zutshi 

After updating an Owner on Territory__c object my trigger is not updating all Accounts with the same owner

My trigger is as follows:-

//Trigger to count sales rep assigned to a single zip code
//Display an error if user attemps to associate another sales
//rep to the same zip code
trigger salesRepCount on Territory__c (before insert, before update, after update) {
     //List<Account> NewAccountlisttoUpdate = new List<Account>();
     //List<Account> AccountsToUpdateOwner = new List<Account>();
     //SET<ID> setAccountId = new SET<ID>();
    //Store all zip codes in a set
    SET<String> territoryZipCodes = new  SET<String>();
    MAP<String, Territory__c> territoryMap = new MAP<String, Territory__c>();
    LIST<Territory__c> zipMatch = [SELECT Id, Name, Territory__c.Owner__c, OwnerId, Zip_Code__c
                                   FROM Territory__c
                                   WHERE Zip_Code__c IN:territoryZipCodes];
    //If a Territory record sales rep is changed
    //check if old and new owners are different
    if(Trigger.isUpdate) {
        if(Trigger.isAfter) {
            List<Account> NewAccountlisttoUpdate = new List<Account>();
            for(Territory__c changedSalesRep : Trigger.new) {
                String oldId = Trigger.oldMap.get(changedSalesRep.Id).OwnerId;
                String newuserId = Trigger.newmap.get(changedSalesRep.Id).OwnerId; 
                if(oldId != newuserId) {  
                     String Territoryzipcode = Trigger.newmap.get(changedSalesRep.Id).Zip_Code__c; 
                     List<Account> AccountsToUpdateOwner = [SELECT ID, OwnerId
                                                               FROM ACCOUNT
                                                               WHERE BillingPostalCode =:Territoryzipcode]; 
                    SET<ID> setAccountId = new SET<ID>();
                    for(Account A : AccountsToUpdateOwner) {
                        if(A.OwnerId != newuserId) {
                            A.OwnerId = newuserId;
                            NewAccountlisttoUpdate.add(A);
                            setAccountId.add(A.Id);
                        }
                    }   
                    update NewAccountlisttoUpdate;
                    
                    //For the Above updated Accounts, check the Contacts and Update with new Owner;
                    LIST<Contact> newContactListToUpdate = [SELECT Id, OwnerId
                                                            FROM Contact
                                                            WHERE AccountId IN :setAccountId];
                    for(Contact updateCon: newContactListToUpdate) {
                        if(updateCon.OwnerId != newuserId) {
                            updateCon.OwnerId = newuserId;
                            newContactListToUpdate.add(updateCon);
                        }
                    }                    
                    update newContactListToUpdate;
                    
                    //For the Above updated Accounts, check the Opportunities and Update with new Owner;
                    LIST<Opportunity> newOpportunityListToUpdate = [SELECT ID, OwnerID 
                                                                    FROM Opportunity
                                                                    WHERE AccountId IN:setAccountId
                                                                    AND StageName != 'Closed Won'];
                    for(Opportunity updateOppty : newOpportunityListToUpdate) {
                        if(updateOppty.OwnerId != newuserId) {
                            updateOppty.OwnerId = newuserId;
                            newOpportunityListToUpdate.add(updateOppty);
                        }
                    }
                    update newOpportunityListToUpdate;
                }
            }
        }
    }
    for(Territory__c repCount : Trigger.new) {
        Integer countSalesRep = [SELECT count() from Territory__c
                                 WHERE Territory__c.OwnerID =:repCount.OwnerId
                                 AND Territory__c.Id !=:repCount.Id
                                 AND Territory__c.Zip_Code__c =:repCount.Zip_Code__c];
        if(countSalesRep>3) {
            repCount.addError('we can not Assign a zipcode to this user Only a single zipcode is assigned to only three Owners(Sales Representatives)'+countSalesrep); 
        }
    }
}

So when I update an owner in the Territory__c object it should also update all Accounts with the same owner. The trigger is not oing that. Rest of my trigger for other things si working. So please help.

Thanks
Vijay
Azeem Khan 15Azeem Khan 15
Vijay, 

Please change the if condition like below and try to execute.I believe order of if condition is incorrect. 


 if(Trigger.isAfter){
      if(Trigger.isUpdate) {

OR 

if(Tringger.isAfter && Trigger.isUpdate)
{
     Logic
}


Thanks,
Azeem Khan



 
Vijay ZutshiVijay Zutshi
Hi Azeem I made the change suggested by you but still it is not updating the Account with the changed owner. Please advise. Thanks Vijay