• Svanskey0207
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hello Guys, 

Under the user object, we have a field named "PrimaryTerritory__c". We need to populate this field with Territory Name/Names assigned to user. Scenario is the following. 

Parent Objects: "User", "Territory2"
Child Object:  "UserTerritory2Association"

"UserTerritory2Association" has 2 lookup fields, one to User, second to Territory. Actually this object is playing "Junction" object role but for lookup relationship. Whenewer territory is assigned to user, "UserTerritory2Association" is created automatically and stores the information about this particular assoctiation between User and Territory2. (This is standard SF feature and no Process or Flow supported)

I need to take Territory2.Name field from Territory2 Object and store to User object using "UserTerritory2Association" object. For our business scenario, we need Batch or Trigger.  I have a Batch template which actually does not work because of not proper setup of Lists sets maps and for loops. I need some help on proper setup these. Please recommend if you had something similar?  

global class Alcon_PrimaryUserTerritoryFillout implements Database.Batchable <sObject> {
     
     global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select id, User.PrimaryTerritory__c, RoleInTerritory2, Territory2.Name from UserTerritory2Association where User.Primary_Territory_vod__c = null AND RoleInTerritory2 ='1 - Primary']);
             
     }
  
    global void execute(Database.BatchableContext bc, List<UserTerritory2Association> scope){
  
      for(UserTerritory2Association UserTerAssoc: scope){
             UserTerAssoc.User.Primary_Territory_vod__c = UserTerAssoc.Territory2.Name; 
                      
            }
        update scope;
      
    }
     global void finish(Database.BatchableContext bc){
        
    }    
}

 

Hello Guys, 

Under the user object, we have a field named "PrimaryTerritory__c". We need to populate this field with Territory Name/Names assigned to user. Scenario is the following. 

Parent Objects: "User", "Territory2"
Child Object:  "UserTerritory2Association"

"UserTerritory2Association" has 2 lookup fields, one to User, second to Territory. Actually this object is playing "Junction" object role but for lookup relationship. Whenewer territory is assigned to user, "UserTerritory2Association" is created automatically and stores the information about this particular assoctiation between User and Territory2. (This is standard SF feature and no Process or Flow supported)

I need to take Territory2.Name field from Territory2 Object and store to User object using "UserTerritory2Association" object. For our business scenario, we need Batch or Trigger.  I have a Batch template which actually does not work because of not proper setup of Lists sets maps and for loops. I need some help on proper setup these. Please recommend if you had something similar?  

global class Alcon_PrimaryUserTerritoryFillout implements Database.Batchable <sObject> {
     
     global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select id, User.PrimaryTerritory__c, RoleInTerritory2, Territory2.Name from UserTerritory2Association where User.Primary_Territory_vod__c = null AND RoleInTerritory2 ='1 - Primary']);
             
     }
  
    global void execute(Database.BatchableContext bc, List<UserTerritory2Association> scope){
  
      for(UserTerritory2Association UserTerAssoc: scope){
             UserTerAssoc.User.Primary_Territory_vod__c = UserTerAssoc.Territory2.Name; 
                      
            }
        update scope;
      
    }
     global void finish(Database.BatchableContext bc){
        
    }    
}