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
Geetanjali Sharma 9Geetanjali Sharma 9 

create apex batch class that uses scheduled apex to insert, update, delete profile records

Hi Guys,
i am creating Batch apex class to insert/update/delete records in my custom object emploee_profile from Profiles. I am able to insert but delete/update not working.

please see my code and guide me where i am wrong..actually i am beginner in salesforce:

global class ProfilesSync implements database.batchable<sObject>,Database.Stateful{
    global map<ID,Profile> ProfileRegMap=new map<ID,Profile>();
    global Database.QueryLocator start(Database.BatchableContext BC){
        System.debug('in start');
            return database.getQueryLocator([select ID,Name from Profile order by ID]);
        catch(Exception e){
        return null;
    global void execute(Database.BatchableContext BC, List<Profile> ProfilesList)
            for(Profile rl:ProfilesList){
    global void finish(Database.batchableContext BC){
        List<Employee_Profile__c> Employee_Profile_list1 = new List<Employee_Profile__c>([select Profile_ID__c,Profile_Name__c from Employee_Profile__c order by Profile_ID__c]);
        List<Employee_Profile__c> Employee_Profile_list_Insert = new List<Employee_Profile__c>();
        List<Employee_Profile__c> Employee_Profile_list_update = new List<Employee_Profile__c>();
        List<Employee_Profile__c> Employee_Profile_list_delete = new List<Employee_Profile__c>();
        map<ID,Employee_Profile__c> custRoleMap=new map<ID,Employee_Profile__c>();
        for(Employee_Profile__c us:Employee_Profile_list1){
            for(Profile urs:ProfileRegMap.values()){
                  system.debug('New records');
                        Employee_Profile__c urs2 = new Employee_Profile__c();
            system.debug('size of cust list'+Employee_Profile_list_Insert.size());
            insert Employee_Profile_list_Insert;
           update Employee_Profile_list_update;
           delete Employee_Profile_list_delete;
Sankeerth ReddySankeerth Reddy
You never populated the lists Employee_Profile_list_update and Employee_Profile_list_delete, for them to perform update or delete functions.
Hi Geetanjali,

As per your code your Employee_Profile_list_update list and  Employee_Profile_list_delete list is empty so it is not doing any update and delete operation.