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
Allen ManamelAllen Manamel 

How to traverse a lookup field to find if a value exists or not

I have a lookup field on my contact object named "ALO" and it is a restrictive lookup field as in it only accepts few values.I want to assign it the value stored in "account owner" field on the account object but here is the problem.
If the "account owner" value is the legitimate value for ALO lookup field i.e if the value exists as one of the options in ALO lookup field, only then it gets assigned otherwise if the value does not exist in the ALO field then it should set the field to null. However, I am confused how do I traverse the ALO lookup field to find out if the account owner value is an acceptable value for ALO lookup field?

Here is my code:

acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.AccountId
                        ];
                        
                // I need help here        
                List<User> user= [SELECT ALO__c FROM contact where ALO =:acc.OwnerId];
                
                if (user.size() == 1) 
                
                  {
                
                       contact.ALO__c = acc.OwnerId;
                
                  }
                  
                else
                
                 {
                
                      contact.ALO__c= null;
                
                 }