3

Can anyone explain to me how to make a field read-only without Validation Rule? Is it possible through Triggers?

Scenario:

I have to make a checkbox disabled (Not unchecked, but read-only) when some other field is empty.

Can anyone quickly revert back with solution?

In some other posts I have seen some solution which is posted below...

AND
(
ISCHANGED(field_to_protect__c),
Trigger_set_read_only_c
)

Not sure how to use it though..Is it correct coding in triggers?..If Yes, can anyone clearly explain me this?

0

2 Answers 2

1

The best way to achieve this is use of formula field of type Checkbox:

  • Create a new formula field
  • Select type Checkbox
  • Create a formula like this:

    IF(NOT(ISBLANK(MyTextField__c.Name)), true, false)

Now if MyTextField__c field is empty the checkbox is not selected and checkbox is selected if the field is not empty. This field is always read only because it will be generated on the fly.

Try to change MyTextField__c to your real field name.

Here some information about formula fields: Calculated field type

1
  • Thanks for sugesstion!! But in my requirement...Checkbox is not only depends on other field which i have mentioned above...It has some other functionality to do too.. If this checbox is checked then we will update some other fields and if it is unchecked we will update some other fields...and in scenario we have to make it uneditable (based upon some other field's value .i.e., empty)... So a formula field ..might not work in my case :( Anymore sugesstions?? Can we do it through apex coding in triggers?? .
    – Arshad
    Commented Jan 26, 2014 at 19:08
1

I think for your case that you need to used different logic dependent in the specific user case/scenario the correct option could be use apex code = triggers, and might be use too object layouts to show to the user which specific fields are going to be always read only.

If you write all your logic in the triggers then you won't need to have validation rules (they will be a plus but not required), also for future you will be able to extend those triggers adding new functionality.

(***Take care where you create the triggers you wouldn't like to break the CRM ;) )

In addition to the trigger logic you could use layouts, then the user will be more aware about the read only fields. But remember don't use only layout validations because they won't stop the users to do any modifications by DML through developer console for example (Universal Required ). Also if the read only is optional , don't use layouts.

Logic in trigger would be something like this:

//You will need to implement the logic in insert and update, also before insert and before update if you would like to modify fields, the modifications can't be done in after trigger!! REMEMBER this tip : MODIFY BEFORE, VALIDATE AFTER :) ( when would the validation is posible in after of course ) If(checkbox is true) { update some other fields } else { update some other fields
}

//validation if(scenario and user try to modify field) { throw error "You can't edit the field when scenario" }

-----Related to your example : AND ( ISCHANGED(field_to_protect__c), Trigger_set_read_only_c )

"ISCHANGED" Function comes from a validation Rule, therefore if you don't want to use a Validation Rule this code won't be helpful.

However if you still would like to use a validation Rule, the code above is doing this : - I guess that the whole code should be as follow :

NOT(AND(ISCHANGED(field_to_protect__c), Trigger_set_read_only__c) )

  • what it does is : if the user try to change the field_to_protect__c value and Trigger_set_read_only__c is true, then an error will be thrown to don't allow to do that.

Hope it helps I'll keep an eye on the question if I can help more :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .