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
bikla78bikla78 

Update Picklist Values - Dynamic APEX?

I have 2 fields. One called contact.employee_type__c and a dependent picklist field called contact.employee_name__c.   The employee type is contain 2 picklist values "Internal" and "Consultant".  The employee name field is also a picklist that contains a list of all employee names which is dependent on what value is selected in the employee type field.

 

We want to create a trigger so that after a contact record is created with contact.contact_type__c = Internal or consultant, then it would update the picklist values with that record name. Is this a before insert using dynamic apex? any sample code would be helpful

Message Edited by bikla78 on 04-16-2009 11:03 AM
Message Edited by bikla78 on 04-16-2009 11:03 AM
Message Edited by bikla78 on 04-16-2009 01:54 PM
werewolfwerewolf
Remember that Apex is something that runs on the database after your record is saved.  If you're trying to make the UI change dynamically as the person is creating the record, you want to use Visualforce.
bikla78bikla78
Maybe I was not clear but I am trying to update the UI picklist after the update. I think i have to use an after update trigger but not clear on how to write a picklist update using APEX.
werewolfwerewolf

And my reply still holds.

 

Apex runs after you save the record.  Not while you're editing the record.

 

Visualforce is the technology you use to display and modify user interfaces.  So Visualforce is what you'll want to learn about.

bikla78bikla78

But I want my logic to run after I save the record not while I am editing it. Right when a contact record is inserted, I want to update the picklist values of a field on the contact object. In this case, contact.firstname && contact.lastname will be a value in the contact.employee_name__c depending on whether the contact.contact_type__c is an internal or consultant employee. I noticed there is a dynamic apex method called (getPicklistValue ). I'm not sure a VF page is the right way since I am not trying to change anything in the layout using MVC. I am simply trying to update the picklist values for a field in my schema using an after update trigger

 

 

Message Edited by bikla78 on 04-16-2009 02:23 PM
werewolfwerewolf
Well, if that's what you're trying to do then you could set the picklist with a trigger.  Still, something about this sounds fundamentally wrong to me.  For one, putting names in a picklist like that is not such a hot idea -- that's what lookup relationships are for.
werewolfwerewolf
But let's be clear: you can set the actual value of that field with your trigger.  You cannot set the potential values of the picklist with your trigger -- you can't set the values that appear when you drop down the list.
bikla78bikla78

The picklist of employee names is important to our business since we use this field to show who had referred the lead. This way, our internal staff or consultants is credited for the line of business. The dependent picklist was created so we can separate the employees by internal staff and consultants so it's easier for the end user to select and update. This may not be a hot idea to you from a technical perspective but it brought and continues to bring business value to our organization.

 

We have been updating the employee names in the picklist value set when people are terminated or hired. So....we figured we can use force.com custom code logic to udpate the picklists when an employee is created or determined as no longer employed.

 

You've made your point about using VF instead of APEX ...i just thought dymanic apex would work since we can query sobjects and update the picklists. I will look into if an MVC would work

 

thank for your help

Message Edited by bikla78 on 04-17-2009 09:17 AM
werewolfwerewolf
I don't think that's going to work because in order to update those dynamic picklist values you'd need to use the metadata API, which as far as I know is not presently accessible via Apex.
Marco_____Marco_____

you can achieve this but you need to change your design.

you will need to replace your depend field Contact.Employee_name__c by a lookup field to User and add a custom field like user.employeetype__c .

Then via Apex and Visualforce you will replace that lookup field by an home made picklist.

Apex will retrieve your employee by type: [select name from User where user.employee_type__c =:contact.Employee_type__c]

and then you can build a picklist in Apex with the selectoption class and display the result in your visualforce page.