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
arcadiabakery1.315390544091857arcadiabakery1.315390544091857 

controller takes value in inputField (lookup) and returns an object

I have 3-step cake-ordering wizard. At the first step I ask user to select the Decoration through (lookup) field.

 

<apex:inputField id="Cake_Decoration" value="{!order.Decoration__c}" label="Cake Decoration" />

 Before moving to the next page, I need to check if the decoration is Available (Decoration__c.Available__c = true)

 

Here is the controller and the way I'm trying to solve it (look at step2() code)

 

public with sharing class orderWizard {
        
           private final Order__c ord;
public orderWizard(ApexPages.StandardController controller) { this.ord = (Order__c)controller.getRecord(); } Public Order__c order { get{ if (order==null){ order = new Order__c(); } return order; } set; }
public PageReference step1() { return Page.step1; } public PageReference step2(){ Set<Decoration__c> available = new Set<Decoration__c>(); for(Decoration__c d :[SELECT Id, Name, Available__c FROM Decoration__c WHERE Decoration__c.Available__c = true]) { available.add(d); } if(available.contains(order.Decoration__c)) { return Page.step2; }else{ Decoration__c.Name.addError('The decoration is not available'); return Page.step1;} }

 It gives me an error, basically saying that I cannot check an Id against a set of Objects :(

 

 My goal is to get an object based on the user's lookup selection, check if the Available__c field for this object is 'true' or 'false', if available (true), direct the user to the step 2 of the wizard, if not available (false), display an error message and stay on the same page. 

 

How do I get the controller recognize the value in the inputField (lookup) and return a record based on that value? 

 

This sounds like a trivial question but I've been battling it for days without success. Any help would be Greatly Appreciated! 

 

Thank you

 

Alex

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The lookup value is actually an id, not an object in its own right.  

 

Thus you should create a set of ids, and check if the lookup appears in that:

 

public PageReference step2(){
  
      Set<Id> available = new Set<Id>();
    
for(Decoration__c d :[SELECT Id, Name, Available__c
                            FROM Decoration__c
                            WHERE Decoration__c.Available__c = true]) 
                                    {
                                    available.add(d.id);
                                    }
if(available.contains(order.Decoration__c)) {
          return Page.step2;
}else{
    Decoration__c.Name.addError('The decoration is not available');
       return Page.step1;}
    }

 

All Answers

bob_buzzardbob_buzzard

The lookup value is actually an id, not an object in its own right.  

 

Thus you should create a set of ids, and check if the lookup appears in that:

 

public PageReference step2(){
  
      Set<Id> available = new Set<Id>();
    
for(Decoration__c d :[SELECT Id, Name, Available__c
                            FROM Decoration__c
                            WHERE Decoration__c.Available__c = true]) 
                                    {
                                    available.add(d.id);
                                    }
if(available.contains(order.Decoration__c)) {
          return Page.step2;
}else{
    Decoration__c.Name.addError('The decoration is not available');
       return Page.step1;}
    }

 

This was selected as the best answer
arcadiabakery1.315390544091857arcadiabakery1.315390544091857

Bob (or Keir), thank you so much! This works beautifully. Also, found your blog and that was too very helpful.