[SalesForce] Make picklist blank/null on RecordType change

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?

Best Answer

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.

Related Topic