+ Start a Discussion
Jyosi jyosiJyosi jyosi 

Excute() batch is triggering twice once in a while

Hello All,
I wrote the batch class and i make the callout to external system.The batch class is schedule for eelpe hvery 3 hours.
but here is some problem The batch job works fine .but it gives weried error at 9 clock batch pushing the values twice to the exteral system.(And it happens 2 months once) Is there any way we can check out the excute methods fire twices for that time interval.

Thanks for the help.

Regards,
Jyo

 
James LoghryJames Loghry
Is the batch class scheduled then?  Is it possible there is a second schedule that kicks off the same batch job?  Another possiblity is that the batch job is executed manually by another admin or developer.  A third possiblity is that the batch job is "chained" e.g. executes itself in the finish method.

If you could provide us some code (both for the scheduled class and the batch job), perhaps it could should some light on your issue.  If you choose to do so, please use the code format (< >) button.

Thanks!
Jyosi jyosiJyosi jyosi
Hello James

Sorry for the delay response.

Schedule class

global class OutboundScheduler implements Schedulable  {
    global void execute(SchedulableContext ctx) {
        GlobalComplaintOutbound SAPoutbound= new GlobalComplaintOutbound();
        Database.executeBatch(SAPoutbound,5);
       
    }
}



global class GlobalComplaintOutbound implements Database.Batchable<sObject>, Database.AllowsCallouts,Database.Stateful  {
    
    
    public String query = 'SELECT ID,Date_Received__c,GL_Complaint_Number__c,Cause_Group__c,Complaint_Mailing_City__c,Complaint_Mailing_State_Provin__c,Complaint_Mailing_Zip_Postal_Code__c,Complaint_Mailing_Country__c,Complaint_Phone__c,Complaint_Source__c,Origin,product__c,Complaint_Qty__c,Complaint_Qty_Unit_of_Measure__c,Complaint_Lot3__c,Complaint_Lot__c,Complaint_Code__c,Complaint_Lot_Unknown__c,Voice_of_Customer__c,Associated_Product__c,Problem_Damage_Code__c,Complaint_Lot2__c,Normal_Usage__c,Actual_Usage__c,Send_Samples__c,GCS_Distribution_Channel__c,Description,GCS_Divsion__c, GCS_Sales_Org__c,Product_Brand__c,Cust_Number__c,Complaint_Mailing_Street__c,Samples_to_be_Sent__c,Contact.Email,Potential_Injury__c,Product__r.Name,Account.Name,Contact_First_Name__c,Opt_out_complaint_code__c,Go_to_Potential_Reason__c,Contact.LastName,Contact_Last_Name__c,GCS_Customer_Number__c,CreatedBy.Name,LastModifiedBy.Name from case where status=:ReadyForSAP';
    
 global Database.QueryLocator start(Database.BatchableContext BC)
   {    
       return Database.getQueryLocator(query);   
    
   }
   
   global void execute(Database.BatchableContext BC, List<Case> scope)
    {
         Datetime Date_Received_date;
         String Date_Received;
        String CaseId;
       
          String CasetoSap;
        String Date2SAP;
        
        String Biz_Talktostring;
        
        List<Database.SaveResult> updateResults;
        List<case> casequery=  new List<case>();
        Map<Id, case> CaseUpdate= new Map<Id, case>();
       List<case> Case_Values= new List<case>();
        List<hiGcsQncreateQncreaterequest.QN_element> QN_ListSetData = new List<hiGcsQncreateQncreaterequest.QN_element>();
    casequery=[SELECT ID,Date_Received__c,GL_Complaint_Number__c,Country__c,Cause_Group__c,Complaint_Mailing_City__c,Complaint_Mailing_State_Provin__c,complaint_Mailing_Zip_Postal_Code__c,Complaint_Mailing_Country__c,Complaint_Phone__c,Complaint_Source__c,Origin,product__c,Complaint_Qty__c,Complaint_Qty_Unit_of_Measure__c,Complaint_Lot3__c,Complaint_Lot__c,Complaint_Code__c,Complaint_Lot_Unknown__c,Voice_of_Customer__c,Associated_Product__c,Problem_Damage_Code__c,Complaint_Lot2__c,Normal_Usage__c,Actual_Usage__c,Send_Samples__c,GCS_Distribution_Channel__c,Description,GCS_Divsion__c, GCS_Sales_Org__c,Product_Brand__c,Cust_Number__c,Complaint_Mailing_Street__c,Samples_to_be_Sent__c,Contact.Email,Potential_Injury__c,Product__r.Name,Account.Name,Contact_First_Name__c,Opt_out_complaint_code__c,Go_to_Potential_Reason__c,Contact.LastName,Contact_Last_Name__c,GCS_Customer_Number__c,CreatedBy.Name,LastModifiedBy.Name from case where status=:ReadyForSAP];

             system.debug('casequery----------------' +casequery);
             system.debug('casequerysize----------------' +casequery.size());

            for(case CaseList:casequery)
             {
                        // QN_Listset.clear();
    
     
          List<Survey_Custom__c> Survey_Records= new List<Survey_Custom__c>();
         Survey_Records =[SELECT Id,A1_Description__c,A2_Description__c,LastModifiedDate ,A3_Description__c,A4_Description__c,A5_Description__c,A6_Description__c,A7_Description__c,A8_Description__c,A9_Description__c FROM Survey_Custom__c where complaint__c=:CaseId ORDER BY  LastModifiedDate DESC Limit 1];
        System.debug('complainantInfo_element.accountNumberAfterSurvey@@@@@@@@@@@'+complainantInfo_element.accountNumber);
         system.debug('Survey_Records============='+Survey_Records);
         system.debug('Survey_RecordsSize============='+Survey_Records.size());
        for(Survey_Custom__c  Survey_Custom:Survey_Records)
        {
            System.debug('complainantInfo_element.accountNumberAfterLoop@@@@@@@@@@@'+complainantInfo_element.accountNumber);  
            complaintInfo_element.answer1=Survey_Custom.A1_Description__c;
              complaintInfo_element.answer2=Survey_Custom.A2_Description__c;
              complaintInfo_element.answer3=Survey_Custom.A3_Description__c;
              complaintInfo_element.answer4=Survey_Custom.A4_Description__c;
              complaintInfo_element.answer5=Survey_Custom.A5_Description__c;
              complaintInfo_element.answer6=Survey_Custom.A6_Description__c;
              complaintInfo_element.answer7=Survey_Custom.A7_Description__c;
              complaintInfo_element.answer8=Survey_Custom.A8_Description__c;
              complaintInfo_element.answer9=Survey_Custom.A9_Description__c;            
        }
        System.debug('complainantInfo_element.accountNumberAfterLoop@@@@@@@@@@@'+complainantInfo_element.accountNumber);          
        QN_Element.complaintInfo=complaintInfo_element;
        

             QN_Element.qnInfo=qnInfo_element;
             QN_Element.productInfo=productInfo_element;
             QN_Element.complainantInfo=complainantInfo_element;
         system.debug('QN_Listset-------------'+QN_ListSetData.size()+'|'+QN_Element.qnInfo.sfdcIDNumber+'|'+QN_Element+'|'+qnInfo_element.sfdcIDNumber);                 
             QN_ListSetData.add(QN_Element);
             
//             QNSet_element.QN=QN_ListSetData;
       
     
    }


        for(integer k =0 ; k<qnListMap.size(); k++){
         try{    
         Blob headerValue = Blob.valueOf(sapUserDet);
         system.debug('userName------'+sapUserDet);
         String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
         system.debug('authorizationHeader-----'+authorizationHeader);
         createsapdata.timeout_x=120000;
         createsapdata.endpoint_x=Biz_Talk_EndPoint;    
         createsapdata.inputHttpHeaders_x = new Map<String,String>();
         createsapdata.inputHttpHeaders_x.put('Authorization', authorizationHeader);
         system.debug('HeaderValue'+createsapdata.inputHttpHeaders_x.put('Authorization', authorizationHeader));
         hiGcsQncreateQncreaterequest.QNSet_element QNSet_element= new hiGcsQncreateQncreaterequest.QNSet_element();                                              
         QNSet_element.QN=qnListMap.get(k);
              system.debug('XX:'+qnListMap.get(k).size()+'|'+QNSet_element.QN.size());
                hiGcsQncreateQncreateresponse.Response_element QNCreate_Response= createsapdata.opQNCreate(QNSet_element.QN);
                system.debug('QNCreate_Response'+QNCreate_Response);
                 if(QNCreate_Response!=null)
                 {
                     system.debug('Line124Response'+QNCreate_Response.dateReceived+'||'+QNCreate_Response.countReceived+'||'+QNCreate_Response.countSuccess);
                      If(QNCreate_Response.batchReceived==true && QNCreate_Response.dateReceived!=null)
                       {
                           
                          Datetime Date_Receiveds=QNCreate_Response.dateReceived;
                          system.debug('Baxtch Recevied'+QNCreate_Response.batchReceived+' ERR '+QNCreate_Response.countSuccess+'||'+QNCreate_Response.countError);
                          datetime  Biz_TalkDateGMT= QNCreate_Response.dateReceived;
                          system.debug('Biz_TalkDateGMT---'+Biz_TalkDateGMT);
                          Biz_Talktostring  =Biz_TalkDateGMT.formatGMT('yyyy/MM/dd HH:mm:ss');
                          system.debug('Biz_Talktostring---'+Biz_Talktostring);
                         for(case CaseList:casequery){
                             CaseList.Web_Service_Response_Timestamp__c=Biz_Talktostring;
                              CaseList.Status='Sent to SAP';
                              //CaseList.id=CaseId;
                              Case_Values.add(CaseList);
                              //CaseUpdate.put(CaseListS.Id, CaseListS);
                          }  
                       }
                 }
}catch(System.CalloutException e)
              {
                    String Readtimeout= e.getMessage();
                  system.debug('Readtimeout@@@@@@@@@@@'+Readtimeout);
                  if(!Readtimeout.containsIgnoreCase('IO Exception: Read timed out'))
                  {
                     system.debug('CalloutException'+e.getMessage());
                      String SubjectMessage=e.getMessage();
                       datetime Failure_Time_GMT= Datetime.now();
                      String Failure_Time= Failure_Time_GMT.format('yyyy/MM/dd HH:mm:ss');
                      String body = 'Error Message::::::::'+ SubjectMessage;
                                             mail.setToAddresses(toAddresses);
                       mail.setCcAddresses(ccAddresses);
                         mail.setPlainTextBody('content');
                         mail.setSubject('Salesforce.com is unable to process the batch to BizTalk Server  '   + '@' +Failure_Time );
                         mail.setHtmlBody(body);
                         mail.setSenderDisplayName('Noreply@salesforce.com');
                         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                            break;
                  }else{
                      //hiGcsQncreateQncreateresponse.Response_element QNCreate_Response= createsapdata.opQNCreate(QN_Listset);
                                  
                  }
              }
                  
         }
            
        

                           
         
        if(Case_Values.size()>0)
        try{
            updateResults = Database.update(Case_Values,true);
            for (Database.SaveResult sr : updateResults)
            {
              if (sr.isSuccess())
              {
                System.debug('Successfully Update Case Values. CaseIds' + sr.getId());
              }
        else{
             for(Database.Error err : sr.getErrors())
             {
                System.debug('The following error has occurred.');                    
                system.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('CaseIds that affected this error: ' + err.getFields());
              }
            }
                
            }
        }catch(DmlException e) {
            system.debug('DmlException while updating case'+e.getMessage());
            
        }
        
         
    }
    
    global void finish(Database.BatchableContext BC)
      
    {
    }
       
  }


Thanks for the help.

Regards,
Jyo