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
guyr1981guyr1981 

Filtering a custom object instances screen from user choices

Hi,

 

I have a few questions and I know it is a bit a lot but...
if you can help me with even just one I would really appreciate it.
I have my custom object (Employee) and I would like to make a screen that allows the user to choose a Company (custom object), a department (custom object) and a year.
And then create a table with all the employees that work in this company in this department.
Problems: (and again, any problem answered would be great).
1. If I use an <apex:inputField...> on company__c it is not allowed because  <apex:inputField...> can only be used on sObject Fields, and if I use it on company__c.name, it doesn't give the user the choices of existing companies to choose, only a plain empty input text box.
How can I make the input be a choice between already existing company instances?
(One way would be to create a custon object ("Choice__c") that holds lookup fields of type company__c and department__c and then use <apex:inputField...> on value= {!Choice__c.company} and {!Choice__c.department} but is that not a "dirty code", because I don't need to save any instances of Choice__c in my data base, just use them to display later the employees that matches the chice on the screen and that's it.
2. After choosing the company from the list of existing company instances, I would like to allow the user to choose the department in the same way, but also, I want only the departments that belong to this company choosen to appear as choice options (my department custom object has Master detail relationship with company__c and every department instance is pointing to a specific company).
How can I filter only that department to show as choice options?
3. Lets say I have an object "Choice__c" that holds the company and department and I want to go over the employees and find the ones that are related to the choosen company and department and present them on a table on the VF page.
so I make a refresh action API that goes on all the employees and checks if:
employee.Company__c.name == choice.Company__c.name
(Employee__c employee & Choice__c choice - were defined).
but keep getting this error message:
Error: Compile Error: Invalid foreign key relationship: Choice__c.Company__c at line 43 column 19
What does it mean and how should I correct my code?
Thanks,
Guy

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Question #1.  No, it's not dirty code, and it's a fine way to do it.  Taking maximum advantage of the built-in features is the way to go.   And yes, you are instantiating an entire object when all you really need is one field in that object... but so what. 

 

#2 is harder.  there is no way to instantiate a Department object and restrict the set of choices to ones related to a specific company.    So, one way to do this is to let the user choose the company.  Then, use that value to query the allowed departments.  Then loop through those building a List of SelectOption objects and use that in an Apex:SelectList tag.  So, it's not a lookup, but a drop-down list of departments.  You can do that in the setter for the Company, or the getter for the SelectList, etc.  But remember to only do it once when the Company changes otherwise you may be rebuilding the same select list again and again.

 

#3   So,if Choice__c is a salesforce object with two fields, company is a lookup to a Company__c object and department is another to a Department__c object:

 

Choice__c theChoice;

use an <apex:inputField value="{!theChoice.company__c}">

 

then build selectOption objects with the Department Name as the label, and the Department Id as the value and when

the user selects one of the options,   theChoice.department__c = the value from the selected option.

 

Now, if you want to find the employees, just leverage the query engine and do:

 

List<Employee__c> theEmps = [select name, etc. from employee__c where department__c = :theChoice.department__c and company__c = :theChoice.company__c];

 

 

Best, Steve.

 

All Answers

sforce2009sforce2009

Choice__r.Company__c you have to use.

SteveBowerSteveBower

Question #1.  No, it's not dirty code, and it's a fine way to do it.  Taking maximum advantage of the built-in features is the way to go.   And yes, you are instantiating an entire object when all you really need is one field in that object... but so what. 

 

#2 is harder.  there is no way to instantiate a Department object and restrict the set of choices to ones related to a specific company.    So, one way to do this is to let the user choose the company.  Then, use that value to query the allowed departments.  Then loop through those building a List of SelectOption objects and use that in an Apex:SelectList tag.  So, it's not a lookup, but a drop-down list of departments.  You can do that in the setter for the Company, or the getter for the SelectList, etc.  But remember to only do it once when the Company changes otherwise you may be rebuilding the same select list again and again.

 

#3   So,if Choice__c is a salesforce object with two fields, company is a lookup to a Company__c object and department is another to a Department__c object:

 

Choice__c theChoice;

use an <apex:inputField value="{!theChoice.company__c}">

 

then build selectOption objects with the Department Name as the label, and the Department Id as the value and when

the user selects one of the options,   theChoice.department__c = the value from the selected option.

 

Now, if you want to find the employees, just leverage the query engine and do:

 

List<Employee__c> theEmps = [select name, etc. from employee__c where department__c = :theChoice.department__c and company__c = :theChoice.company__c];

 

 

Best, Steve.

 

This was selected as the best answer
SteveBowerSteveBower

BTW, another thing to consider is that you don't really need to create Choice__c as a Salesforce object.  You can create it as an Apex class instead.   So, just define an Apex class:

 

public class Choice {

      Company__c company;

      Department__c department;

 

     // Helpers

     List<SelectOption> departments = new List<SelectOptions>();

     SelectOption selected;

 

      Choice() {

            company = new Company__c();

            department = new Department__c();

      }

}

 

public Choice myChoice {get; set;}

 

Now, instead if instantiating a Choice__c object, you can just use an instance of Choice.

 

In your Apex tags you can refer to   <Apex:inputField value="{!myChoice.company}">

 

And, you could put some "helpers" into Choice to help with the SelectOptions, and write a getter for Choice that rebuilds the list as needed.

 

Just wanted to point out that this might be more what you want than using up a persistent Salesforce Object.

 

Best, Steve