I wrote an APEX trigger to check for duplicates before a record is created or saved to ensure there are no duplicates. However, since the variables are empty (before record creation) I keep getting the following error message:
Apex trigger COIDuplicateTrigger caused an unexpected exception, contact your administrator: COIDuplicateTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.COIDuplicateTrigger: line 7, column 1
APEX Trigger:
trigger COIDuplicateTrigger on COI__c (before insert,before update) {
// creates trigger on the COI object to occur before insert or before update of record
map<Id,COI__c> existingCOImap = new map<Id,COI__c>([Select Id, Name, Company__c From COI__c]);
// maps variable to store data queried from SOQL statement
for(COI__c i : Trigger.new){
if(i.Name == existingCOImap.get(i.Id).Name && i.Company__c == existingCOImap.get(i.Id).Company__c){
i.addError('Error! Duplicate COI record found.');
}
}
}
// loop to see if data from name AND company fields match a COI record. if TRUE then throws error message.
Any tips on how to work around this?
Thanks!
Mercury1986