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
bookerawardbookeraward 

Error Message while testing my Trigger

I am trying to write a trigger that fires when my custom object ‘Vehicles' is either updated or new recored is inserted.
If any of its 2 particular fields (In_service__c, Vehicle_designation__c) have been updated ,i want those records to be saved in a list.
And  i want to assign the value of  field (Group__c ) equal to the field (Vehicle_designation__c)
 My Code:  
Trigger TUpdate on Vehicle__c (before insert, before update) {
   set<String>  vehicleID = new Set<String>() ;
  for(Vehicle__c v: trigger.new){
    // if the fields are updated:
     Vehicle__c oldv = trigger.oldMap.get(v.Vehicle_ID__c);
      if ((v.In_service__c != oldv.In_service__c) || (v.Vehicle_designation__c != oldv.Vehicle_designation__c) )
      {  
       // field was updated,do something!
           v.Group__c = v.Channel_designation__c;
        }    
     // if new records are inserted:
      vehicleID.add(v.Vehicle_ID__c);
      v.Group__c = v.Channel_designation__c;
  }
There seems to be some problem with Line 6
if ((v.In_service__c != oldv.In_service__c) || (v.Vehicle_designation__c != oldv.Vehicle_designation__c) )
 when i run my test case it fails and if i insert a new record I get the error message
(Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TruUpdate caused an unexpected exception, contact your administrator: TUpdate: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.TUpdate: line 6, column 1.)

My Unit test:
@IsTest
public with sharing class TFieldUpdate {
static testMethod void testupdates(){

// Create our records from scratch!
 Vehicle__c vc = newVehicle__v()
 vc.Serial_Number__c='1233333';
 vc.In_service__c ='yes';
 insert vc;
//update Vehicle Record:
 vc.In_service__c ='no';
 update vc;  
 }
}


 
Best Answer chosen by bookeraward
Vivek DeshmaneVivek Deshmane
Hi ,
Try below code and let me know if this works,mark the answer
trigger TUpdate on Vehicle__c (before insert, before update) {
   set<String>  vehicleID = new Set<String>() ;
  for(Vehicle__c v: trigger.new)
  {
    // if the fields are updated:
	if(Trigger.isUpdate)
	{
		if(trigger.oldMap !=null)
		{
		 Vehicle__c oldv = trigger.oldMap.get(v.id);
		  if (oldv!=null && ((v.In_service__c != oldv.In_service__c) || (v.Vehicle_designation__c != oldv.Vehicle_designation__c)) )
		  {  
		   // field was updated,do something!
			   v.Group__c = v.Channel_designation__c;
		  }    
			   
		}
    }else if(Trigger.isInsert)
	{
		v.Group__c = v.Channel_designation__c;
    }	
  }
}

Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi,
Try below trigger code and let me know if this works.
trigger TUpdate on Vehicle__c (before insert, before update) {
   set<String>  vehicleID = new Set<String>() ;
  for(Vehicle__c v: trigger.new)
  {
    // if the fields are updated:
	if(trigger.oldMap !=null)
	{
     Vehicle__c oldv = trigger.oldMap.get(v.id);
      if (oldv!=null && ((v.In_service__c != oldv.In_service__c) || (v.Vehicle_designation__c != oldv.Vehicle_designation__c)) )
      {  
       // field was updated,do something!
           v.Group__c = v.Channel_designation__c;
      }    
     // if new records are inserted:
      vehicleID.add(v.Vehicle_ID__c);
      v.Group__c = v.Channel_designation__c;
	}  
  }
}

Best Regards,
-Vivek
bookerawardbookeraward

Yes, its not giving any errors.Thanks.

But its not doing what its supposed to do.
Forexample, when I edit "In_service" field in a record,it should assign the value of the field "Vehicle_designation__c" to "Group__c ",which i have set it to do in this case.

Appreciate your help Vivek.

Vivek DeshmaneVivek Deshmane
Hi ,
Try below code and let me know if this works,mark the answer
trigger TUpdate on Vehicle__c (before insert, before update) {
   set<String>  vehicleID = new Set<String>() ;
  for(Vehicle__c v: trigger.new)
  {
    // if the fields are updated:
	if(Trigger.isUpdate)
	{
		if(trigger.oldMap !=null)
		{
		 Vehicle__c oldv = trigger.oldMap.get(v.id);
		  if (oldv!=null && ((v.In_service__c != oldv.In_service__c) || (v.Vehicle_designation__c != oldv.Vehicle_designation__c)) )
		  {  
		   // field was updated,do something!
			   v.Group__c = v.Channel_designation__c;
		  }    
			   
		}
    }else if(Trigger.isInsert)
	{
		v.Group__c = v.Channel_designation__c;
    }	
  }
}

Best Regards,
-Vivek
This was selected as the best answer
bookerawardbookeraward
Thanks.Works perfect!