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
Bob_zBob_z 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY & NVALID_CROSS_REFERENCE_KEY,

I am trying to deploy a trigger and a test class to my production org and I'm recieving a couple of errors. Below is the error lesssage I recieved while using Eclipse to deploy them.
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]

Although the issue is probalby something simple to fixe I've tried several different changes with no success. I have the Trigger and the Apex test class below. If anyone could help me with this issue I would really appreciate it.

Test Class:
@isTest
private class TestAfterPOLogUpdateTest {

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

      Opportunity opp = new Opportunity( Name ='Bob Inc.',polgid__c ='a4C39000000QFFiEAO', AccountId ='0017000000NAEsZ',StageName='Quoting',Who_is_quoting__c='Inside Sales', Type='EOAT', LeadSource ='NPE 2018', Describe_what_you_need_quoted__c='I need this test to work', Product_Type__c='Robot',   CloseDate=System.today() );
          
          insert opp;
          
          opp.polgid__c ='a4C39000000QFFiEAO';
          opp.OwnerId ='0053900000614sS';
          opp.Who_is_quoting__c='Inside Sales';
          opp.Product_Type__c='Conveyor';
          
          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.Model__c='';
          yus.Product_Detail_Report__c = 'EOAT-SM';
          yus.Discharge_Direction__c ='Clamp Traverse';
       	  
          insert yus;
          
          
          yus.Equipment_Type__c = 'Servo Trav 600 and under';
          yus.Model__c='RCII-100S-11-8.5';
          yus.Product__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 = 'EOAT-SM';
          yus.Discharge_Direction__c ='Clamp Traverse';
       	
          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='';
          update yus;
           
          
          yus.Equipment_Type__c = 'Safety Guarding';
          yus.Product_Detail_Report__c = 'AUTO-SM';
          update yus;
           
          yus.Equipment_Type__c = 'Conveyor';
          update yus;
           
          yus.Equipment_Type__c = 'Servo Trav over 600';
          update yus;
           
          yus.Equipment_Type__c = 'Adapter Plate';
          update yus;
           
          yus.Equipment_Type__c = 'A-Axis';
          update yus;
          
          yus.Equipment_Type__c = 'B-Axis';
          update yus;
           
          yus.Equipment_Type__c = 'Freight';
          update yus;
           
          yus.Equipment_Type__c = 'Degating Station';
          update yus;
           
          yus.Equipment_Type__c = 'Installation';
          update yus;
           
          yus.Equipment_Type__c = 'Hybrid';
          update yus;
           
          yus.Equipment_Type__c = 'Software';
          update yus;
           
          yus.Equipment_Type__c = 'Servo Traverse Super Large 1300 and greater';
          update yus;
           
          yus.Equipment_Type__c = 'Spacer';
          update yus;
           
          yus.Equipment_Type__c = 'Stanchion';
          update yus;
          
          yus.Equipment_Type__c = 'Robot';
          update yus;
           
          yus.Equipment_Type__c = 'System - SMALL';
          update yus;
           
          yus.Equipment_Type__c = 'Collaborative robot';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (horz)';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (Vert)';
          update yus;
           
          yus.Equipment_Type__c = 'Parts';
          update yus;
           


      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);
}


 
Raj VakatiRaj Vakati
Insted of hardcoding the record type id set  it with query as below in the trigger and update the test class also 

Repeat the same step for the record types in trigger and test class
 
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<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Opportunity.getRecordTypeInfosByName();

Id standardRecTypeId = recordTypes.get('Parent').getRecordTypeId();
Id customRecTypeId = recordTypes.get('My Record Type Display Name').getRecordTypeId();



    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=standardRecTypeId;               
                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=customRecTypeId;               
                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);
}


 
@isTest
private class TestAfterPOLogUpdateTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        test.startTest();
		
		Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Account.getRecordTypeInfosByName();

Id standardRecTypeId = recordTypes.get('Parent').getRecordTypeId();
Id customRecTypeId = recordTypes.get('My Record Type Display Name').getRecordTypeId();



      Opportunity opp = new Opportunity( Name ='Bob Inc.',polgid__c ='a4C39000000QFFiEAO', AccountId ='0017000000NAEsZ',StageName='Quoting',Who_is_quoting__c='Inside Sales', Type='EOAT', LeadSource ='NPE 2018', Describe_what_you_need_quoted__c='I need this test to work', Product_Type__c='Robot',   CloseDate=System.today() );
          
          insert opp;
          
          opp.polgid__c ='a4C39000000QFFiEAO';
          opp.OwnerId ='0053900000614sS';
          opp.Who_is_quoting__c='Inside Sales';
          opp.Product_Type__c='Conveyor';
          opp.RecordTypeId= customRecTypeId.Id ;
          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.Model__c='';
          yus.Product_Detail_Report__c = 'EOAT-SM';
          yus.Discharge_Direction__c ='Clamp Traverse';
       	  
          insert yus;
          
          
          yus.Equipment_Type__c = 'Servo Trav 600 and under';
          yus.Model__c='RCII-100S-11-8.5';
          yus.Product__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 = 'EOAT-SM';
          yus.Discharge_Direction__c ='Clamp Traverse';
       	
          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='';
          update yus;
           
          
          yus.Equipment_Type__c = 'Safety Guarding';
          yus.Product_Detail_Report__c = 'AUTO-SM';
          update yus;
           
          yus.Equipment_Type__c = 'Conveyor';
          update yus;
           
          yus.Equipment_Type__c = 'Servo Trav over 600';
          update yus;
           
          yus.Equipment_Type__c = 'Adapter Plate';
          update yus;
           
          yus.Equipment_Type__c = 'A-Axis';
          update yus;
          
          yus.Equipment_Type__c = 'B-Axis';
          update yus;
           
          yus.Equipment_Type__c = 'Freight';
          update yus;
           
          yus.Equipment_Type__c = 'Degating Station';
          update yus;
           
          yus.Equipment_Type__c = 'Installation';
          update yus;
           
          yus.Equipment_Type__c = 'Hybrid';
          update yus;
           
          yus.Equipment_Type__c = 'Software';
          update yus;
           
          yus.Equipment_Type__c = 'Servo Traverse Super Large 1300 and greater';
          update yus;
           
          yus.Equipment_Type__c = 'Spacer';
          update yus;
           
          yus.Equipment_Type__c = 'Stanchion';
          update yus;
          
          yus.Equipment_Type__c = 'Robot';
          update yus;
           
          yus.Equipment_Type__c = 'System - SMALL';
          update yus;
           
          yus.Equipment_Type__c = 'Collaborative robot';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (horz)';
          update yus;
           
          yus.Equipment_Type__c = 'Side-entry (Vert)';
          update yus;
           
          yus.Equipment_Type__c = 'Parts';
          update yus;
           


      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();
    }
}

 
Bob_zBob_z
Thank you Raj !
I will give it ago.
 
Raj VakatiRaj Vakati
Will its wokrked ? You need to change both apex trigger and test class 
Bob_zBob_z
I was able to fix this error
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].  (I had the wrong ID in the test class)  I havent been able to resolve the TestAfterPOLogUpdateTest.myUnitTest System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggerNewYushinProd: execution of AfterInsert. 
Raj VakatiRaj Vakati
Change your trigger to before insert , before update as below 


trigger TriggerNewYushinProd on Opportunity ( after insert, after update) {

}
Raj VakatiRaj Vakati
After insert the records will be read only  ..
 
trigger TriggerNewYushinProd on Opportunity ( before insert, before 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);
}

 
Bob_zBob_z
I changed the trigger to before insert before update and I am getting the same error from line 8
*** Deployment Log ***
Result: FAILED
Date: September 14, 2018 11:16:03 AM EDT

# Deployed From:
   Project name: Bob Sandbox
   Username: bpoliquin@yushin.com.bptest
   Endpoint: login.salesforce.com

# Deployed To:
   Username: bpoliquin@yushin.com
   Endpoint: login.salesforce.com

# Deploy Results:
   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/TriggerNewYushinProd.trigger
   Full Name:  TriggerNewYushinProd
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

# Test Results:

Run Failures:
  TestAfterPOLogUpdateTest.myUnitTest System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggerNewYushinProd: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.TriggerNewYushinProd: line 8, column 1: []

 
Bob_zBob_z
I've tried a couple different changes to my apex test and I am still getting the same results.

I tried changing the code to below, Knowing the test would probably fail. It did and it gave me the same error message as it does when i add the fields in the test to insert as you can see below. So I'm not sure how to get this to work. 

No matter what i try it doesnt like Trigger.TriggerNewYushinProd: line 8, column 1: []

 
test.startTest();

      Opportunity opp = new Opportunity();
      
   
   
          insert opp;



Insert with the fields:
 
test.startTest();

      Opportunity opp = new Opportunity();
      
    opp.Name ='Bob Inc.';
    opp.AccountId ='0017000000NAEsZ';
    opp.StageName='Quoting';
    opp.Who_is_quoting__c='Inside Sales';
    opp.Type='EOAT';
    opp.LeadSource ='NPE 2018';
    opp.Describe_what_you_need_quoted__c='I need this test to work';
    opp.Product_Type__c='Robot';
    opp.CloseDate=System.today();
   
          insert opp;

Run Failures:
  TestAfterPOLogUpdateTest.myUnitTest System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggerNewYushinProd: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.TriggerNewYushinProd: line 8, column 1: []



 
Bob_zBob_z
Good morning Raj, 

I was wonder since you help me out with this trigger if you could help me add a qaunity field in my trigger. I have a new fields i just created entitled "Robot_Qauntity__c"  "EOAT_Qauntity__c" "Software_Qauntity__c". What i'm trying to do is add them into the trigger so when i user select robot they select how many products to create of that particular product. I'm not quite sure if i can add this to the trigger or how to add it.  Any help would be greatly appreciated.