RecordType context for Table Filter

  • 2
  • Idea
  • Updated 4 months ago
Thought this might be helpful for folks.  This is a snippet that you can use on tables to limit the values of a picklist field to the permitted ones for a selected Record type.  Skuid tables don't have record context, so the default behavior is to display all picklist values for a field in the filter.  For more involved objects, this gets unwieldy.

The code here is an extension of this article:  https://docs.skuid.com/latest/en/skuid/filters/snippet-as-item-source.html and the setup of the page (and the logic) is pretty similar to how it's described in the article.  A model condition driving the table needs to work off the RecordTypeId and only works with a condition where the operator is "="  (No multiple values allowed in the condition).  We use "Field from another model" for the condition and that one looks up the RecordType object which is either accepting a parameter fed into it or setting the DeveloperName to pull the recordType as well as an SObject condition.  If the filter is inactivated, all values for the picklist are populated.

This snippet is a bit more dynamic and both the lookup field and the logic to sort out the sObject information are passed in as a parameter from the filter.  It will also find the condition for the record type based on the condition filtering the RecordTypeId field, so you don't need to to worry about naming conventions on the condition.  This means you can use this piece of code for any picklist on any object where RecordTypes are in play.



If anyone has an easier approach or improvements, much appreciated.  Here's the code snippet and I'll post a sample page in a comment here.



var f = arguments[0],
$ = skuid.$;
m = f.model;
o = m.objectName;
fld = f.list.filterCondition.field;
filterItems = [];

$.each(m.conditions,function(a,b){
   if(b.field == 'RecordTypeId' && b.inactive === false && b.operator == '='){
       rt = b.value;
       d = skuid.utils.getAPIDescribeLayout(o);
       $.each(d.recordTypeMappings,function(e,rtm){
           if(rtm.recordTypeId == rt){
               $.each(rtm.picklistsForRecordType,function(f,pl){
                   if(pl.picklistName == fld){
                       $.each(pl.picklistValues, function(g,pv){
                           if(pv.active === 'true'){
                               lab = pv.label;
                               val = pv.value;
                               filterItems.push({label: lab,value: val});
                           }
                         
                       });
                   }
               });
           }
       });           
           
   }

   if(b.field == 'RecordTypeId' && b.inactive === true){
        obj = skuid.utils.getAPIDescribeSObject(o);
        $.each(obj.fields, function(e,f){
           if(f.name == fld){
               $.each(f.picklistValues,function(g,pv){
                    if(pv.active === 'true'){
                       lab = pv.label;
                       val = pv.value;
                       filterItems.push({label: lab,value: val});
                   }
               });
           } 
        });
   }  
   
});

return filterItems;
Photo of John Dahlberg

John Dahlberg, Champion

  • 2,442 Points 2k badge 2x thumb

Posted 4 months ago

  • 2
Photo of John Dahlberg

John Dahlberg, Champion

  • 2,442 Points 2k badge 2x thumb
Here's a simple sample page to see how this works in operation.  This is off the Task Object and doesn't have any custom references so you should be able to just drop it in to play with it.  Note that there's no change to the javascript from the first post where I pulled it from an opportunity page.

One quick point of clarity from the original post.  You don't actually need a dedicated model for the RecordType. This example uses an auto generated condition from a table filter for selecting the RecordType.  Skuid already does a good job of generating this filter, so no reason to over-complicate it if you don't have to.



<skuidpage unsavedchangeswarning="yes" personalizationmode="server" useviewportmeta="true" showsidebar="true" showheader="true">
    <models>
        <model id="Task" limit="20" query="true" createrowifnonefound="false" datasource="salesforce" sobject="Task">
            <fields>
                <field id="RecordTypeId"/>
                <field id="Id"/>
                <field id="Subject"/>
                <field id="Type"/>
                <field id="Status"/>
                <field id="OwnerId"/>
                <field id="Owner.Name"/>
                <field id="WhatId"/>
                <field id="What.Name"/>
                <field id="WhoId"/>
                <field id="Who.Name"/>
                <field id="RecordType.Name"/>
            </fields>
            <conditions>
                <condition type="fieldvalue" value="" enclosevalueinquotes="true" field="Type" state="filterableoff" inactive="true" name="Type"/>
                <condition type="fieldvalue" value="" enclosevalueinquotes="true" field="Status" state="filterableoff" inactive="true" name="Status"/>
                <condition type="fieldvalue" field="RecordTypeId" operator="=" inactive="true" enclosevalueinquotes="true" name="__autofilter__RecordTypeId" state="filterableoff" value=""/>
            </conditions>
            <actions/>
        </model>
    </models>
    <components>
        <skootable showconditions="true" showsavecancel="true" showerrorsinline="true" searchmethod="server" searchbox="true" showexportbuttons="false" hideheader="false" hidefooter="false" pagesize="10" alwaysresetpagination="false" createrecords="true" model="Task" buttonposition="" mode="read" allowcolumnreordering="true" responsive="true" uniqueid="sk-2q90-390">
            <fields>
                <field id="Subject" uniqueid="fi-2q90-393"/>
                <field id="Type" uniqueid="fi-2q90-394"/>
                <field id="Status" uniqueid="fi-2q90-395"/>
                <field id="OwnerId" uniqueid="fi-2q90-396">
                    <label>Assigned</label>
                </field>
                <field id="Owner.Name" uniqueid="fi-2q90-397"/>
                <field id="WhatId" uniqueid="fi-2q90-398">
                    <label>Related To</label>
                </field>
                <field id="WhoId" uniqueid="fi-2q90-400">
                    <label>Who</label>
                </field>
                <field id="RecordTypeId" uniqueid="fi-2q90-391">
                    <label>Record Type</label>
                </field>
            </fields>
            <rowactions>
                <action type="edit"/>
                <action type="delete"/>
            </rowactions>
            <massactions usefirstitemasdefault="true">
                <action type="massupdate"/>
                <action type="massdelete"/>
            </massactions>
            <views>
                <view type="standard"/>
            </views>
            <filters>
                <filter type="select" createfilteroffoption="true" affectcookies="false" autocompthreshold="25" conditionsource="auto" filtermethod="server" labelmode="manual" condition="RecordTypeId" label="Record Types" filteroffoptionlabel="All Record Types" conditionfield="RecordTypeId" fieldtargetobjects="RecordType"/>
                <filter type="select" createfilteroffoption="true" affectcookies="false" autocompthreshold="25" conditionsource="manual" filtermethod="server" labelmode="auto" condition="Type" filteroffoptionlabel="All Types">
                    <sources>
                        <source type="custom" effectsbehavior="justdefault" snippet="RecordTypeFilter"/>
                    </sources>
                </filter>
                <filter type="select" createfilteroffoption="true" affectcookies="false" autocompthreshold="25" conditionsource="manual" filtermethod="server" labelmode="auto" condition="Status" filteroffoptionlabel="All Statuses">
                    <sources>
                        <source type="custom" effectsbehavior="justdefault" snippet="RecordTypeFilter"/>
                    </sources>
                </filter>
            </filters>
        </skootable>
    </components>
    <resources>
        <labels/>
        <javascript>
            <jsitem location="inlinesnippet" name="RecordTypeFilter" cachelocation="false">var f = arguments[0],
$ = skuid.$;
m = f.model;
o = m.objectName;
fld = f.list.filterCondition.field;
filterItems = [];

$.each(m.conditions,function(a,b){
   if(b.field == 'RecordTypeId' &amp;&amp; b.inactive === false &amp;&amp; b.operator == '='){
       rt = b.value;
       d = skuid.utils.getAPIDescribeLayout(o);
       $.each(d.recordTypeMappings,function(e,rtm){
           if(rtm.recordTypeId == rt){
               $.each(rtm.picklistsForRecordType,function(f,pl){
                   if(pl.picklistName == fld){
                       $.each(pl.picklistValues, function(g,pv){
                           if(pv.active === 'true'){
                               lab = pv.label;
                               val = pv.value;
                               filterItems.push({label: lab,value: val});
                           }
                         
                       });
                   }
               });
           }
       });           
           
   }

   if(b.field == 'RecordTypeId' &amp;&amp; b.inactive === true){
        obj = skuid.utils.getAPIDescribeSObject(o);
        $.each(obj.fields, function(e,f){
           if(f.name == fld){
               $.each(f.picklistValues,function(g,pv){
                    if(pv.active === 'true'){
                       lab = pv.label;
                       val = pv.value;
                       filterItems.push({label: lab,value: val});
                   }
               });
           } 
        });
   }  
   
});

return filterItems;</jsitem>
        </javascript>
        <css/>
        <actionsequences uniqueid="sk-2q8l-259"/>
    </resources>
    <styles>
        <styleitem type="background" bgtype="none"/>
    </styles>
</skuidpage>