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
The new LearnerThe new Learner 

Need to fix the apex trigger , when we update two values

Hi Experts,
 
I have a requirement where I need to delete Manually assigned territories for the accounts.
On Account, we have checkboxes like AccountManager__c, and InsideSales__c when I uncheck these checkboxes I need to delete related Assigned territories. I have written code that is working perfectly for the one checkbox, however, when I included the next condition that is InsideSales__c problem started, even if I uncheck one checkbox it is removing both the territories. can anyone help me out?
public class Accounthandler
{
    public static void manualTerritory(list<account>newAccounts,Map<id,Account>oldAccounts)
    {  
        list<Id>accountIds= new list<Id>();
        list<Id>accountuserterritaryIds=new list<Id>();
        list<Id>territoryIds= New list<Id>();
        list<Id>userterritaryAsication= new list<Id>();
        list<Id>deletedterrirotarIds= new list<Id>();
        
        for(Account acc: newAccounts)
        {
            Account oldAcc=oldAccounts.get(acc.id);
            
            if((acc.AccountManager__c==False && acc.AccountManager__c !=oldAcc.AccountManager__c)
			||(acc.InsideSales__c==False && acc.InsideSales__c !=oldAcc.InsideSales__c))
            {
                accountIds.add(acc.id);
                
            }
            
        }
        if(accountIds.size()>0)
        {
            list<AccountUserTerritory2View>accuserTerrtrView=[select id,RoleInTerritory2,Territory2Id,AccountId from AccountUserTerritory2View where accountid in: accountIds];
            if(accuserTerrtrView.size()>0)
            {
                For(AccountUserTerritory2View accuserview: accuserTerrtrView)
                {
                    if((accuserview.RoleInTerritory2=='Z2' && accuserview.account.AccountManager__c==False && accuserview.account.AccountManager__c !=oldAcc.AccountManager__c)
					||(accuserview.RoleInTerritory2=='Z4' && accuserview.account.InsideSales__c==False && accuserview.account.InsideSales__c !=oldAcc.InsideSales__c))
                    {
                        accountuserterritaryIds.add(accuserview.AccountId);
                        territoryIds.add(accuserview.Territory2Id);
                    }
                }
                
                List<ObjectTerritory2Association> manualTerritoryAssignments = [SELECT Id,ObjectId,Territory2Id,SobjectType  FROM ObjectTerritory2Association WHERE AssociationCause = 'Territory2Manual' AND Territory2Id IN :territoryIds AND ObjectId in:accountuserterritaryIds ];
                
                if(manualTerritoryAssignments.size()>0)
                {
                    delete manualTerritoryAssignments;
                }
                
            }
        }
    } 
}

 
Wendy MillerWendy Miller
https://www.mycenturahealth.us/
In order to pull out the query, you need to collect the Id of the retired GP.
Then you can retrieve the account records that need to be updated and set GeneralPracticioner__c using Trigger.NewMap.

trigger trgnewGP on Contact (before update) {
    List<Id> retiredGpIds = new List<Id>();
    for (Contact gp : Trigger.new) {
        if (gp.GPChange__c && gp.NewGP__c != null) {
            retiredGpIds.add(gp.Id);
        }
    }
    
    if (!retiredGpIds.isEmpty()) {
        List<Account> patients = [SELECT GeneralPracticioner__c from Account WHERE GeneralPracticioner__c IN :retiredGpIds];
        for (Account acc : patients) {
            acc.GeneralPracticioner__c = Trigger.NewMap.get(acc.GeneralPracticioner__c).NewGP__c;
        }
        update patients;
    }
The new LearnerThe new Learner
Wendy Miller: I am not understanding what ur saying, for one field it's working as expected, after adding the second condition and I went to record and updated one checkbox,however, both the territories got deleted.
AnkaiahAnkaiah (Salesforce Developers) 
When you uncheck the InsideSales__c field, what should happen?

Thanks!!
The new LearnerThe new Learner
Thanks for the reply, no, it should work below scenarios, as of now below are getting failed
 
1. when I uncheck AccountManager__c(Territory is Z2) territories that are related to this checkbox need to be deleted. Similarly, If I uncheck InsideSales__c( Territory is Z4) then territories related to inside sales territories need to be deleted.
2. If I uncheck both the fields at a time then both the terrorists need to be deleted