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
Jyothi Priya JarangJyothi Priya Jarang 

getting an error:FIELD_INTEGRITY_EXCEPTION, Location: id value of incorrect type: a0Dc000000i9msBEAQ: [Location__c] 07:16:38.0 (18256011)|HEAP_ALLOCATE|[56]|Bytes:156

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');
  }
  }
AbhishekAbhishek (Salesforce Developers) 
Try the suggestions as mentioned below article,
https://help.formassembly.com/help/salesforce-error-id-value-of-incorrect-type


For further reference check the below too,
https://salesforce.stackexchange.com/questions/93564/getting-this-error-field-integrity-exception-contract-id-value-of-incorrect-t


I hope it helps.
pk1772pk1772
From the error message, it appears Location__c is a custom lookup field. You are assigning Location_Code__c in this line from staging object.
emp1.Location__c =stg2.Location_Code__c; 
This assignment might be incorrect, not exactly pointing to the object which Location__c field is pointing. Best way to verify this is to open the ID in error in your browser and check if it refers to the same object that is intended to be.