0

I wanted to set picklist value to Null/Blank whenever I changed/try to update the Record Type on standard object.

*Whenever I try to change RecordType, picklist values should be set to blank (Like it happens with Dependent picklists), BUT this should happen before I click on "SAVE" button.


EDITS :

OR Lets put it in this way :

Steps:

  1. If I change Record Type on Case and
  2. Click on Save button
  3. It should set my picklist values to blank/null
  4. THEN, It should again force user to select the picklist values. (FYI : Business Reason)

Code Attempt (before update):

public List<Case> updateCaseReasonIssue(list<Case> listNewCases, map<id, Case> mapOldCases){
      for(Case newCase : listNewCases){
        if(newCase.recordTypeId != mapOldCases.get(newCase.Id).recordTypeId){
              newCase.Picklist_one__c ='';
              newCase.Picklist_two__c ='';                               
              newCase.addError('Please select Picklist values again.');
        }
      }
}

Is there any way to do it?

2

I'm skeptical that what you're describing would be good from a User Experience (UX for short) perspective, but I won't get caught up on that.

The requirement that your picklist be nulled out before any "Save" button is pressed severely limits your options here. Process Builder, Workflow, and Triggers are all out of the question (as all of them are invoked by the very act of pushing that "Save" button). If you can relax your requirement (so that this can happen after clicking "Save"), then I'd recommend taking care of this with a workflow rule + workflow field update.

If you truly must do this before clicking any "Save" button, then you're left with one option. You'll need to make a Visualforce page, and make use of a custom controller (or controller extension) along with a partial page refresh.

The general idea is that you'd add an <apex:actionSupport> to your <apex:inputField> (that displays your recordType field), and call a helper method in your controller/extension onchange.

The helper method would check the previous value of your recordTypeId field (this could be taken care of with a private class variable, no need to add a getter or setter to it), and null out your target picklist when the current value of RecordTypeId is different than the previous value.

Finally, by having your <apex:actionSupport> re-render a portion of the Visualforce page that contains your target picklist field, you'll be able to see the picklist value be nulled out (and allow your user to potentially pick a new value before hitting "Save").

About the only pitfalls that I can think of that you'll need to be mindful of are:

  • Make sure your helper method is nulling out the picklist value on the instance of the record that the controller is operating on.
  • Make sure your helper method is updating the 'previous' value of RecordTypeId when it detects that the new and previous values differ

As one final note, I don't know if you'd be able to make this approach work if you were to use <apex:detail>. I believe this approach is better suited for pretty much anything other than trying to override the standard view/edit pages.

| improve this answer | |
  • Thank you for brief answer. As I you said I relaxed (on save button) my requirements. Updated Question. Is there any other way than VF page. Please have a look. – Salesforce Steps Mar 10 '17 at 8:52
  • If I use Workflow Field update, by the time my record will be committed to DB which I don't want. Hoping that somehow it will work in BeforeUpdate Trigger (updated try code in Question). – Salesforce Steps Mar 10 '17 at 8:53
  • 1
    @SalesforceSteps Even with your updated question, your only real option here is still to make a VF page because you want your users to complete two separate tasks (changing the record type, followed by choosing a new picklist value), and you want those tasks to be completed in sequence, without interruption. – Derek F Mar 10 '17 at 14:12
  • 1
    @SalesforceSteps An imperfect alternative could be to just forget about the 'null out the picklist field' step. If you want to force users to change a picklist value when the record type changes, you could accomplish that with a validation rule. The reason why I call it 'imperfect' is because if the new picklist value should be the same as the old picklist value, the validation rule wouldn't allow it (unless you explicitly add exceptions to the validation rule, which will make your life harder down the line). – Derek F Mar 10 '17 at 14:20
  • Agree, I am thinking of going with Apex Trigger and Custom Settings now. (because have other logic related to it too) – Salesforce Steps Mar 10 '17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.