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
Patrick FoyPatrick Foy 

Trying to convert Apex Query List to string not sure if this is correct

I am trying to execute a SOQL query that may return a list. If list is null I want to set default value. I am new to apex so not sure if this is correct.
 
// SET USERNAME STRING
string username = '%'+UserInfo.getFirstName()+'%'; 
//SET DEFAULT PRODUCTid
Id ProductID = '01t46000000nPO0AAM'; 
//MAP  IDMAPPRODUCT2
Map<Id> idmapproduct2 = new Map<Id>([Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like:username]); system.debug(idmapproduct2); 
//CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
if(idmapproduct2 != null)
  {ProductID = idmapproduct2;}

 
Best Answer chosen by Patrick Foy
Steven NsubugaSteven Nsubuga
Made a small change as far as getting the Child.RecordTypeId is concerned.
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update){
    if (trigger.isAfter){
        if (trigger.isUpdate){
			List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =Schema.SObjectType.OptimaPro__Work_Order_Line__c.getRecordTypeInfosByName().get('Labor').getRecordTypeId();
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);  
                }
            }
			insert Childs;
        }
    }
}

 

All Answers

Abdul KhatriAbdul Khatri
Here is the code with List
 
// SET USERNAME STRING
string username = '%'+UserInfo.getFirstName()+'%'; 
//SET DEFAULT PRODUCTid
Id ProductID = '01t46000000nPO0AAM'; 
//MAP  IDMAPPRODUCT2
List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
system.debug(idmapproduct2); 
//CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
if(idmapproduct2 != null)
    ProductID = idmapproduct2.get(0).Id;

 
Patrick FoyPatrick Foy
Steven, I am getting Expression must be a list type: Set on Line #09 ?
Steven NsubugaSteven Nsubuga
Please use Abdul's code Patrick.
Patrick FoyPatrick Foy
Abdul,

I am getting no problems but received this error : System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WorkOrderCheckIN2: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0 Trigger.WorkOrderCheckIN2: line 15, column 1: []
Steven NsubugaSteven Nsubuga
Patrick, I suggest you post all the code here so we can be clued in on how everything works on your end.
Patrick FoyPatrick Foy
Sure thing I will get you my trigger and class I left your code in.
 
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update, after insert){
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null)
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =[select Id from RecordType where Name = 'Labor'].id ;
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);
                    
                    insert Childs;
                }
            }
        }
    }
}
 
@isTest
private class WorkOrderCheckINTest2 {
    public static testMethod void WorkOrderCheckINTest2Method()
    {
        User u3 = [SELECT Id FROM User WHERE FirstName='Zach'];
        System.runAs(u3) {
            Account testAccount = new Account();
            testAccount.Name='Test Account';
            
            insert testAccount;
            
            OptimaPro__Work_Order__c testWorkOrder = new OptimaPro__Work_Order__c();
            testWorkOrder.OptimaPro__MOBSVC_Account__c= testAccount.id;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Equipment_Hour_Reading__c=1300;
            testWorkOrder.Final_Amps_Reading__c=12;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Initial_Pressure_Reading_PSI__c=1000;
            testWorkOrder.Initial_Spike_Reading_PSI__c=1800;
            testWorkOrder.Initial_Volume_Reading__c=4.0;
            testWorkOrder.Motor_Amps_Reading__c=12.25;
            testWorkOrder.Pump_Pressure_Reading_PSI__c=1200;
            testWorkOrder.Pump_Volume_Reading__c=5.5;
            testWorkOrder.What_Size_Nozzle_Is_Being_Used__c=4.0;
            testWorkOrder.Work_Preformed__c= 'test';
            testWorkOrder.Check_In__c = False;
            
            insert testWorkOrder;
            
           
            testWorkOrder.Check_In__c = True;
            update testWorkOrder;
            
        }
    }
}

 
Steven NsubugaSteven Nsubuga
The issue seems to be in your test class, you needed to create a Product2 record for use within the test.
@isTest
private class WorkOrderCheckINTest2 {
    public static testMethod void WorkOrderCheckINTest2Method()
    {
        User u3 = [SELECT Id FROM User WHERE FirstName='Zach'];
        System.runAs(u3) {
            Account testAccount = new Account();
            testAccount.Name='Test Account';
            
            insert testAccount;
			
			Product2 prod = new Product2(Name = 'Some Product', 
                                    ProductCode = ' theLABproduct');
			insert prod;

            
            OptimaPro__Work_Order__c testWorkOrder = new OptimaPro__Work_Order__c();
            testWorkOrder.OptimaPro__MOBSVC_Account__c= testAccount.id;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Equipment_Hour_Reading__c=1300;
            testWorkOrder.Final_Amps_Reading__c=12;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Initial_Pressure_Reading_PSI__c=1000;
            testWorkOrder.Initial_Spike_Reading_PSI__c=1800;
            testWorkOrder.Initial_Volume_Reading__c=4.0;
            testWorkOrder.Motor_Amps_Reading__c=12.25;
            testWorkOrder.Pump_Pressure_Reading_PSI__c=1200;
            testWorkOrder.Pump_Volume_Reading__c=5.5;
            testWorkOrder.What_Size_Nozzle_Is_Being_Used__c=4.0;
            testWorkOrder.Work_Preformed__c= 'test';
            testWorkOrder.Check_In__c = False;
            
            insert testWorkOrder;
            
           
            testWorkOrder.Check_In__c = True;
            update testWorkOrder;
        }
    }
}

 
Patrick FoyPatrick Foy
Steve,

thank you so much for working with me on this. So I am still getting a error.
 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WorkOrderCheckIN2: execution of AfterInsert

caused by: System.ListException: List index out of bounds: 0

Trigger.WorkOrderCheckIN2: line 15, column 1: []

Stack trace
 
Class.WorkOrderCheckINTest2.WorkOrderCheckINTest2Method: line 32, column 1

I'm sorry I just feel a little incompetent here I have beel all over high and low trying to fix this and no luck I keep hitting same issue.
Patrick FoyPatrick Foy
Here is a screen shot of debug. 
I was hopeful that it would emulate this query

Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like '%ZACH%'
That query when I run it in query editor returns 1
I'm not sure the correct value is being passed into username could that be where my break is?

User-added image
Steven NsubugaSteven Nsubuga
The query in the code does not return anything!! Not a single product is returned. That's what we must fix. 
Try this
@isTest
private class WorkOrderCheckINTest2 {
    public static testMethod void WorkOrderCheckINTest2Method()
    {
		
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u3 = new User(Alias = 'Zach', Email='Zach@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Zach', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName='ZachLAB@testorg.com');
		
        System.runAs(u3) {
            Account testAccount = new Account();
            testAccount.Name='Test Account';
            
            insert testAccount;
			
			Product2 prod = new Product2(Name = 'Some Product', 
                                    ProductCode = ' theLABproduct');
			insert prod;

            
            OptimaPro__Work_Order__c testWorkOrder = new OptimaPro__Work_Order__c();
            testWorkOrder.OptimaPro__MOBSVC_Account__c= testAccount.id;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Equipment_Hour_Reading__c=1300;
            testWorkOrder.Final_Amps_Reading__c=12;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Initial_Pressure_Reading_PSI__c=1000;
            testWorkOrder.Initial_Spike_Reading_PSI__c=1800;
            testWorkOrder.Initial_Volume_Reading__c=4.0;
            testWorkOrder.Motor_Amps_Reading__c=12.25;
            testWorkOrder.Pump_Pressure_Reading_PSI__c=1200;
            testWorkOrder.Pump_Volume_Reading__c=5.5;
            testWorkOrder.What_Size_Nozzle_Is_Being_Used__c=4.0;
            testWorkOrder.Work_Preformed__c= 'test';
            testWorkOrder.Check_In__c = False;
            
            insert testWorkOrder;
            
           
            testWorkOrder.Check_In__c = True;
            update testWorkOrder;
        }
    }
}

 
Patrick FoyPatrick Foy
Steve

I tried that and still no success here is log. 

Username is null and products both are not working 

User-added image
Steven NsubugaSteven Nsubuga
I am persistent, try this
@isTest
private class WorkOrderCheckINTest2 {
    public static testMethod void WorkOrderCheckINTest2Method()
    {
		
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u3 = new User(Alias = 'Zach', Email='Zach@testorg.com',
        EmailEncodingKey='UTF-8', FirstName='Zach', LastName='Zach',LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName='ZachLAB@testorg.com');
		
        System.runAs(u3) {
            Account testAccount = new Account();
            testAccount.Name='Test Account';
            
            insert testAccount;
			
			Product2 prod = new Product2(Name = 'Some Product', 
                                    ProductCode = ' theZachLABproduct');
			insert prod;

            
            OptimaPro__Work_Order__c testWorkOrder = new OptimaPro__Work_Order__c();
            testWorkOrder.OptimaPro__MOBSVC_Account__c= testAccount.id;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Equipment_Hour_Reading__c=1300;
            testWorkOrder.Final_Amps_Reading__c=12;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Initial_Pressure_Reading_PSI__c=1000;
            testWorkOrder.Initial_Spike_Reading_PSI__c=1800;
            testWorkOrder.Initial_Volume_Reading__c=4.0;
            testWorkOrder.Motor_Amps_Reading__c=12.25;
            testWorkOrder.Pump_Pressure_Reading_PSI__c=1200;
            testWorkOrder.Pump_Volume_Reading__c=5.5;
            testWorkOrder.What_Size_Nozzle_Is_Being_Used__c=4.0;
            testWorkOrder.Work_Preformed__c= 'test';
            testWorkOrder.Check_In__c = False;
            
            insert testWorkOrder;
            
           
            testWorkOrder.Check_In__c = True;
            update testWorkOrder;
        }
    }
}

 
Patrick FoyPatrick Foy
Steve,

We are half way username did populatecorrectly this time. query still failed. I'm also kind of curious because even if the query returns null it should use the variable I set. 
Steven NsubugaSteven Nsubuga
We missed something!! The wrong check was used.
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update, after insert){
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =[select Id from RecordType where Name = 'Labor'].id ;
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);
                    
                    insert Childs;
                }
            }
        }
    }
}

Try with that trigger, checking for null is not enough, we have to check if the list is empty. The disadvantage with this is that the ID is hardcded and will probably not be the same in the production instance.
Steven NsubugaSteven Nsubuga
quick update
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update, after insert){
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB%' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =[select Id from RecordType where Name = 'Labor'].id ;
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);
                    
                    insert Childs;
                }
            }
        }
    }
}

 
Patrick FoyPatrick Foy
I tested and changed one thing off of what you just sent.I put spaces in product code and we found it.  Between us we just got that to go a whole lot farther.

I do have a new error:

 System.DmlException: Update failed. First exception on row 0 with id a130t000000DYSFAA4; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WorkOrderCheckIN2: execution of AfterUpdate

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OptimaPro.MOBSVC_WorkOrderLine: execution of AfterInsert

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

Class.OptimaPro.MOBSVC_InstalledProduct_WorkOrderLine.createInstalledProduct: line 48, column 1
Trigger.OptimaPro.MOBSVC_WorkOrderLine: line 11, column 1: []

Trigger.WorkOrderCheckIN2: line 25, column 1: []

Stack trace 
Class.WorkOrderCheckINTest2.WorkOrderCheckINTest2Method: line 44, column 1
 
@isTest
private class WorkOrderCheckINTest2 {
    public static testMethod void WorkOrderCheckINTest2Method()
    {
		
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u3 = new User(Alias = 'Zach', Email='Zach@testorg.com',
        EmailEncodingKey='UTF-8', FirstName='Zach', LastName='Zach',LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName='ZachLAB@testorg.com');
		
        System.runAs(u3) {
            Account testAccount = new Account();
            testAccount.Name='Test Account';
            
            insert testAccount;
			
			Product2 prod = new Product2(Name = 'Some Product', 
                                    ProductCode = ' the Zach LAB product');
			insert prod;

            
            OptimaPro__Work_Order__c testWorkOrder = new OptimaPro__Work_Order__c();
            testWorkOrder.OptimaPro__MOBSVC_Account__c= testAccount.id;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Equipment_Hour_Reading__c=1300;
            testWorkOrder.Final_Amps_Reading__c=12;
            testWorkOrder.Incoming_Voltage_Reading__c=230;
            testWorkOrder.Initial_Pressure_Reading_PSI__c=1000;
            testWorkOrder.Initial_Spike_Reading_PSI__c=1800;
            testWorkOrder.Initial_Volume_Reading__c=4.0;
            testWorkOrder.Motor_Amps_Reading__c=12.25;
            testWorkOrder.Pump_Pressure_Reading_PSI__c=1200;
            testWorkOrder.Pump_Volume_Reading__c=5.5;
            testWorkOrder.What_Size_Nozzle_Is_Being_Used__c=4.0;
            testWorkOrder.Work_Preformed__c= 'test';
            testWorkOrder.Check_In__c = False;
            
            insert testWorkOrder;
            
           
            testWorkOrder.Check_In__c = True;
            update testWorkOrder;
        }
    }
}

 
Steven NsubugaSteven Nsubuga
Try this
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update, after insert){
    if (trigger.isAfter){
        if (trigger.isUpdate || trigger.isInsert){
			List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =[select Id from RecordType where Name = 'Labor'].id ;
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);  
                }
            }
			insert Childs;
        }
    }
}

 
Patrick FoyPatrick Foy
Tired that and a few other thinds still not getting anything. I changed the code to below pulled out after inset and get thid error:

System.DmlException: Update failed. First exception on row 0 with id a130t000000DYSjAAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, WorkOrderCheckIN2: execution of AfterUpdate

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OptimaPro.MOBSVC_WorkOrderLine: execution of AfterInsert

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

Class.OptimaPro.MOBSVC_InstalledProduct_WorkOrderLine.createInstalledProduct: line 48, column 1
Trigger.OptimaPro.MOBSVC_WorkOrderLine: line 11, column 1: []

Trigger.WorkOrderCheckIN2: line 26, column 1: []

Stack:

Class.WorkOrderCheckINTest2.WorkOrderCheckINTest2Method: line 43, column 1
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update){
    if (trigger.isAfter){
        if (trigger.isUpdate){
			List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =[select Id from RecordType where Name ='Labor'].id ;
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);  
                }
            }
			insert Childs;
        }
    }
}
Steven NsubugaSteven Nsubuga
Made a small change as far as getting the Child.RecordTypeId is concerned.
trigger WorkOrderCheckIN2 on OptimaPro__Work_Order__c (after update){
    if (trigger.isAfter){
        if (trigger.isUpdate){
			List<OptimaPro__Work_Order_Line__c> Childs = new List<OptimaPro__Work_Order_Line__c>();
            for (OptimaPro__Work_Order__c newWorkOrder:Trigger.new) {
                // SET USERNAME STRING
                string username = '%'+UserInfo.getFirstName()+'%'; 
                //SET DEFAULT PRODUCTid
                Id ProductID = '01t46000000nPO0AAM'; 
                //MAP  IDMAPPRODUCT2
                List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like :username]; 
                system.debug(idmapproduct2); 
                //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
                if(idmapproduct2 != null || idmapproduct2.isEmpty())
                    ProductID = idmapproduct2.get(0).Id;
                if(newWorkOrder.Check_In__c ==true){
                    OptimaPro__Work_Order_Line__c Child = new OptimaPro__Work_Order_Line__c();  
                    Child.RecordTypeId =Schema.SObjectType.OptimaPro__Work_Order_Line__c.getRecordTypeInfosByName().get('Labor').getRecordTypeId();
                    Child.OptimaPro__MOBSVC_work_Order__c= newWorkOrder.id;
                    Child.OptimaPro__MOBSVC_Start_Date_and_Time__c = datetime.now();
                    Child.OptimaPro__MOBSVC_Line_Status__c = 'Open';
                    Child.OptimaPro__MOBSVC_Product__c = ProductID;
                    Childs.add(Child);  
                }
            }
			insert Childs;
        }
    }
}

 
This was selected as the best answer
Patrick FoyPatrick Foy
Same.

Here is debug if it helps I don't get why it is running all those steps after. Looks like it is completing all the list just not inserting right.

User-added image
Steven NsubugaSteven Nsubuga
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.OptimaPro.MOBSVC_InstalledProduct_WorkOrderLine.createInstalledProduct: line 48, column 1


Could you check that line or share the code for the class OptimaPro.MOBSVC_InstalledProduct_WorkOrderLine
Patrick FoyPatrick Foy
Steve,

Cannot access just shows hidden probably because of managed package. I just threw that debug in a few spots and check and I don't see why it shouldn't work beyond hitting that wall. I may just have to get a hold of them and see what i'm up against. code coverage is at 100% now though for this. So i should be ok to deploy right?
Steven NsubugaSteven Nsubuga
So i should be ok to deploy right? Fingers crossed, YES!!
Patrick FoyPatrick Foy
Well thank you for your help and persistance. I will make sure to vote up last coded answer. 
Steven NsubugaSteven Nsubuga
It has been quite a ride Patrick!!