2

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');
                }
        }
    }
}
  • Hi Ratan, I have also tried using currentLocation.adderror('Please select different location'); same error is coming. if i use trigger.new[0].adderror('Please select different location'); then its working fine. – May the Force be with you Feb 19 '16 at 10:01
4

This is the solution for the above post

Map<id,Location_junction__C> Mapjun = Trigger.newmap;
Mapjun.get(currentLocation.id).Location__c.adderror('Please select different location');
  • is it an answer?... seems like it is. – Ratan Paul Feb 19 '16 at 13:24
  • @Ratan yes, it is an answer. – May the Force be with you Feb 19 '16 at 14:55
  • Pls add a comment in your post like this is working.. like else any one will be confuse and will give you downvote. – Ratan Paul Feb 19 '16 at 14:56
4

.addError can be called on Trigger.new in before/after insert and before/after update triggers, and on Trigger.old in before delete List. You can't add this to any other sObject that is why error is saying SObject row does not allow error.

So in your case you can add this to Location_Junction__c

  • Hi Himanshu, I was getting "Variable does not exist: Location_Junction__c " – May the Force be with you Feb 19 '16 at 10:17
  • when I say Location_Junction__c it is Trigger.new list. – Himanshu Feb 19 '16 at 11:15
  • when i use trigger.new[0]. Location__c.adderror('Please select different location'); its is showing error message like "Please select different location (Related field: Location)". can't i show the message next to the Location Field ? – May the Force be with you Feb 19 '16 at 11:24
  • inside trigger you can only add error to Trigger.new[0] – Himanshu Feb 19 '16 at 11:38
  • Not quite right... You can call it on the old values in a delete trigger. – Adrian Larson Feb 19 '16 at 14:15
1

You can use the addError method for only those records that are avaliable in Trigger Context. You are calling the addError on a field instead of sObject. Change the line to

provided currentLocation is part of the trigger context

 currentLocation.adderror('Please select different location');

You are traversing through existingList and you have populated with non trigger context records.

for(Location_Junction__c jun:trigger.new){

        locid.add(jun.location__c);
        junid.add(jun.id);
    }

    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];

What is the business logic here ? You want to throw errors for records which are not in trigger context ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.