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
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5 

Help with a Batch Class!!

Hi All,
I need to create a batch class to be executed every night to make some field updates.
 
I have a custom object called rate_card_item_price__c with two lookup fields:
Advert_Assignment__c    
Product_Node__c

the batch have to find al the rate_card_item_price__c records with the field borrado__c not equals to "X" , and update the Title_Product_Node__c field (IF IT IS NULL) from the Advert_Assignment__c lookup record with the id of the Product_Node__c lookup field.

Also i need to change the picklist value of the Media_Types__c field from the Product_Node__c lookup record to "Print"

Any idea how to do it?

Thanks in advance!
Best Answer chosen by Sergio Ruiz Barrios 5
Saurabh BSaurabh B
Hi Sergio,
Below is the code that you can use.

Batch Class:
global class UpdateRateCardBatch implements Database.Batchable<sObject>, Schedulable {


        global Database.QueryLocator start(Database.BatchableContext BC){
            string myX = 'x';
String query = 'SELECT Advert_Assignment__c,borrado__c,Id,Product_Node__c FROM rate_card_item_price__c WHERE borrado__c != :myX' ;
         return Database.getQueryLocator(query);
        }
      

       global void execute(Database.BatchableContext BC, List<rate_card_item_price__c>scope){

                Map <id,String> RateMaps  = New Map <id,String> ();  
           Set <id> prodIds = New Set <id> ();
           
           List <Advert_Assignment__c> AAList = New List <Advert_Assignment__c> ();
               List <Product_Node__c> PList = New List <Product_Node__c> ();
    
           For (rate_card_item_price__c RC: scope) {
               if (Rc.borrado__c != 'x')
               RateMaps.put(rc.Advert_Assignment__c,rc.Product_Node__c) ;
               prodIds.add(RC.Product_Node__c);
               system.debug(ratemaps);
                    }
                   
           List <Advert_Assignment__c> advAsgn = [SELECT ID,Title_Product_Node__c FROM Advert_Assignment__c WHERE ID IN :RateMaps.keySet() ];        
                                  system.debug(advAsgn);

           For (Advert_Assignment__c AA : advAsgn) {

               if (AA.Title_Product_Node__c == null)
             system.debug(String.valueOf(RateMaps.get(AA.id)));
            AA.Title_Product_Node__c = String.valueOf(RateMaps.get(AA.id)) ;                   
            AAList.add(AA);
           }
           
           List <Product_Node__c> ProdList = [SELECT ID,Media_Types__c FROM Product_Node__c WHERE ID IN:prodIds ];
           
           For (Product_Node__c P :ProdList ){
               P.Media_Types__c = 'Print';
               PList.add(P);
               
           }
           
             Update AAList;  
             Update Plist;
           }
      
       global void finish(Database.BatchableContext BC){

       }
    
     global void execute(SchedulableContext sc) {
         UpdateRateCardBatch b = new UpdateRateCardBatch ();
         database.executebatch(b);
     }
    
     }

To schedule daily run, just run below snippet in Execute Anonymous Window in Developer Console -
UpdateRateCardBatch r1 = new UpdateRateCardBatch ();
String sch1 = '0 0 01 1/1 * ? *';
System.schedule('Rate Card Update', sch1, r1);

FYI... I have tested this in my Dev org and it appears to work fine.

Please mark this as BEST ANSWER if it helps you!
 

All Answers

Saurabh BSaurabh B
Hi Sergio,
Below is the code that you can use.

Batch Class:
global class UpdateRateCardBatch implements Database.Batchable<sObject>, Schedulable {


        global Database.QueryLocator start(Database.BatchableContext BC){
            string myX = 'x';
String query = 'SELECT Advert_Assignment__c,borrado__c,Id,Product_Node__c FROM rate_card_item_price__c WHERE borrado__c != :myX' ;
         return Database.getQueryLocator(query);
        }
      

       global void execute(Database.BatchableContext BC, List<rate_card_item_price__c>scope){

                Map <id,String> RateMaps  = New Map <id,String> ();  
           Set <id> prodIds = New Set <id> ();
           
           List <Advert_Assignment__c> AAList = New List <Advert_Assignment__c> ();
               List <Product_Node__c> PList = New List <Product_Node__c> ();
    
           For (rate_card_item_price__c RC: scope) {
               if (Rc.borrado__c != 'x')
               RateMaps.put(rc.Advert_Assignment__c,rc.Product_Node__c) ;
               prodIds.add(RC.Product_Node__c);
               system.debug(ratemaps);
                    }
                   
           List <Advert_Assignment__c> advAsgn = [SELECT ID,Title_Product_Node__c FROM Advert_Assignment__c WHERE ID IN :RateMaps.keySet() ];        
                                  system.debug(advAsgn);

           For (Advert_Assignment__c AA : advAsgn) {

               if (AA.Title_Product_Node__c == null)
             system.debug(String.valueOf(RateMaps.get(AA.id)));
            AA.Title_Product_Node__c = String.valueOf(RateMaps.get(AA.id)) ;                   
            AAList.add(AA);
           }
           
           List <Product_Node__c> ProdList = [SELECT ID,Media_Types__c FROM Product_Node__c WHERE ID IN:prodIds ];
           
           For (Product_Node__c P :ProdList ){
               P.Media_Types__c = 'Print';
               PList.add(P);
               
           }
           
             Update AAList;  
             Update Plist;
           }
      
       global void finish(Database.BatchableContext BC){

       }
    
     global void execute(SchedulableContext sc) {
         UpdateRateCardBatch b = new UpdateRateCardBatch ();
         database.executebatch(b);
     }
    
     }

To schedule daily run, just run below snippet in Execute Anonymous Window in Developer Console -
UpdateRateCardBatch r1 = new UpdateRateCardBatch ();
String sch1 = '0 0 01 1/1 * ? *';
System.schedule('Rate Card Update', sch1, r1);

FYI... I have tested this in my Dev org and it appears to work fine.

Please mark this as BEST ANSWER if it helps you!
 
This was selected as the best answer
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
It really works fine!

Many thanks for your help Saurabh

Regards.
Saurabh BSaurabh B
You are welcome! :)