You need to sign in to do that
Don't have an account?

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:
(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:
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; } }
Try below code and let me know if this works,mark the answer
Best Regards,
-Vivek
All Answers
Try below trigger code and let me know if this works.
Best Regards,
-Vivek
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.
Try below code and let me know if this works,mark the answer
Best Regards,
-Vivek