You need to sign in to do that
Don't have an account?
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY and Attempt to de-reference a null object on Trigger
I've gone and read through a lot of these forums but I find it difficult to apply the advice to my code, so here I am posting!
I get the following error from my Test class for my Trigger:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateRoomLocation: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.updateRoomLocation: line 30, column 1: []
It is the same error for all three test functions. The test class is below, and the trigger below that:
@isTest private class TestRoomBuildingAssignment { public static Location__c createLocation(Boolean createRoom){ Location__c location = new Location__c( caan__c = '1000', Building_Code__c = 'TEST', Location_Name__c = 'Test building' ); if(createRoom == True){ location.Room__c = '100'; } insert location; return location; } static Location__c createLocation(){ return createLocation(False); } static testMethod void testRoomAssignment() { test.startTest(); Location__c location = createLocation(); Location__c room = createLocation(True); test.stopTest(); system.assertEquals(room.Location__c, location.Id); } static testMethod void testLocationName() { // relies on createLocation making a location with // caan__c = 1000, Building_Code__c = 'TEST' & Room__c = 100 test.startTest(); Location__c location = createLocation(); test.stopTest(); system.assertEquals(location.Name, '0001-TEST'); } static testMethod void testRoomName() { // relies on createLocation making a location with // caan__c = 1000, Building_Code__c = 'TEST' & Room__c = 100 test.startTest(); Location__c location = createLocation(True); test.stopTest(); system.assertEquals(location.Name, '0001-TEST-100'); } }
Trigger:
trigger updateRoomLocation on Location__c (before insert) { /* We want to update all rooms with their correspondent buildings. We do this by: 1. getting all caan IDs for our trigger set 2. finding all buildings who have that caan id (buildings have no rooms) 3. matching room ID with building ID */ // we match rooms to caan numbers Map<Id, String> roomMap = new Map<Id, String>(); // eventually we match room Ids to Map<Id, Location__c> updateMap = new Map<Id, Location__c>(); for (Location__c room : Trigger.new){ roomMap.put(room.Id, room.caan__c); } if (roomMap.size() > 0){ Map<String, Location__c> buildingMap = new Map<String, Location__c>([ SELECT caan__c, Id FROM Location__c WHERE caan__c IN :roomMap.values() AND Room__c = '' ]); for (String roomId : roomMap.keySet()){ String caan = roomMap.get(roomId); updateMap.put(roomId, buildingMap.get(caan)); } for (Location__c room : Trigger.new){ room.Location__c = updateMap.get(room.Id).Id; } } }
Any help is greatly appreciated!
if(updateMap.containsKey(room.Id))
room.Location__c = updateMap.get(room.Id).Id;
Hi
Try this trigger
If this post answers your questions, please mark it as solved and give kudos if this helps you
Thanks
this line is not returning any value
updateMap.get(room.Id).Id
HI
Try this..