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
KiransssssssssssssssssssKiransssssssssssssssssss 

Before Trigger is not updating record

I want to capture the Territory Ids the user is assigned to. I wrote the trigger and the logs show the value that i am expecting, but the record is not getting updated. Could you suggest me please?

trigger preventTerritoryAssignmentDeletion on User (before update) {
    
    Set<Id> userIds = new Set<Id>();
    List<User> UT2List = new List<User>();
   
    for(User u : Trigger.New){
        if(u.Resource_ID__c == 'testresourceid'){
            //u.Territory_Backup_IDs__c = 'workinig';
            userIds.add(u.Id);
        }
    }
    
        UT2List = [SELECT ID,Territory_Backup_IDs__c, (SELECT ID,Territory2Id FROM UserTerritory2Associations) FROM User WHERE Id IN :userIds];
		
        for(User u : UT2List){
            string terrString =',';
            for(UserTerritory2Association ut2a : u.UserTerritory2Associations){
                terrString = terrString + ut2a.Territory2Id +',';
                system.debug('terrString inside inner for loop'+terrString);
               
            }
           
            system.debug('terrString in out for loop'+terrString);
            system.debug('u.Territory_Backup_IDs__c'+ u.Territory_Backup_IDs__c);
            u.Territory_Backup_IDs__c =u.Territory_Backup_IDs__c+ terrString;
            system.debug('u.Territory_Backup_IDs__c'+ u.Territory_Backup_IDs__c);
            }
          
    
}
Thank you in advance!
Ramprasad VarriRamprasad Varri
This looks fundamentally wrong. Territory assignment is to be managed by Salesforce.  You set up Territories and Territory assignment rules like say an account zip code is 70167 then account gets associated with Territory  X.

To each Territory you assign users or groups.  So if you want to remove a user from a Territory you do it through configuration or trigger. But removal will be taken care either by Salesforce if user is deactivated or manually removed from the Territory. So what is the use case you are thinking of that Territory is being  lost?
KiransssssssssssssssssssKiransssssssssssssssssss
Hi Ramprasad, Thank you for responding. When a user is deactivated, the territory assignments records (UserTerritory2Association) are deleted. So before those are deleted, I want to capture the ID of the Territories. I want to use it in some way later. So, considering that I just need the territoryIDs in the backup field that I created, please help me understand, technically what went wrong here. I am running the code in Anonymous and its updating the record. When I changed the same code to trigger, its not updating. What am I missing? u.Territory_Backup_IDs__c = u.Territory_Backup_IDs__c+ terrString; system.debug('u.Territory_Backup_IDs__c'+ u.Territory_Backup_IDs__c); Here I am even seeing the value that I expect the field to be updated with in the logs. But at the record level the value is not getting updated. .
KiransssssssssssssssssssKiransssssssssssssssssss

Can any one help me on this please..

I have finished the requirement. And I got a hint of what might have gone wrong here. IF any one can help me it would be great. With your thoughts