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');
                }
        }
    }
}
1
  • 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. Commented Feb 19, 2016 at 10:01

3 Answers 3

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');
3
  • is it an answer?... seems like it is.
    – Ratan Paul
    Commented Feb 19, 2016 at 13:24
  • @Ratan yes, it is an answer. Commented Feb 19, 2016 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
    Commented Feb 19, 2016 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

10
  • Hi Himanshu, I was getting "Variable does not exist: Location_Junction__c " Commented Feb 19, 2016 at 10:17
  • when I say Location_Junction__c it is Trigger.new list.
    – Himanshu
    Commented Feb 19, 2016 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 ? Commented Feb 19, 2016 at 11:24
  • inside trigger you can only add error to Trigger.new[0]
    – Himanshu
    Commented Feb 19, 2016 at 11:38
  • Not quite right... You can call it on the old values in a delete trigger.
    – Adrian Larson
    Commented Feb 19, 2016 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 ?

7
  • Thanks Bennie, I was getting "Variable does not exist: Location_Junction__c " Commented Feb 19, 2016 at 10:16
  • @MaytheForcebewithyou see my updated post
    – Jarvis
    Commented Feb 19, 2016 at 10:20
  • I tried existingLocation.adderror('Please select different location'); and currentLocation.adderror('Please select different location'); both giving same error. Commented Feb 19, 2016 at 10:24
  • @Bennie Please check this salesforce.stackexchange.com/questions/110753/… Commented Feb 19, 2016 at 10:44
  • @MaytheForcebewithyou see updated post. currentLocation.adderror('Please select different location');
    – Jarvis
    Commented Feb 19, 2016 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .