Render a text field as picklist field with specific values using custom field renderer

  • 2
  • Problem
  • Updated 3 years ago
  • Solved
I've got a table where the object has a Value field, and sometimes I want that Value show as is, but sometimes I'd like it to render as a Yes/No dropdown that has a value of 1 or 0. That sometimes is dependent on a picklist field Benchmark Type on my object's master object. 

I've put together a snippet based on some research on the community, but I'm getting a console error that my picklistEntries variable is null and so no values can be pushed to it, specifically: 
"Cannot read property 'push' of null"

Here's my snippet:

var field = arguments[0], 
    row = field.row,
    isPercentage = (row.Indicator__r.Benchmark_Type__c == 'Percentage') ? true : false;
    value = skuid.utils.decodeHTML(arguments[1]),
    metadata = field.metadata,
    element = field.item.element,
    $ = skuid.$;



if (isPercentage){
    
skuid.ui.fieldRenderers.PICKLIST[field.mode](field,value);

if (field.mode == 'edit'){

var picklistEntries = field.metadata.picklistEntries;

// picklistEntries.length = 0;
// I tried setting the length to see if it would help but it didn't

    picklistEntries.push(


   { value: '1', label: 'Yes', defaultValue: false, active: true  },


   { value: '0', label: 'No', defaultValue: true, active: true }
   
);
    
} 
}
else{
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
}
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,322 Points 5k badge 2x thumb

Posted 3 years ago

  • 2
Photo of Mordechai

Mordechai

  • 968 Points 500 badge 2x thumb
you may have to do something like this:
var picklistEntries = [];
picklistEntries.push(...);
field.metadata.picklistEntries = picklistEntries;
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,322 Points 5k badge 2x thumb
THAT WORKED!! Thanks so much
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,322 Points 5k badge 2x thumb
With Mordechai's help here's the final snippet, even though I set my No value as Default = true, Yes showed up as the default until I switched the order in which they appeared. Also moved the skuid.ui.fieldRenderers.PICKLIST below the picklistEntries, it worked the other way sometimes but became stable moving it below

var field = arguments[0], 
    row = field.row,
    
    // set the render condition
    isPercentage = (row.Indicator__r.Benchmark_Type__c == 'Percentage') ? true : false;
    value = skuid.utils.decodeHTML(arguments[1]),
    metadata = field.metadata,
    element = field.item.element,
    $ = skuid.$;

if (isPercentage && field.mode =='edit'){

//create a blank variable for picklist entries
var picklistEntries = [];

// set the picklist entries. note defaultValue doesn't matter, the first one will be default
    picklistEntries.push(

 { value: '0', label: 'No', defaultValue: true, active: true },
 { value: '1', label: 'Yes', defaultValue: false, active: true  }
   
);
    field.metadata.picklistEntries = picklistEntries;

// render the field as a picklist
    skuid.ui.fieldRenderers.PICKLIST[field.mode](field,value);
} 

else{
    
    //use the default renderer
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
}