0

I posted a question yesterday check it here, the answer was to create a batch to run the code. i tried to write a batch class to solve the problem knowing that i have never done that before. But apparently the batch does not work (although no error is returned , i have configured the batch to send me a message at the end of the operation (in the finish method) t no message received so i assumed that the batch is not working).

here is the batch class:

global class batchMouliner implements Database.Batchable<sObject> {
    public String mois {get;set;}
    public String annee {get;set;}
    public String query;
    
    public batchMouliner(String mois, String annee){
        this.mois = mois;
        this.annee = annee;
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc){
            this.query = 'select id, name from poste__c where annee__c =\'' + annee + '\'' +  'and id in (select poste__c from rubrique__c where CALENDAR_YEAR(CreatedDate) =: 2016)';
      return Database.getQueryLocator(this.query);
    }
    
    global void execute(Database.BatchableContext BC, List<poste__c> scope){
        if(scope.size() > 0)
 {
        for(poste__c po: scope)
        String var = calculpaie2.Calculrubrique(po.id,mois);
    }

}
    global void finish(Database.BatchableContext BC) {
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'xxxxx.xxxxxx@gmail.com'};
        mail.setToAddresses(toAddresses);
         mail.setSubject('batch test');
        mail.setPlainTextBody
       ('The batch Apex job processed ....');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });



}
    
        
    }

this batch is called by another class

public class MoulinerToutlemonde {
    public String annee {get;set;}
    public String mois {get;set;}
    public MoulinerToutlemonde (){
        
    }
    public void sauve(){
        
        batchMouliner test = new batchMouliner(mois,annee); 
        database.executeBatch(test );
        
    }
}

where the parameters mois and annee are retrieved from a visualforce page: enter image description here

when i click on the executer button no error is returned but no message received either , i am doing things wrong ?

thank you

Edit: I get this error Batch Apex Failed First error: Only variable references are allowed in dynamic SOQL/SOSL

12
  • Open your developer console, then try executing the batch from the VF page you have. In the logs tab of developer console you should see the logs if the batch is executing or if any error is stopping the batch from executing. Ideally i would put in debug statements in my start and execute methods just to make sure if they are executing these methods. If you are running on sandbox, you would need to explicitly allow non system emails
    – Prady
    Mar 10 '16 at 8:43
  • ok, i will try it and keep you updated Mar 10 '16 at 8:44
  • In the "Setup" UI, search for and examine the "Apex Jobs" information; that gives a top-level view of what happened.
    – Keith C
    Mar 10 '16 at 8:47
  • I wrote that in the developer console String annee = '2015'; String mois ='4'; batchMouliner test = new batchMouliner(mois,annee); database.executeBatch(test ); and it returns a success status bhut always no message recieved Mar 10 '16 at 8:49
  • 1
    I also note that your query string contains colons even though you are not using bind variables so that needs fixing.
    – Keith C
    Mar 10 '16 at 8:49
1

to workaround this problem i have adjusted my code and it works now:

I have used just String variables in the query.for the nested query used to get the IDs of the object 'rubrique __c'(select poste__c from rubrique__c where CALENDAR_YEAR(CreatedDate) =: 2016), i moved it to the class that calls this batch and then i passed these IDS in a String SET Set<String> sett to the batch constructor.

global class batchMouliner implements Database.Batchable<sObject> {
        public String mois {get;set;}
        public String annee {get;set;}
         public Set<String> setS {get;set;}
        public String query;

        public batchMouliner(String mois, String annee,Set<String> sett){
            this.mois = mois;
            this.annee = annee;
            this.setS = sett;
        }

        global Database.QueryLocator start(Database.BatchableContext bc){
               string xx='2015';

            this.query = 'select id, name from poste__c where  annee__c = xx and id IN :setS';

            return Database.getQueryLocator(this.query);
        }

        global void execute(Database.BatchableContext BC, List<poste__c> scope){
            if(scope.size() > 0)
     {

         for(poste__c po: scope){

            String var = calculpaie2.Calculrubrique(po.id,mois);

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

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'xxxxx.xxxxxx@gmail.com'};
            mail.setToAddresses(toAddresses);
             mail.setSubject('batch test');
            mail.setPlainTextBody
           ('The batch Apex job processed ....');

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });





    }


        }

here is the ajusted method that calls the batch:

public void sauve(){

    List<rubrique__c> listrub = [select poste__c from rubrique__c where CALENDAR_YEAR(CreatedDate) =: 2016];
    if(listrub.size() > 0){



  for(rubrique__c rub : listrub){  // add all the IDs to the set 
       setS.add(rub.poste__c);
    };
}
batchMouliner test = new batchMouliner(mois,annee,setS);  // pass the set as a parameter
database.executeBatch(test,10); // fix the size of the batch to 10

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.