If I use adderror
as below I get the following error. How do I fix it?
SObject row does not allow errors
trigger LocationJunctionTrigger on Location_Junction__c (After insert,After Update)
{
set<id> locid =new set<id>();
set<id> junid =new set<id>();
for(Location_Junction__c jun:trigger.new){
locid.add(jun.location__c);
junid.add(jun.id);
}
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
{
list<Location_Junction__c> currentList = [SELECT Id,Location__c,Opportunity__r.Start_Date__c,Opportunity__r.AccountId from Location_Junction__c WHERE Location__c IN :locid AND Id IN : junid];
list<Location_Junction__c> existingList = [SELECT Id,Location__c,Opportunity__r.Start_Date__c,Opportunity__r.closedate,Opportunity__r.AccountId from Location_Junction__c WHERE Location__c IN :locid AND Id Not IN : junid];
for(Location_Junction__c currentLocation :currentList)
{
for(Location_Junction__c existingLocation :existingList){
if( existingLocation.Opportunity__r.AccountId == currentLocation.Opportunity__r.AccountId && currentLocation.Opportunity__r.Start_Date__c < existingLocation.Opportunity__r.closedate && currentLocation.Opportunity__r.Start_Date__c > existingLocation.Opportunity__r.Start_Date__c)
currentLocation.Location__c.adderror('Please select different location');
}
}
}
}