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
symantecAPsymantecAP 

Help with Batch Apex that is scheduled every 15 mins.

Hi All

 

Here is my requirement. I have an Object called Big Machines Quotes and there is a field which is look up to opportunity . Whenever the Status fields on Big Machine Quote is changed to "Approve" then the  stage field on Opportunity is changes to "Closed Won".

The challenge for me is I want to acheive this by Scheduling by Batch Apex. By batch apex should run every 15 mins and update the corresponding field.

 

Kindly help.

symantecAPsymantecAP

Here is my code so far since morning

global class updateOpportunityStage implements Database.Batchable<sObject>{
public String query;

global database.querylocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(query);    
}
    
global void execute(Database.BatchableContext BC, List<sObject> scope){
       // List<BigMachines__Quote__c> quoteList= new List<BigMachines__Quote__c>();
     
                List <Opportunity> oppList = new List<Opportunity>() ;
        for(sObject s : scope){
        
        BigMachines__Quote__c quote = (BigMachines__Quote__c)s;
        System.debug('Adil'+quote);
         if(quote.BigMachines__Status__c.contains ('unison') ){

        Opportunity opp = [select id, StageName from Opportunity where id=:quote.id];
         opp.stageName = 'Closed Won' ;
            
    }
    update oppList;
    
    
    }
    }
  global void finish(Database.BatchableContext BC){}  

}

 Please review

Insane@blrInsane@blr

Hi,

 

You need to right a schedular class which runs on every 15 mins.In schedular you have execute method, in this method you need to write your logic for updating fields.

 

Let me know if any problems.

 

Regards,

Insane

 

 

symantecAPsymantecAP

I have written a schedulable class. but isnt the logic correct to update field

 

List <Opportunity> oppList = new List<Opportunity>() ;
        for(sObject s : scope){
        
        BigMachines__Quote__c quote = (BigMachines__Quote__c)s;
        System.debug('Adil'+quote);
         if(quote.BigMachines__Status__c.contains ('unison') ){

        Opportunity opp = [select id, StageName from Opportunity where id=:quote.id];
         opp.stageName = 'Closed Won' ;
            
    }
    update oppList;

 

 

Thanks

Adil

symantecAPsymantecAP

and also i am getting the following error once i Scheduled it

 

First error: Argument 1 cannot be null

Insane@blrInsane@blr

Hi ,

 

Send me your schdule class and how your invoking this schduler class.

 

symantecAPsymantecAP

Here is my scheduled class

 

global class scheduledBatchable implements Schedulable{
   global void execute(SchedulableContext SC){
        updateOpportunityStage stg = new updateOpportunityStage();
        database.executebatch(stg);
        
    }
}

 Thanks

Insane@blrInsane@blr

What is the problem here?

symantecAPsymantecAP

I get this

 

 

First error: Argument 1 cannot be null

 

in my apex jobs

Insane@blrInsane@blr

Where is the value for "query" in your batch class. are you passing from scheduler or directly hard coding in Batch class. I dont see the value for query field in ur code. This is causing error.

 

Regards,

Insane

symantecAPsymantecAP

where do i put the query in my batch class and what query do i put for my condition?

do i put inside the start method or above the start method as

 

Query = 'SELECT id,BigMachines__Status__c from BigMachines__Quote__c where BigMachines__Status__c = '*unison*';

Insane@blrInsane@blr

In Batch class ,

 

global String query = 'Write ur query here ';

symantecAPsymantecAP

Hi I have added the query

 

the job says completed but batch processed is just one and also plz look in to my logic in execute.

 

global class updateOpportunityStage implements Database.Batchable<sObject>,Schedulable{
global string query = 'SELECT id,BigMachines__Status__c from BigMachines__Quote__c';



global database.querylocator start(Database.BatchableContext BC){


     //List<BigMachines__Quote__c> quoteList= new List<BigMachines__Quote__c>();
//BigMachines__Quote__c bmq = [Select id,BigMachines__Status__c from BigMachines__Quote__c where BigMachines__Status__c = '*unison*'];
    return Database.getQueryLocator(query);    
}
    global void execute(SchedulableContext SC){
        updateOpportunityStage stg = new updateOpportunityStage();
        database.executebatch(stg);
        
    }

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

     
                List <Opportunity> oppList = new List<Opportunity>() ;
        for(sObject s : scope){
        
        BigMachines__Quote__c quote = (BigMachines__Quote__c)s;
        System.debug('Adil'+quote);
        
        
        
         if(quote.BigMachines__Status__c.contains ('unison') &&  quote.BigMachines__Is_Primary__c == true ){

        Opportunity opp = [select id, StageName from Opportunity where id=:quote.id];
         opp.stageName = 'Closed Won' ;
        
    }
    update oppList;
    
    
    }
    }
  global void finish(Database.BatchableContext BC){}  

}

 Thanks

Adil

salonidhuria1.3964283414155535E12salonidhuria1.3964283414155535E12
hi i need to know the flow, i mean whether schedular class executes first or batch  class and if schedular then how ?