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
BusyBoyBusyBoy 

Apex trigger on a checkbox

How do I a write trigger on a checkbox. I am trying to change the value of a picklist based on a checkbox click. Do i even need to write an apex trigger for this, or is there any other method of achieving this? I dont want to write a trigger to update the record, but just need to change the value of the picklist before even the user hits save button.

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
BaguiarBaguiar

Go to your fields section for the object you want to set the dependancy. Click on the field dependencies. Click on NEW.

ON the controlling field , pick the check box field you have.

On the Dependent field, select the picklist field taht depends on the check box for the value.

Click continue.

ON the Table that will come up, on the column "Checked" double click ONLY on the value you want, if the check box is checked. It should highlight the value in Yellow. on the "Unchecked" column, double click on the other remaining values, NOT the one you;ve picked under the ":Checked" column.

Click Save.

 

You can make the picklist required, so that the user will not leave the box "Null".

 

Go to your page and test the check box and picklist values...

All Answers

Cory CowgillCory Cowgill

This sounds like you want to use Field Dependency.

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_dependent_picklists.htm

 

Apex Triggers only get executed once DML operations have been executed (insert,update,delete,undelete, etc). Since you want this changed before the user clicks Save you can't use Triggers to accomplish that.

 

 

BusyBoyBusyBoy

Is that only for filtering values in a picklist, i want the value to be changed automatically to something.

BaguiarBaguiar

Set the value for the picklist as a unique value if the checkbox is checked. Not sure if thats what you mean. Is that an unique checkbox or do you have multiple checkboxes that would set the picklist values ?

BusyBoyBusyBoy

Yeah only one checkbox. Set the value of the AccountType picklist to "Head Office" when I check the checkbox "Main Account". How do i do that using field dependency.

BaguiarBaguiar

Go to your fields section for the object you want to set the dependancy. Click on the field dependencies. Click on NEW.

ON the controlling field , pick the check box field you have.

On the Dependent field, select the picklist field taht depends on the check box for the value.

Click continue.

ON the Table that will come up, on the column "Checked" double click ONLY on the value you want, if the check box is checked. It should highlight the value in Yellow. on the "Unchecked" column, double click on the other remaining values, NOT the one you;ve picked under the ":Checked" column.

Click Save.

 

You can make the picklist required, so that the user will not leave the box "Null".

 

Go to your page and test the check box and picklist values...

This was selected as the best answer
BusyBoyBusyBoy

Thanks for the detailed explanation. Sorry I still didnt achieve what i want. According to this, the user has to select the value manually, I want to automatically change the picklist value when checkbox is checked. I dont know if it is even possible.

BaguiarBaguiar

You can use a workflow rule with a field update. If the checkbox field is checked, update the field (picklist) to the value you want.  I'd add a help text to the checkbox field so users know when checking that field, the picklist value will be set automatically.

BusyBoyBusyBoy

But that will update the field in the background. I dont want that. I want the picklist item to be changed and if the user wishes to change it he should be able to do so before clicking the save button.

BaguiarBaguiar

When you tested the dependant picklist, did you make your Picklist field required on the page layout?

 

 If you did so, and the option for the value, if the box is checked, is unique, when the user checks the box, the value goes automatically on the picklist.

BusyBoyBusyBoy

I made it required now and it worked. Thanks a lot.  I missed that in your earlier post. So whenever you need to change the value of a field automatically you should make it required? Sorry for asking too many questions..SFDC is new to me.. Appreciate your help.