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
Ryan PatRyan Pat 

Batch class help

I have one object called CPM which has couple of fields one is user field which has a lookup with user object and second one is week number which is a formula field it will show which week is going on based on Todays date.
2. On opportunity object i have 2 field one is sub date it is a date field and other is current week which is a formula field it will show current week number based on opportunity sub date .
Now the requirement is based on CPM object record user and week number field it will go to all opportunity which is created this year and sub date is not blank and on opp current week is same and update CPM Field .
Here is a batch class it is working currectly but some one check and optimized this code as much as possible with bulkify also :
global class CPMBatch implements Database.Batchable<sObject> ,Database.Stateful
{
 list< CPM__c > lstToUpdate = new list <CPM__c>();
  global Database.QueryLocator start(Database.BatchableContext BC) {
            
            list <string> stValue = new list <string>();
            list <CPM_Team_List__mdt > LstSaleTeam = [select id , User_Name__c from CPM_Team_List__mdt ];
            for(CPM_Team_List__mdt  st : LstSaleTeam)
            {
                stValue.add(st.User_Name__c);
                
            }
                
                String query = 'SELECT Id,Name, Week_Number__c , User__c , User_Name__c from CPM__c  where Week_Number__c != null and  User_Name__c IN : stValue  ';
          
            
            return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List< CPM__c > scope){
          String  uname ;
          Decimal weeknum ; 
          ID  CPMId;
         for (CPM_MANAGEMENT__c mtscope : scope) 
           {
                      uname = mtscope.User_Name__c ;
                      weeknum = mtscope.Week_Number__c ;
                    CPMId = mtscope.id;
                      
           }
           
           
         list <opportunity > opp = [select id , name , Owner_Name__c from opportunity 
                                                               where sub_Date__c != null 
                                                                     and Current_Week__c =: weeknum  
                                                                     and Owner_Name__c =: uname ]  ; 
                         
                              
         
             for(CPM_MANAGEMENT__c Ctam : scope )
             {
             
                Ctam.id = CPMId ;
                Ctam.CPM_Updated_Goal__c = opp.size();
                lstToUpdate.add(Ctam);
               
         
             }
         if(lstToUpdate.size()>0)
            {
            database.update(lstToUpdate,false);
            }
         }  
    global void finish(Database.BatchableContext BC)
    {
      
    }
}