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
uday uday chavanuday uday chavan 

null point exception error

Getting this Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger WorkOrderTrigger caused an unexpected exception, contact your administrator: WorkOrderTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.WorkOrderTriggerHelper.CreateWorkOrderItem: line 648, column 1

public static void CreateWorkOrderItem(List<Work_Order__c> workOrder){
        list<Work_Order_Item__c> listWOI = new list<Work_Order_Item__c>();
        for(Work_Order__c woItem:workOrder){
            String[] s = woItem.Service_Type__c.split(';');
            if(woItem.Status__c =='Completed' && s.contains('Waste Export') ){
this is 648 line                 woItem.Account__r.Terminal__c = true;
                Work_Order_Item__c newWOI = new Work_Order_Item__c();
                newWOI.Work_Order__c = woItem.Id;
                newWOI.Quantity__c    = woItem.Total_Ordered_Quantity__c;
                newWOI.Received_Quantity__c = woItem.Total_Delivered_Quantity__c;
                newWOI.Unit_Price__c = woItem.Price__c;
                newWOI.Description__c = woItem.Notes__c;
                listWOI.add(newWOI);
            }
        }
        Insert listWOI;
    }

after insert trigger i am using 
i am creating new record for work order item when condition are met also i am updating account related object terminal checkbox to true when condition are met i got this error
Best Answer chosen by uday uday chavan
ravi soniravi soni
hy uday uday chavan,
try following code. before using below code make sure that when you are passing trigger.new from trigger to this method CreateWorkOrderItem then you have to pass Terminal__c  field because Terminal__c is a relation's field and trigger never give any relation's referance  field. so in trigger you have to query this field.
Note - 2 : you never update any relation directly. you have to query of that object and then you have to update.
public static void CreateWorkOrderItem(List<Work_Order__c> workOrder){
        list<Work_Order_Item__c> listWOI = new list<Work_Order_Item__c>();
		set<Id> accIds = new set<Id>();
        for(Work_Order__c woItem:workOrder){
			
            String[] s = woItem.Service_Type__c.split(';');
			if(s.size() > 0){
            if(woItem.Status__c =='Completed' && s.contains('Waste Export') ){
				if( woItem.Account__r.Terminal__c == false){
					accIds.add(woItem.Account__r);
				}
                //woItem.Account__r.Terminal__c = true;
                Work_Order_Item__c newWOI = new Work_Order_Item__c();
                newWOI.Work_Order__c = woItem.Id;
                newWOI.Quantity__c    = woItem.Total_Ordered_Quantity__c;
                newWOI.Received_Quantity__c = woItem.Total_Delivered_Quantity__c;
                newWOI.Unit_Price__c = woItem.Price__c;
                newWOI.Description__c = woItem.Notes__c;
                listWOI.add(newWOI);
            }
        }
		}
		
		if(accIds.size() > 0){
			 list<Account> lstAccount = new list<Account>();
            for(Account Acc : [SELECT Id,name,Terminal__c From Account Where id In : accIds]){
				if( Acc.Terminal__c == false){
					Acc.Terminal__c = true;
				}
                lstAccount.add(Acc);
            }
            if(lstAccount.size() > 0){
                update lstAccount;
            }
			
		}
		if(listWOI.size() > 0){
		Insert listWOI;	
		}
        
    }
let me know if it helps you and marking it as best answer.
Thank you
 

All Answers

mukesh guptamukesh gupta
Hi uday,
 
public static void CreateWorkOrderItem(List<Work_Order__c> workOrder){
        list<Work_Order_Item__c> listWOI = new list<Work_Order_Item__c>();
        for(Work_Order__c woItem:workOrder){
            String[] s = woItem.Service_Type__c.split(';');
            if(! s.IsEmpty() && s != null){
            if(woItem.Status__c =='Completed' && s.contains('Waste Export') ){
       woItem.Account__r.Terminal__c = true;
                Work_Order_Item__c newWOI = new Work_Order_Item__c();
                newWOI.Work_Order__c = woItem.Id;
                newWOI.Quantity__c    = woItem.Total_Ordered_Quantity__c;
                newWOI.Received_Quantity__c = woItem.Total_Delivered_Quantity__c;
                newWOI.Unit_Price__c = woItem.Price__c;
                newWOI.Description__c = woItem.Notes__c;
                listWOI.add(newWOI);
            }
  }
        }

if(listWOI.size() > 0)
        Insert listWOI;
    }
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Please follow the below code:-
public static void CreateWorkOrderItem(List<Work_Order__c> workOrder){
        list<Work_Order_Item__c> listWOI = new list<Work_Order_Item__c>();
        for(Work_Order__c woItem:workOrder){
            String[] s = woItem.Service_Type__c.split(';');
          if(s != NULL){
            if(woItem.Status__c =='Completed' && s.contains('Waste Export') ){
this is 648 line                 woItem.Account__r.Terminal__c = true;
                Work_Order_Item__c newWOI = new Work_Order_Item__c();
                newWOI.Work_Order__c = woItem.Id;
                newWOI.Quantity__c    = woItem.Total_Ordered_Quantity__c;
                newWOI.Received_Quantity__c = woItem.Total_Delivered_Quantity__c;
                newWOI.Unit_Price__c = woItem.Price__c;
                newWOI.Description__c = woItem.Notes__c;
                listWOI.add(newWOI);
            }
}
        }
        Insert listWOI;
    }
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
uday uday chavanuday uday chavan
Mukesh thanks for your reply still getting same error
ravi soniravi soni
hy uday uday chavan,
try following code. before using below code make sure that when you are passing trigger.new from trigger to this method CreateWorkOrderItem then you have to pass Terminal__c  field because Terminal__c is a relation's field and trigger never give any relation's referance  field. so in trigger you have to query this field.
Note - 2 : you never update any relation directly. you have to query of that object and then you have to update.
public static void CreateWorkOrderItem(List<Work_Order__c> workOrder){
        list<Work_Order_Item__c> listWOI = new list<Work_Order_Item__c>();
		set<Id> accIds = new set<Id>();
        for(Work_Order__c woItem:workOrder){
			
            String[] s = woItem.Service_Type__c.split(';');
			if(s.size() > 0){
            if(woItem.Status__c =='Completed' && s.contains('Waste Export') ){
				if( woItem.Account__r.Terminal__c == false){
					accIds.add(woItem.Account__r);
				}
                //woItem.Account__r.Terminal__c = true;
                Work_Order_Item__c newWOI = new Work_Order_Item__c();
                newWOI.Work_Order__c = woItem.Id;
                newWOI.Quantity__c    = woItem.Total_Ordered_Quantity__c;
                newWOI.Received_Quantity__c = woItem.Total_Delivered_Quantity__c;
                newWOI.Unit_Price__c = woItem.Price__c;
                newWOI.Description__c = woItem.Notes__c;
                listWOI.add(newWOI);
            }
        }
		}
		
		if(accIds.size() > 0){
			 list<Account> lstAccount = new list<Account>();
            for(Account Acc : [SELECT Id,name,Terminal__c From Account Where id In : accIds]){
				if( Acc.Terminal__c == false){
					Acc.Terminal__c = true;
				}
                lstAccount.add(Acc);
            }
            if(lstAccount.size() > 0){
                update lstAccount;
            }
			
		}
		if(listWOI.size() > 0){
		Insert listWOI;	
		}
        
    }
let me know if it helps you and marking it as best answer.
Thank you
 
This was selected as the best answer
uday uday chavanuday uday chavan
hiii veer soni thanks solved the issue but terminal checkbox is not checked in account object
uday uday chavanuday uday chavan
hey thaks veer soni for your time issue solved just you have to use Account__c instead of account__r when adding the set id of account in your soln
also i have question this will work on update trigger 
ravi soniravi soni
hy uday,
yes, You are right it must be Account__c. It had happend by mistake but when you got solved your answer then must mark it as best answer.
your one best answer give us motivation to helping others.
Thank you