2

I'm want to update all Closed Won Opportunities Every Month and this is my batch apex class the problem here is it's not entering if loop but I guess if loop is proper. I'm unable to rectify where the bug is.

global class UpdateOpportunity implements Database.Batchable<sObject>{

    string query;

    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id, name, Amount__c, Man_Power_Amount__c,Stage__c From Opportunity__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Opportunity__c> scope1){
       system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+scope1);

        for(Opportunity__c p : Scope1){
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+p.stage__c);    
              if(p.stage__c == 'Closed Won')
              {
               p.Man_Power_Amount__c = 000;
              }    
               update p;  
        }       
    }

    global void finish(Database.BatchableContext BC){
    }
}

Rather i guess it's not at all executing "Execute Method" Kindly help on this.

3
  • 1
    Are you gettting any result set in scope method ?Whats the size of data thats returned by query locator?Whats the scheduler code you are executing May 21, 2014 at 12:29
  • Does this query compile OK? Maybe you mistyped some field names and the start() fails. Attach debug log to yourself and try submitting the batch? Another option is that this query would take too long to run (the condition should be in WHERE clause, not in the if) - but you should be able to see something in the log (Setup -> Apex jobs)
    – eyescream
    May 21, 2014 at 22:37
  • Why does Opportunity have the __c at the end? Have you made a Custom Object named Opportunity that you are trying to use? If you are trying to use the Standard Salesforce Opportunity remove the "__c"
    – dBeltowski
    Jun 22, 2015 at 14:19

2 Answers 2

2

I see a couple issues with your code.

1) Are you trying to use the standard Salesforce Opportunity and standard field Stage? If so remove the "__c" from both. Otherwise if you have created a custom object and named it "Opportunity__c" then I see no reason whey the query should fail, or enter the If condition.

2) Your code is not properly bulkified. Instead of querying every Opportunity object in your org and checking to see if it's "Closed Won" you should only query those records. I would also put in a check to make sure you don't query records in which you've already populated your Man_Power_Amount fields. Finally you should put your update outside of the for loop and update the list rather than updating each record at a time.

You can use the code below as a guide.

global class UpdateOpportunity implements Database.Batchable<sObject>{

    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 Stage = \'Closed Won\'' +
                'AND Man_Power_Amount__c != 000';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Opportunity__c> scope1){
       system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+scope1);

        for(Opportunity__c p : Scope1){
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+p.stage__c);    
            p.Man_Power_Amount__c = 000;

        }       
        update p; 
    }

    global void finish(Database.BatchableContext BC){
    }
}
3
  • 2
    and, as per @sfdcfox, global as access modifier is not required unless building an appexchange package; public will suffice.
    – cropredy
    Jun 22, 2015 at 19:46
  • @crop1645 Is there documentation around this? Everything I've seen from Salesforce has the global access modifier on it.
    – dBeltowski
    Jun 22, 2015 at 20:24
  • not that I'm aware of ... I learned it from an answer from @sfdcfox, tried it myself and it works fine. I agree that all of the sfdc examples use global but it really isn't required in the non-managed package use case
    – cropredy
    Jun 22, 2015 at 21:47
-1

i tried my developer edition its working fine

global class UpdateOpportunity implements Database.Batchable<sObject>{

    string query;

    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id, name,Stage__c From Opportunity__c';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Opportunity__c> scope1){
       system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+scope1);

        for(Opportunity__c p : Scope1){
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+p.stage__c);    
              if(p.stage__c == 'Closed Won')
              {
               p.Name = 'test';
              }    
               update p;  
        }       
    }

    global void finish(Database.BatchableContext BC){
    }
}

Execute batch in developer console like

UpdateOpportunity up = new UpdateOpportunity(); 

Database.executeBatch(up,1);  
2
  • 2
    Database.executeBatch(up,1); what is the significance of "1" here
    – Sharat
    May 22, 2014 at 5:39
  • The 1 limits the number of records in each batch execute method. This is necessary since you are calling update inside your loop and will run into DML limits. Oct 22, 2014 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .