+ 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');
        try{
            return database.getQueryLocator([select ID,Name from Profile order by ID]);
        }
        catch(Exception e){
            System.debug('Error=='+e.getStackTraceString());
        }
        return null;
    }
    global void execute(Database.BatchableContext BC, List<Profile> ProfilesList)
    {
        System.debug('ProfileList=='+ProfilesList.size());
        if(ProfilesList!=NULL)
        {
            for(Profile rl:ProfilesList){
                ProfileRegMap.put(rl.ID,rl);
            }
        }
    }
    global void finish(Database.batchableContext BC){
        System.debug('UserProfileList=='+ProfileRegMap);
        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){
            custRoleMap.put(us.Profile_ID__c,us);
        }
 
            for(Profile urs:ProfileRegMap.values()){
                if(!custRoleMap.containsKey(urs.id)){
                  system.debug('New records');
                        Employee_Profile__c urs2 = new Employee_Profile__c();
                        urs2.Profile_ID__c=urs.id;
                        urs2.name=urs.id;
                        urs2.Profile_Name__c=urs.name;
                       Employee_Profile_list_Insert.add(urs2); 
              
                }
                
            }
            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.
<Saket><Saket>
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.