function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DBManagerDBManager 

Validate Lookup with Apex - Match Grandparent to Parent

I have run out of lookups within an object (Item__c), and there is a validation rule I want to write. I am trying to do it in an Apex trigger, but it doesn't seem to work properly.

 

The Item__c object has two lookups - one to the Event_Issue__c object and one to the Stand__c object. In turn, the Stand__c object has a lookup to Event_Issue__c. So I want to check, without using a lookup filter, that  Item__r.Stand__r.Event_Issue__c is the same record as Item__r.Event__Issue__c.

 

However, the code I have written seems to throw up the error whether those values match or not.

 

This is the code I have currently:

trigger LinkStand on Item__c (after insert, after update) {

for (integer i=0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Probability__c == 100 && Trigger.new[i].Item_Sold__c == 'Stand'){
        Stand__c[] stand = [SELECT Id, Event_Issue__c FROM Stand__c WHERE Name != 'TBC'];
        for (Stand__c sta :stand){
            if (sta.Event_Issue__c <> Trigger.new[i].Event_Issue__c){
                Trigger.new[i].Stand__c.adderror('The Stand you have chosen is at the wrong event. Please use the lookup icon to select the correct Stand');
            }
        }
    }
}

}

 

Hope someone can help.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

When you iterate the stands whose name != 'TBC', you are essentially comparing every stand with a name against a particular item, so it seems quite likely to me that you will encounter one where they don't match.

 

It sounds like you want to be restricting your stand query based on the relationship from the item.    Something like:

 

Stand__c[] stand=[select id, Event_Issue__c from Stand__c where id=:trigger.new[i].Stand__c];

 

All Answers

bob_buzzardbob_buzzard

When you iterate the stands whose name != 'TBC', you are essentially comparing every stand with a name against a particular item, so it seems quite likely to me that you will encounter one where they don't match.

 

It sounds like you want to be restricting your stand query based on the relationship from the item.    Something like:

 

Stand__c[] stand=[select id, Event_Issue__c from Stand__c where id=:trigger.new[i].Stand__c];

 

This was selected as the best answer
DBManagerDBManager

I actually realised the same thing when I looked at it again, so thanks!!

 

This is my working code:

for (integer i=0; i < Trigger.new.size(); i++){
    if (Trigger.new[i].Probability__c == 100 && Trigger.new[i].Item_Sold__c == 'Stand'){
        Stand__c[] stand = [SELECT Id, Event_Issue__c, Item__c FROM Stand__c WHERE Name != 'TBC'];
        for (Stand__c sta :stand){
            if (sta.Id == Trigger.new[i].Stand__c){
                if (sta.Event_Issue__c != Trigger.new[i].Event_Issue__c){
                    Trigger.new[i].Stand__c.adderror('The Stand you have chosen is at the wrong event. Please use the lookup icon to select the correct Stand');
                }
                else if (sta.Item__c != Trigger.new[i].Id && sta.Item__c != NULL){
                    Trigger.new[i].Stand__c.adderror('The Stand you have chosen is Reserved. Please choose another.');
                }
                else {
                    ItemIds.add(Trigger.new[i].Id);
                    EventIds.add(Trigger.new[i].Event_Issue__c);
                }
            }
        }
    }
}