• Jyothi Priya Jarang
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
I am tring to create parent (emp)object and child(training) object..PArent has D-M with another object (site). DEficiency is a staging object.
global class BatchStaging implements Database.Batchable<sObject> {
  
  global Database.QueryLocator start(Database.BatchableContext BC) {
    String query = 'SELECT id,Employee_Code__c,Account_Provisioning__c,Arbitration_Agreement__c,Basic_Info__c,BGC__c,Business_Coach__c,Classroom_Training__c,Compliance_Specialist__c,Current_Stage__c,Delivery_Attributes__c,Driver_License_Verification__c,Driver_Message__c,Driver_Record_Verification__c,Drug_Alcohol_test__c,DSP__c,First_Name__c,Form_Type__c,Invitation_Accepted__c,Last_Name__c,Location_Code__c,Location_Level_4__c,Message_Description__c,Middle_Initial__c,Photo_Upload__c,Provider_Info__c,Station__c,Status__c FROM Deficiency__c';
        
    return Database.getQueryLocator(query);
  }
  
  global void execute(Database.BatchableContext BC, List<Deficiency__c> scope)
    {
                     
                Map<string,Deficiency__c> newAccRecMap = new Map<string,Deficiency__c>();
            for(Deficiency__c stg : scope)
                  {
                      
                newAccRecMap.put(stg.Employee_Code__c,stg);
                newAccRecMap.put(stg.First_Name__c,stg);
                newAccRecMap.put(stg.Middle_Initial__c,stg);
                newAccRecMap.put(stg.Last_Name__c,stg);
                      
                //newAccRecMap.put(stg.First_Name__c + ''+stg.Middle_Initial__c +''+stg.Last_Name__c,stg); 
                      System.debug(newAccRecMap);
                }
                   
                List<Deficiency__c> List2=[select ID,Employee_Code__c,First_Name__c,Middle_Initial__c,Last_Name__c from Deficiency__c where Employee_Code__c IN: newAccRecMap.keySet() Limit 50000];
               System.debug(List2);
                if(List2.size() > 0)
                       {
                            for(Deficiency__c stg1 : List2)
                            {
                                if(newAccRecMap.containsKey(stg1.Employee_Code__c)&& newAccRecMap.containsKey(stg1.First_Name__c)&&newAccRecMap.containsKey(stg1.Middle_Initial__c)&& newAccRecMap.containsKey(stg1.Last_Name__c))
                                    
                                {
                                    System.debug(stg1);
                                    System.debug('duplicate');
                                }
                                                          
      
                            }
                       }
                        List<Employee__c> emp=new List<Employee__c>();
                            for(Deficiency__c stg2: scope)
                            {
                                System.debug(scope);
                                Employee__c emp1=new Employee__c();
                                
                                emp1.Employee_Code__c = stg2.Employee_Code__c;
                                //emp1.Id=stg2.id;
                                emp1.Location__c =stg2.Location_Code__c;
                                //emp1.Email__c='abc@gmail.com';
                               // emp1.Phone__c= 0000;
                                emp.add(emp1);
                                Sytem.debug('created parent');
                                System.debug(emp1.Location__c);
                                System.debug(stg2.Location_Level_4__c);
                            }
                            insert emp;
                            List<Training_Detail__c> Childs = new List<Training_Detail__c>();
                     
                                for(Deficiency__c stg3 : scope)
                                {
                                    System.debug(stg3);
                                   Training_Detail__c Child = new Training_Detail__c ();
                                   Child.Employee__c = stg3.Employee_Code__c;
                                  // Child.Name = stg3.Training_Name__c; 
                                   Childs.add(Child);  
                                    System.debug('created child');
                                }
                            
                                insert Childs;
    }

  global void finish(Database.BatchableContext BC)
    {
        System.debug('>>>Finish');
  }
  }
Can anyone help me out in writing batch apex that will create or update records.I never worked on batch apex so struggling here I have two objects
1) SIS_Staging__c
2) Contact
Both objects are having same field Siscode__c, so on inserting records in SIS_Staging__c, I need to check if any contact records are having same Siscode__c or not, if yes then I need to update else I need to create Contact.
I tried below code but when I run batch apex in anonymous window then I am getting below error.
"Constructor not defined: [BatchSISstagingObject].()"
My code is below.
 
global class BatchSISstagingObject implements Database.Batchable<sObject>{
    List <SIS_Staging__c> mapSisobject = new List <SIS_Staging__c> ();
    List <Contact> contactlist1 = new List <Contact> ();
    
    global BatchSISstagingObject(List <SIS_Staging__c> sisobjectUpdate) {
        mapSisobject=sisobjectUpdate;
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return DataBase.getQueryLocator([SELECT Id, SIS_Student_ID__c
                                         FROM Contact
                                        ]);
    }
    global void execute(Database.BatchableContext BC , List <Contact> contactlist) {
        for (SIS_Staging__c acct : mapSisobject){ 
            for (Contact con : contactList){
                if (con.SIS_Student_ID__c == acct.Name){
                        contactlist1.add(new Contact(
                            Id = con.Id,
                            FirstName = acct.First_Name__c,
                            LastName = acct.Last_Name__c
                           ));
                    }
            }   
        }
        
         update contactlist1;
    } 
    global void finish(Database.BatchableContext BC){
        
    }

}