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
DJ 367DJ 367 

batch is not inserting record

Hello All,

I am querying record from one object and inserting to the other object but record is not getting inserted into another object. Can someone help me pls. Thanks

here is my code:
global class FileImpoLog implements Database.Batchable<sObject>{   
    global Database.QueryLocator start(Database.BatchableContext bc){
        date d=system.today();            
        return Database.getQueryLocator([Select Document_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById from Importer_Submit__c ]);      
    } 

    global void execute(Database.BatchableContext BC, list<Importer_Submit__c> lstFileImpor){  
        system.debug('lstFileImpor******' + lstFileImpor);
        List<Importer_Log_Entry__c> lstLogEntry = new List<Importer_Log_Entry__c>();
        for(Importer_Submit__c M : lstFileImpor){
            Importer_Log_Entry__c ML = new Importer_Log_Entry__c();
            ML.created_by__c = string.valueof(M.CreatedBy);
            ML.Document_Process_Date__c = M.Document_Process_Date__c;
            ML.Workspace_Id__c = M.Workspace__c;
            ML.Workspace_Name__c = M.Folder_Name__c;
            ML.Created_Date__c = M.CreatedDate;
        lstLogEntry.add(ML);
        }
        system.debug('lstLogEntry*******' + lstLogEntry);
        
            insert lstLogEntry;
    }
    
    global void finish(Database.BatchableContext BC){                 

    }
}

 
Best Answer chosen by DJ 367
DJ 367DJ 367

Hello,

The error was here
 return Database.getQueryLocator([SelectDocument_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById fromImporter_Submit__c ]); 

Instead we need to pass the query in string 

somthing like : query = 'SelectDocument_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById fromImporter_Submit__c '

All Answers

Raj VakatiRaj Vakati
Your code looks correct to me 
Check below point 
  1. Check the logs to see any error is happening 
  2. Are you inserting all the required fields ??
DJ 367DJ 367
Hi Raj,

Yes I am inserting all the required fields and I am not seeing the debug in the debug log.
 
Raj VakatiRaj Vakati
try this code and see what is printing and you will see mutliple logs for each execute method and ceck the logs
global class FileImpoLog implements Database.Batchable<sObject>{   
    global Database.QueryLocator start(Database.BatchableContext bc){
        date d=system.today();            
        return Database.getQueryLocator([Select Document_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById from Importer_Submit__c ]);      
    } 

    global void execute(Database.BatchableContext BC, list<Importer_Submit__c> lstFileImpor){  
        system.debug('lstFileImpor******' + lstFileImpor);
        List<Importer_Log_Entry__c> lstLogEntry = new List<Importer_Log_Entry__c>();
        for(Importer_Submit__c M : lstFileImpor){
            Importer_Log_Entry__c ML = new Importer_Log_Entry__c();
            ML.created_by__c = string.valueof(M.CreatedBy);
            ML.Document_Process_Date__c = M.Document_Process_Date__c;
            ML.Workspace_Id__c = M.Workspace__c;
            ML.Workspace_Name__c = M.Folder_Name__c;
            ML.Created_Date__c = M.CreatedDate;
        lstLogEntry.add(ML);
        }
        system.debug('lstLogEntry*******' + lstLogEntry);
        try{
            insert lstLogEntry;
		}catch(Exception e){
		System.debug('******** EEEE'+e);
		}
			
    }
    
    global void finish(Database.BatchableContext BC){                 

    }
}


 
DJ 367DJ 367
Hi,

Debug is not available in the log.
Raj VakatiRaj Vakati
Can you check the below query its returning any data ot not  .. execute the below SOQL from developer console and see whether you have any data or not

SelectDocument_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById fromImporter_Submit__c 


Set logs like this 




From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
Click New.
Set the traced entity type to User.
Open the lookup for the Traced Entity Name field, and then find and select your guest user.
Assign a debug level to your trace flag.
Click Save.
Raj VakatiRaj Vakati
Ping me here ..rajamohanvakati@gmail.com
Abdul KhatriAbdul Khatri
Please check under what user Batch is running and that user has Read/View All Permission for that SObject Importer_Submit__c
DJ 367DJ 367

Hello,

The error was here
 return Database.getQueryLocator([SelectDocument_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById fromImporter_Submit__c ]); 

Instead we need to pass the query in string 

somthing like : query = 'SelectDocument_Process_Date__c,Workspace__c,Folder_Name__c,CreatedDate,CreatedById fromImporter_Submit__c '

This was selected as the best answer