Delete Object Data Created By Within the 48 Hours Automatically Useing Batch Class


 Senario :Account The segment in the account is not matched with Lead within the 48 hours

Example code :

public class BatchAccountLeadSegmentUpdate implements Database.Batchable<sobject> {   

    public Database.QueryLocator start(Database.BatchableContext BC) {

        datetime last48hrago = system.now().addhours(-48);

        String query = 'Select Id, Segment__c,Account__r.Segment__c from Lead where Account__r.LastModifiedDate>:last48hrago';

        system.debug('queryresult'+query);

        return Database.getQueryLocator(query); 

    }

    public void execute(Database.BatchableContext BC, List<Lead> leadList) { 

        list<lead> lelist = new  list<lead>();

         for(lead le :leadList){

            if(le.Segment__c!=le.Account__r.Segment__c){

                le.Segment__c=le.Account__r.Segment__c;

            }

            lelist.add(le);  

        }

        if(lelist.size()>0){

            update lelist;

            system.debug('lelist'+lelist);

        }

    }

   public void finish(Database.BatchableContext BC) {  

         }

}

Post a Comment

0 Comments