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
Raghav TRaghav T 

Batch class is not working as expected

Hi there,
I have been trying to send some case and related case records from apex batch class from SF to SF connectors with code.
But here i am stuck init as it is unable to execute.
Which means its not getting inside the start and execute method of batch class. Please let me understand where i am wrong.
Below is the code for same :
public class SFConnectionFromABCToDEFOrg implements Database.Batchable<sObject> {
    public Date dateFrom = date.newInstance(2023,01,10 0,0,0);
    public Id connectionId;
    public String query;
    public SFConnectionFromABCToDEFOrg(Date dateRange) {        
        dateFrom = dateRange;
        system.debug('inside const '+dateRange);
        connectionId = [Select Id, ConnectionStatus, ConnectionName From PartnerNetworkConnection Where ConnectionName = 'MyConnectionOrg' AND ConnectionStatus = 'Accepted'].Id;
    }
    
    public Database.QueryLocator start(Database.BatchableContext BC){
        system.debug('inside start ');
        query = 'SELECT Id, (SELECT Id FROM Attachments), (SELECT Id FROM CaseComments), (SELECT Id FROM Events), (SELECT Id FROM Tasks) FROM CASE WHERE createdDate>=:'+dateFrom;
        system.debug('inside start '+query);
      	return Database.getQueryLocator(query);
   }

   public void execute(Database.BatchableContext BC, List<Case> caseRecords){
       system.debug('inside exec '+caseRecords);
       List <PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
       for(Case cs : caseRecords){
           prncList.add(new PartnerNetworkRecordConnection(
               ConnectionId = connectionId,
               LocalRecordId = cs.Id,
               SendClosedTasks = true,
               SendOpenTasks = true,
               SendEmails = true));
           
           if(!cs.Attachments.isEmpty()){
               for(Attachment att : cs.Attachments){
                   prncList.add(new PartnerNetworkRecordConnection(
                       ConnectionId = connectionId,
                       LocalRecordId = att.Id,
                       SendClosedTasks = true,
                       SendOpenTasks = true,
                       SendEmails = true));
               }
           }
		
       
           if(!cs.CaseComments.isEmpty()){
               for(CaseComment cc : cs.CaseComments){
                   prncList.add(new PartnerNetworkRecordConnection(
                       ConnectionId = connectionId,
                       LocalRecordId = cc.Id,
                       SendClosedTasks = true,
                       SendOpenTasks = true,
                       SendEmails = true));
               }
           }
           
           if(!cs.Events.isEmpty()){
               for(Event evt : cs.Events){
                   prncList.add(new PartnerNetworkRecordConnection(
                       ConnectionId = connectionId,
                       LocalRecordId = evt.Id,
                       SendClosedTasks = true,
                       SendOpenTasks = true,
                       SendEmails = true));
               }
           }
           
           if(!cs.Tasks.isEmpty()){
               for(Task tsk : cs.Tasks){
                   prncList.add(new PartnerNetworkRecordConnection(
                       ConnectionId = connectionId,
                       LocalRecordId = tsk.Id,
                       SendClosedTasks = true,
                       SendOpenTasks = true,
                       SendEmails = true));
               }
           }
       }
       
       if(!prncList.isEmpty()){

            try{
                system.debug('prncList : '+prncList);
                // upsert prncList;
            }
            catch(System.Dmlexception dmlExceptionInstance){
                System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
            }
        }
   }

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

 
Michael StclairMichael Stclair
Your code actually has a number of problems, though, so let's clean it up: public decimal sum; Useless variable. This variable, if you did need it, should be in the correct scope, close to the point where you actually use it. string query ='select id,name,(select name,id from opportunities) from account where CreatedDAte =Today'; Try this once may be this will work well for you. (https://www.mysanfordchart.net/