[SalesForce] CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY – INVALID_CROSS_REFERENCE_KEY

I'm also getting a the following error from my apex test and trigger while validating. The error,test class and trigger are below.

Run Failures: TestAfterPOLogUpdateTest.myUnitTest System.DmlException:
Insert failed. First exception on row 0; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggerNewYushinProd: execution
of AfterInsert caused by: System.DmlException: Insert failed. First
exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record
Type ID: this ID value isn't valid for the user: 012180000004huPAAQ:
[RecordTypeId]

Test Class:

    @IsTest
public class TestAfterPOLogUpdateTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        test.startTest();

        PageReference pageRef = Page.OppTabDetailinsidesales2;
           
      Opportunity opp = new Opportunity();
        
          opp.Name ='Bob Inc.';
          opp.AccountId ='0017000000NAEsZ';
          opp.StageName='Quoting';
          opp.Who_is_quoting__c='Inside Sales';
          opp.CloseDate=System.today();
   
          insert opp;
          
        
          opp.Name ='Bob Inc.';
          opp.AccountId ='0017000000NAEsZ';
          opp.StageName='Quoting';
          opp.Who_is_quoting__c='Regional Manager';
          opp.CloseDate=System.today();
    
          update opp;
          
          Yushin_Product__c yus = new Yushin_Product__c ();
          yus.Account__c ='0017000000NAEsZ';
          yus.Opportunity__c='0063900000sutn9';
          yus.PO_Log__c='a4C39000000QFKO';
          yus.Equipment_Type__c= 'A-Axis';
         
          yus.Product_Detail_Report__c = 'EOAT-SM';
          yus.Discharge_Direction__c ='Clamp Traverse';
          yus.RecordTypeId='012390000006Dvb';
          
          
          insert yus;
          
               
          yus.Equipment_Type__c = 'Servo Trav 600 and under';
          yus.Model__c='RCII-100S-11-8.5';
          yus.Price__c =45080;
          yus.IMM_Mfr__c = 'Toshiba';
          yus.Status__c ='Sold';
          yus.Booked_Date__c = Date.newInstance(2016, 12, 9);
          yus.PO_Log__c='a4C39000000QFKO';
          yus.Discharge_Direction__c ='Clamp Traverse';
          yus.Product_Detail_Report__c = 's-R';
          yus.RecordTypeId='012390000006Dve';
        
          update yus;
          
          
          
          yus.Equipment_Type__c = 'Installation';
          yus.Product_Detail_Report__c = 'Installation';
          update yus;
          
          
          yus.Equipment_Type__c = 'EOAT';
          yus.Price__c = 499;
          yus.Model__c='HOP 750';
          update yus;
           
          
          yus.Equipment_Type__c = 'Safety Guarding';
          yus.Product_Detail_Report__c = 'AUTO-SM';
          yus.RecordTypeId='012390000006Dvb';
          update yus;
           
          yus.Equipment_Type__c = 'Conveyor';
          yus.Price__c =45080;
          yus.Status__c ='Sold';
          yus.Date_Booked__c = Date.newInstance(2016, 12, 9);
          yus.Date_Entered__c = Date.newInstance(2016, 12, 8);
          yus.Quantity__c= 1;
          yus.PO_Log__c='a4C39000000QFKO';
          yus.Product_Detail_Report__c = 'AUTO-SM';
          yus.RecordTypeId='012390000006DvZ';
          update yus;
           
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'Servo Trav over 600';
          yus.Model__c='FRA-1530S-22-11-C1  ';
          yus.RecordTypeId='012390000006Dve';
          update yus;
           
         
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'Adapter Plate';
          yus.Product_Detail_Report__c='Misc. Item';
          update yus;
           
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'A-Axis';
          yus.Product_Detail_Report__c='EOAT-SM';
          yus.RecordTypeId='012390000006Dva';
          update yus;
          
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'B-Axis';
          yus.RecordTypeId='012390000006Dva';
          update yus;
           
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'Freight';
          yus.Product_Detail_Report__c='Freight';
          update yus;
           
          yus.Price__c =45080;
          yus.Equipment_Type__c = 'Degating Station';
          yus.Product_Detail_Report__c='AUTO-SM';
          update yus;
           
          yus.Equipment_Type__c = 'Installation';
          yus.Product_Detail_Report__c='Installation';
          update yus;
           
          yus.Equipment_Type__c = 'Hybrid';
          yus.Product_Detail_Report__c='SM-Hybrid';
          yus.RecordTypeId='012390000006Dve';
          
          update yus;
           
          yus.Equipment_Type__c = 'Software';
          yus.Product_Detail_Report__c='Software';
          update yus;
           
          yus.Equipment_Type__c = 'Servo Traverse Super Large 1300 and greater';
          yus.Model__c='RAII-a-3000SL';
          yus.RecordTypeId='012390000006Dve';
          update yus;
           
          yus.Equipment_Type__c = 'Spacer';
          yus.Product_Detail_Report__c='Misc. Item';
          update yus;
           
          yus.Equipment_Type__c = 'Stanchion';
          update yus;
          
          yus.Equipment_Type__c = 'Servo Trav over 600';
          yus.Model__c='YCII-800D-30-18';
          yus.Product_Detail_Report__c='LG-YC';
          yus.RecordTypeId='012390000006Dve';
          update yus;
           
          yus.Equipment_Type__c = 'System - SMALL';
          yus.Product_Detail_Report__c='AUTO-SM';
          update yus;
           
          yus.Equipment_Type__c = 'Collaborative robot';
          yus.Model__c='OB7';
          yus.Product_Detail_Report__c='SPECIAL';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (horz)';
          yus.Model__c='SVR B50';
          yus.Product_Detail_Report__c='SM-SE';
          yus.RecordTypeId='012390000006Dve';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (Vert)';
          yus.Product_Detail_Report__c='SM-SE';
          yus.RecordTypeId='012390000006Dve';
          update yus;
           
          yus.Equipment_Type__c = 'Parts';
          yus.Product_Detail_Report__c='Misc. Item';
          update yus;
          
          yus.Equipment_Type__c = 'Safety Guarding';
          yus.Product_Detail_Report__c='AUTO-SM';
          yus.RecordTypeId='012390000006Dvb';
          update yus;
           
           
           
         Request_for_Special_Payment__c rsp = new Request_for_Special_Payment__c ();  

            rsp.Opportunity_Name__c = '0063900000sutn9';
            rsp.Requestor_Submit_for_Approval__c = true;
            rsp.Request_Submitted_By__c = 'Bob Poliquin';
            rsp.Requestor_Date_Submitted__c=System.today();
            
            insert rsp;
            
            
            rsp.Opportunity_Name__c = '0063900000sutn9';
            rsp.Supervisor_Approved__c = true;
            rsp.Rejected_By__c='Chris Parillo';
            rsp.Supervisor_Approval_Date__c=System.today();
            
            update rsp;
            
            

      PO_Log__c pol = new PO_Log__c( Customer__c='0017000000NAEsZ',Opportunity__c='0063900000sutn9',Yushin_Product__c='a0639000020MH67', Order_Type__c='EOAT' );
      insert pol;
      
          pol.Opportunity__c='0063900000sutn9';
          pol.PO__c = '12345';
          pol.Sales_Order_Number__c = '12453';
          pol.Order_Type__c = 'Robot';
          pol.AddPLogOpps__c = true;
          pol.Notes__c= 'text';
          pol.Order_Configuration__c = 'ATC';
          pol.Sales_Order_Number__c = '189345';
          pol.AddPLogOpps__c = true;
      
          update pol;
          
          

      

      test.stopTest();
    }
}

Trigger:

   trigger TriggerNewYushinProd on Opportunity ( after insert, after update) {
    
    List<Yushin_Product__c> yus = new List<Yushin_Product__c>();
    List<Yushin_Product__c> yusToDelete = new List<Yushin_Product__c>();
    
    Map<Id, List<Yushin_Product__c>> existingYusMap = new Map<Id, List<Yushin_Product__c>>();
    
    for(Yushin_Product__c yu :[SELECT Id, Opportunity__c, Account__c, Equipment_Type__c from Yushin_Product__c where Opportunity__c IN :Trigger.newMap.keyset()]){
        List<Yushin_Product__c> yushinProducts = existingYusMap.get(yu.Opportunity__c);
        if (yushinProducts == null) {
            yushinProducts = new List<Yushin_Product__c>();
        }
        yushinProducts.add(yu);
        existingYusMap.put(yu.Opportunity__c, yushinProducts);
    }
    
    
    
    for(Opportunity o : Trigger.new)  {
        if (Trigger.IsInsert || Trigger.oldMap.get(o.Id).Product_Type__c == null) {
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Robot'))               
            {                
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Robot'; 
                y.RecordTypeId='012180000004huP';               
                yus.add(y);
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains( 'EOAT'))                
            {
                
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='EOAT';
                y.RecordTypeId='012180000004huU';               
                yus.add(y);
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Conveyor'))               
            {                
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Conveyor';
                y.RecordTypeId='012180000004huZ';
                yus.add(y);
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Other'))               
            {                
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Other';
                yus.add(y);
            }
        }
        else if (Trigger.IsUpdate && Trigger.oldMap.get(o.Id).Product_Type__c != null) {
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Robot') && !Trigger.oldMap.get(o.Id).Product_Type__c.contains('Robot'))
            {
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Robot';
                y.RecordTypeId='012180000004huP'; 
                yus.add(y);
            }
            
            if((o.Product_Type__c == null || !o.Product_Type__c.contains('Robot')) && Trigger.oldMap.get(o.Id).Product_Type__c.contains('Robot'))
            {
                List<Yushin_Product__c> yPs = existingYusMap.get(o.Id);
                for (Yushin_Product__c yP : yPs) {
                    if (yP.Equipment_Type__c == 'Robot') {
                        yusToDelete.add(yP);
                    }
                }
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains('EOAT') && !Trigger.oldMap.get(o.Id).Product_Type__c.contains('EOAT'))
            {                    
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='EOAT';  
                y.RecordTypeId='012180000004huU';                 
                yus.add(y);
            }
            
            if((o.Product_Type__c == null || !o.Product_Type__c.contains('EOAT')) && Trigger.oldMap.get(o.Id).Product_Type__c.contains('EOAT'))
            {
                List<Yushin_Product__c> yPs = existingYusMap.get(o.Id);
                for (Yushin_Product__c yP : yPs) {
                    if (yP.Equipment_Type__c == 'EOAT') {
                        yusToDelete.add(yP);
                    }
                }
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Conveyor') && !Trigger.oldMap.get(o.Id).Product_Type__c.contains('Conveyor'))                    
            {                    
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Conveyor';
                y.RecordTypeId='012180000004huZ';
                yus.add(y);
            }
            
            if((o.Product_Type__c == null || !o.Product_Type__c.contains('Conveyor')) && Trigger.oldMap.get(o.Id).Product_Type__c.contains('Conveyor'))
            {
                List<Yushin_Product__c> yPs = existingYusMap.get(o.Id);
                for (Yushin_Product__c yP : yPs) {
                    if (yP.Equipment_Type__c == 'Conveyor') {
                        yusToDelete.add(yP);
                    }
                }
            }
            
            if(o.Product_Type__c != null && o.Product_Type__c.contains('Safety Guarding') && !Trigger.oldMap.get(o.Id).Product_Type__c.contains('Safety Guarding'))                    
            {                    
                Yushin_Product__c y=new Yushin_Product__c();
                y.Opportunity__c=o.Id;
                y.Account__c=o.AccountId; 
                y.Equipment_Type__c='Safety Guarding';
                y.RecordTypeId='012180000004hue';
                yus.add(y);
            }
            
            if((o.Product_Type__c == null || !o.Product_Type__c.contains('Safety Guarding')) && Trigger.oldMap.get(o.Id).Product_Type__c.contains('Safety Guarding'))
            {
                List<Yushin_Product__c> yPs = existingYusMap.get(o.Id);
                for (Yushin_Product__c yP : yPs) {
                    if (yP.Equipment_Type__c == 'Safety Guarding') {
                        yusToDelete.add(yP);
                    }
                }
            }
        }
    }  
 
    if(yus.size()>0)
        insert yus;
    if(yusToDelete.size()>0)
        database.delete(yusToDelete);
}

Best Answer

As I can see in your code you have hardcoded the ID of records and Record types, this is a bit problematic.

Now the code you have written references recordTypeID. Record type you created in your Sandbox will have different ID than the one you created in Live.

Thus while validating, it gives you that exception that it could not find the(Sandbox recordtype Id) in the prod environment.

You have to remove hardcoding using custom lables or Describe generic method.