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
SFDC 18SFDC 18 

Unable to run the apex batch code to renew opportunities before 30 days of close date

Hi,
I have written the below apex batch code to renew the oportunity before 30 days of close date. I'm unable to run the code. Can anyone help me with the below code.
 
global class UpdateOpportunity implements
	Database.Batchable<sObject>, Database.stateful{

    string query;

    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, Amount__c, Man_Power_Amount__c,Stage__c ' +
                'FROM Opportunity__c ' +
                'WHERE CloseDate = CloseDate()-30';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc, List<Opportunity__c> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
               for (Opportunity opp : Scope ) {
 
    if (opp.CloseDate <= opp.CloseDate - 30) {
       Opportunity renewal = new Opportunity();
       renewal.AccountId   = 'opp.AccountId';
       renewal.Name        = opp.Name + 'Renewal';
       renewal.CloseDate   = opp.CloseDate + 365;
       renewal.StageName   = 'Open';
       renewal.RecordType  = 'Renewal';
       renewal.OwnerId     = opp.OwnerId;
       renewals.put(renewal.Id, renewal);

        }       
        update p; 
    }

    global void finish(Database.BatchableContext bc){
        System.debug(recordsprocessed + '' );
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
          EmailUtils.sendMessage(a, recordsProcessed);
    }
}
    }

 
Best Answer chosen by SFDC 18
Raj VakatiRaj Vakati
 Go to developer console and execute anonymous and run the below code.
 
Database.executeBatch(new UpdateOpportunity());
Update your batch with below code
 
global class UpdateOpportunity implements Database.Batchable<sObject>, Database.stateful{
    
    string query;
    
    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, Amount__c, Man_Power_Amount__c,Stage__c ' +
            'FROM Opportunity__c ' +
            'WHERE CloseDate = CloseDate()-30';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;
        for (Opportunity opp : oppList ) {
            
            if (opp.CloseDate <= opp.CloseDate - 30) {
                Opportunity renewal = new Opportunity();
                renewal.AccountId   = opp.AccountId;
                renewal.Name        = opp.Name + 'Renewal';
                renewal.CloseDate   = opp.CloseDate + 365;
                renewal.StageName   = 'Open';
                renewal.RecordTypeId  = opp.RecordTypeId;
                renewal.OwnerId     = opp.OwnerId;
                opportunities.add(renewal);
                
            }       
            update opportunities; 
        }
    }
    
    global void finish(Database.BatchableContext bc){
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
                            JobItemsProcessed,
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
        EmailUtils.sendMessage(a, recordsProcessed);
    }
}

 

All Answers

Raj VakatiRaj Vakati
 Go to developer console and execute anonymous and run the below code.
 
Database.executeBatch(new UpdateOpportunity());
Update your batch with below code
 
global class UpdateOpportunity implements Database.Batchable<sObject>, Database.stateful{
    
    string query;
    
    global Database.querylocator start(Database.BatchableContext bc){
        Query = 'SELECT id, name, Amount__c, Man_Power_Amount__c,Stage__c ' +
            'FROM Opportunity__c ' +
            'WHERE CloseDate = CloseDate()-30';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Opportunity> scope){
        List<Opportunity> opportunities = new List<Opportunity>();
        List<Opportunity> oppList = (List<Opportunity>)scope ;
        for (Opportunity opp : oppList ) {
            
            if (opp.CloseDate <= opp.CloseDate - 30) {
                Opportunity renewal = new Opportunity();
                renewal.AccountId   = opp.AccountId;
                renewal.Name        = opp.Name + 'Renewal';
                renewal.CloseDate   = opp.CloseDate + 365;
                renewal.StageName   = 'Open';
                renewal.RecordTypeId  = opp.RecordTypeId;
                renewal.OwnerId     = opp.OwnerId;
                opportunities.add(renewal);
                
            }       
            update opportunities; 
        }
    }
    
    global void finish(Database.BatchableContext bc){
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
                            JobItemsProcessed,
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
        EmailUtils.sendMessage(a, recordsProcessed);
    }
}

 
This was selected as the best answer
SFDC 18SFDC 18
Hi, 
I got the below compile errors when i tried to run the code
User-added image
SFDC 18SFDC 18
Hi,
when i have removed the emailutils and recordtype, I was able to run my code successfully