Javascript check if multipicklist value inlcudes value

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Is there an easy way to check in the JS API that a multi-picklist field value includes a value?

Something like the below preferably?

    var caseModel = skuid.model.getModel('Case');   
    var caseRow = caseModel.getFirstRow();
    if(caseRow.FieldName.includes('My Value')){
       // Do stuff
    }
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Ok, I got bored.  This should get you close.

Create a custom field on Contacts like this:





Next, try this page XML.  Note you will need to open the JS Console.


<skuidpage unsavedchangeswarning="yes" showsidebar="true" showheader="true" tabtooverride="Contact">   <models>
      <model id="Contact" limit="1" query="true" createrowifnonefound="false" sobject="Contact">
         <fields>
            <field id="FirstName"/>
            <field id="LastName"/>
            <field id="CreatedDate"/>
            <field id="Hobbies__c"/>
         </fields>
         <conditions>
            <condition type="param" enclosevalueinquotes="true" operator="=" field="Id" value="id"/>
         </conditions>
         <actions/>
      </model>
   </models>
   <components>
      <pagetitle model="Contact">
         <maintitle>
            <template>{{FirstName}} {{LastName}}</template>
         </maintitle>
         <subtitle>
            <template>{{Model.label}}</template>
         </subtitle>
         <actions>
            <action type="delete"/>
            <action type="clone"/>
            <action type="share"/>
            <action type="savecancel" window="self"/>
            <action type="multi" label="Multi-Select Test">
               <actions>
                  <action type="custom" snippet="multiselect"/>
               </actions>
            </action>
         </actions>
      </pagetitle>
      <basicfieldeditor showsavecancel="false" showheader="true" model="Contact" mode="read">
         <columns>
            <column width="50%">
               <sections>
                  <section title="Basics">
                     <fields>
                        <field id="FirstName"/>
                        <field id="LastName"/>
                     </fields>
                  </section>
                  <section title="Multi-Select" collapsible="no">
                     <fields>
                        <field id="Hobbies__c" valuehalign="" type=""/>
                     </fields>
                  </section>
               </sections>
            </column>
            <column width="50%">
               <sections>
                  <section title="System Info">
                     <fields>
                        <field id="CreatedDate"/>
                     </fields>
                  </section>
               </sections>
            </column>
         </columns>
      </basicfieldeditor>
   </components>
   <resources>
      <labels/>
      <css/>
      <javascript>
         <jsitem location="inlinesnippet" name="multiselect" cachelocation="false">var $ = skuid.$;
var contactModel = skuid.$M('Contact');
var contact = contactModel.getFirstRow();
var hobbies = contact.Hobbies__c.split(';');
console.log('Hobbies: ' + hobbies);</jsitem>
      </javascript>
   </resources>
</skuidpage>
(Edited)
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
We all like it when Irvin gets bored.  

You going to be at DF15?  Drop by our booths in the Cloud Expo or in the Dev Zone - we'd love to talk. 
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Split your field value on ; and then use $.inArray() to check for a particular value, e.g. using Irwin's Hobbies field example:

var contactModel = skuid.$M('Contact');
var contact = contactModel.getFirstRow();
var isScubaDiver = skuid.$.inArray("Scuba Diving",contact.Hobbies__c.split(';'))>-1;
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb
Exactly what I was needing! Thanks Zach! This is excellent, I can now use this in an IF statement without having to write any extra loops. I owe you a beer.
Photo of Jarrod Hinson

Jarrod Hinson

  • 2,310 Points 2k badge 2x thumb
Now I don't have to rely on contains like below... :)

if(String(extRow.Field_Name__c).indexOf("myValue") > -1){
}