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
Svanskey0207Svanskey0207 

Batch Class to Populate assigned territory name to User object

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){
        
    }    
}

 

sakhisakhi
Hi ,

Below is clean sample class
global class leadbatch implements Database.Batchable<sObject>
{   
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        
        String query = 'SELECT Id, status FROM Lead where CreatedDate < LAST_N_DAYS:180';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<lead> scope)
    {
    List<Lead> lstLead = new List<lead>();
        for(lead obj: scope){
            obj.status = 'Closed - Not Converted';
           lstLead.add(obj);
        }
        update lstLead;
        
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
    
   
}
Above is for Update the status of the open Leads to 'Closed - Not Converted' if they are more than 3 Months old
 
Svanskey0207Svanskey0207

@Sakhi, 

I am not sure If you answer on my question. I am asking for help to  design batch apex for specific scenario described in post description. :) But anyway thanks for your comment.