How do I render custom picklist values?

edited June 18, 2019 in Questions
I have a page where users need to be able to change a picklist to change the status of a record, such as "working", "submitted", etc. However, normal users shouldn't be able to put it into the approved status. I know I can add a validation rule that says "if the user's profile = x and the picklist value = "approved", then throw error message 'you can't select that' ", but the ideal way, of course, is to prevent a normal user from being able to select the "accepted" value in the first place. This seems like a perfect opportunity for a custom Skuid renderer! I'm thinking that in read mode the picklist shows the current value, but in edit mode it shows only values that the current user has access to based on their profile. Something like: if (field.mode == 'read') { skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value); } else if (field.mode == 'edit') { /* if (profile != admin) { add picklist value 'working'; add picklist value 'submitted'; } else if (profile = 'admin') { add picklist value 'approved'; } render picklist in edit mode */ } How would I do this (or create an equivalent or similar solution)? Bonus points: Maybe it can also prevent normal users from changing the status from "approved" to something else?
ยซ1

Comments

  • Zach McElrathZach McElrath Principal Software Engineer Chattanooga, TN ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited March 2, 2017
    One solution to this is, which doesn't require a custom renderer, is to add multiple RecordTypes to your Salesforce object. Then, different RecordTypes can be assigned to different Profiles, and each RecordType can have its own set of Picklist Values for each of your Picklist / Multi-select Picklist Fields. This functionality is supported by Skuid in the Spring 13 release. However, this strategy involves added setup effort, and adding RecordTypes is not always an ideal / desired solution, particularly if there's only one picklist that you want to have different values for. Also, you'll have to make sure that the correct RecordType is chosen on a given record in order for this functionality to take effect --- either by having a Condition on your model explicitly like RecordTypeId = 'School', or whatever the API name of the RecordType is, or by adding the RecordTypeId field to your Field Editor or Table and requesting the user to select the desired RecordType. All that being said... Here's how you could do it with a custom Field Renderer instead :) If you want to do conditional picklist value adding based on Profile Name, instead of Profile Id, then you'll need to request the Running User's Profile information in a separate model on your page, e.g. a 'RunningUser' model.
        var field = arguments[0],      value = arguments[1];    // Get the Running User's Profile Name from a 'RunningUser' model:  var userModel = skuid.model.getModel('RunningUser'),     user = userModel && userModel.getFirstRow(),     profileName = user && userModel.getFieldValue(user,'Profile.Name',true),     isAdmin = (profileName == 'System Administrator');    //  // Bonus points FIRST :)  //  // Prevent non-Admins from making any edits   // if the current status is 'Approved'.  //  if ((value === 'Approved') && !isAdmin) {      field.editable = false;      field.mode = 'read';  }    if (field.mode == 'edit') {    var picklistEntries = field.metadata.picklistEntries;    if (isAdmin) {  // add picklist values 'working' and 'submitted'  picklistEntries.push(     { value: 'Working', label: 'Working', defaultValue: false, active: true  },     { value: 'Submitted', label: 'Submitted', defaultValue: false, active: true }  );  } else if (profileName == 'Sales Manager') {  picklistEntries.push(     { value: 'In Review', label: 'In Review', defaultValue: false, active: true }  );  }    }    // Run the standard picklist renderer for the given mode  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);    
  • edited June 18, 2019
    Zach, thanks for this. I've used this to render conditional picklist values. I've got two issues, though: 1) How do I make the list default to the existing status if there is a row/record already existing? Right now it always appears as "--None--", even when the existing value is something else. (I've set up the field with an "Add None" property. Maybe this is related to #2...) 2) The page is rendering the standard set of picklist values, then rendering the "pushed" values from my JavaScript, resulting in two sets of identical values in the picklist. How do I get it to only render the values I'm trying to put into the list with my code? Here's my code:
      var field = arguments[0],      value = arguments[1];      var isTeacher = false;    var userModel = skuid.model.getModel('User'),      userRow = userModel.getFirstRow(),      userRole = userModel.getFieldValue(userRow,'Contact.Current_Role__c'),      isTeacher = (userRole == 'Teacher');    // Prevent teachers from making any edits if it is approved  if ((value === 'Reviewed & Approved') && isTeacher) {      field.editable = false;      field.mode = 'read';  }    if (field.mode == 'edit') {      var picklistEntries = field.metadata.picklistEntries;   picklistEntries.push(    { value: 'Working', label: 'Working', defaultValue: false, active: true  },    { value: 'Ready for Review', label: 'Ready for Review', defaultValue: false, active: true }   );   if (!isTeacher) {    picklistEntries.push(     { value: 'Reviewed & Approved', label: 'Reviewed & Approved', defaultValue: false, active: true },     { value: 'Reviewed & Needs Changes', label: 'Reviewed & Needs Changes', defaultValue: false, active: true }    );   }  }  // Run the standard picklist renderer for the given mode  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);  
  • Zach McElrathZach McElrath Principal Software Engineer Chattanooga, TN ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited December 21, 2016
    For (1), my question is, does it still not pick the correct value if the status is "Working"? For (2), try adding the following line after "var picklistEntries = field.metadata.picklistEntries;" picklistEntries.length = 0;
  • edited November 19, 2015
    Thanks Zach. adding that line fixed (2). For (1), the page saves the values fine. When the model refreshes or the page loads and the value is one of the first two (working, ready for review), it loads the correct value as the default. However, when the value is one of the second two (reviewed & approved, reviewed & needs changes), those values are not populated as an existing value. I can edit the field and change its value, though. The rest of the fields seem to load fine. So, something about the logic around building the picklist entries seems to be interfering with loading the existing value in to the field.
  • Zach McElrathZach McElrath Principal Software Engineer Chattanooga, TN ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited February 2, 2018
    Thanks, that helps me confirm what the problem is. Change value = arguments[1] to be value = skuid.utils.decodeHTML(arguments[1]), and that should resolve (1). Here's the detailed explanation of why this is necessary: XSS vulnerability prevention. Whenever you do a Custom Field Renderer on a Text / Textarea / Picklist field, Skuid preemptively HTML encodes the current value stored for a particular field / row, so that, if you, the author of the Custom Field Renderer, happen to accidentally display this text to the screen without first properly encoding it, if necessary, then you won't inadvertently introduce an XSS vulnerability into your page. So, from the theoretical to your specific scenario: the reason that "Reviewed & Approved" and "Reviewed & Needs Changes" aren't working is that they contain a character, the &, which has to be HTML encoded. So the value that you are getting handed in arguments[1] is actually Reviewed & Approved. So by using skuid.utils.decodeHTML(), you're converting this back into the actual value. Then, because you're leveraging the skuid.ui.fieldRenderers to actually do your output (as opposed to hand-coding your own picklist), the XSS vulnerability prevention is taken care of. So the lesson to learn here is this: Whenever doing custom Field Renderers for Text/Textarea/Picklist fields, always decode the value passed in via arguments[1], like so:
        var field = arguments[0],     value = skuid.utils.decodeHTML(arguments[1]);    
  • edited February 2, 2018
    Excellent - that fixed it! Good lessons learned here. Thanks so much for your help on this, Zach. For anyone's future reference, below is the "final" version of my custom picklist renderer.
      var field = arguments[0],      value = skuid.utils.decodeHTML(arguments[1]);      userModel = skuid.model.getModel('User'),      userRow = userModel.getFirstRow(),      userRole = userModel.getFieldValue(userRow,'Contact.Current_Role__c'),      isTeacher = (userRole == 'Teacher'),      grpModel = skuid.model.getModel('Guided Reading Plan'),      grpRow = grpModel.getFirstRow();    // Prevent teachers from making any edits if the status is approved by forcing the mode to read-only  if ((value === 'Reviewed & Approved') && isTeacher) {      field.editable = false;      field.mode = 'read';  }    if (field.mode == 'edit') {      var picklistEntries = field.metadata.picklistEntries;      picklistEntries.length = 0;     // if you don't do this, then the "real" values are already in the picklist and the code below will add duplicate values        // if there is no record yet, add a default value. NOTE: when the page is saved, the grp model is only saved when the value in this field is not blank. I am NOT using the Skuid field property "Add 'None' Option" because I only want this option to appear when there isn't already a record.      if (skuid.model.isNewId(grpRow.Id)) {          picklistEntries.push(              { value: '', label: '-- Select a Status to Create --', defaultValue: false, active: true  }          );      }        // create picklist values for the basic statuses   picklistEntries.push(       { value: 'Working', label: 'Working', defaultValue: false, active: true  },    { value: 'Ready for Review', label: 'Ready for Review', defaultValue: false, active: true }   );        // create picklist values for the review/approval statuses if the user is not a teacher   if (!isTeacher) {    picklistEntries.push(     { value: 'Reviewed & Approved', label: 'Reviewed & Approved', defaultValue: false, active: true },     { value: 'Reviewed & Needs Changes', label: 'Reviewed & Needs Changes', defaultValue: false, active: true }    );   }  }  // Run the standard picklist renderer for the given mode  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);  
  • edited December 18, 2014
    Hey Zach I'm trying to use this code but it doesn't seem to be doing anything for me. I'm trying to ย remove a picklist value, but it keeps showing up... here is my snippet:
    var field = arguments[0],      value = skuid.utils.decodeHTML(arguments[1]);      var picklistEntries = field.metadata.picklistEntries;      picklistEntries.length = 0;     // if you don't do this, then the "real" values are already in the picklist and the code below will add duplicate values   picklistEntries.push(          { value: 'Fixed', label: 'Fixed', defaultValue: false, active: true },    { value: 'Indexed', label: 'Indexed', defaultValue: false, active: true }   );  // Run the standard picklist renderer for the given mode  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);  
    If I log it in the console I can see that my changes have been made, however it doesn't reflect on the page....
  • edited December 18, 2014
    It works!
  • edited May 12, 2015
    Hi,

    I have a picklist with 4 values, i would like to remove 2 values. I ย am following the below code, but it is not removing those values.
    skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);

    var select = field.element.find('select'); if (select.length) {
    // Remove unwanted entries
    $.each(select.children('option'),function(){
    if ($(this).val()==='BA') $(this).remove(); ย  if ($(this).val()==='UA') $(this).remove(); }); }
  • edited August 7, 2015
    Thank you!
    I was beating my head against the wall trying to figure out why overriding the skuid.metamodel.picklistEntries was not working.
  • edited October 12, 2015
    I get an errror saying "TypeError: field.metadata is undefined" am I missing something here?
    var field = arguments[0], value = skuid.utils.decodeHTML(arguments[1]); skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value); var select = field.element.find('select');  if (select.length) {    // Remove unwanted entries     $.each(select.children('option'),function(){         if ($(this).val() === 'Below Expectation'){             $(this).remove();         }     });     select.append($('<option value="cheese">Cheese</option>')); }
  • Matt SonesMatt Sones ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited December 7, 2016
    Hasantha,

    It sounds like your snippet isn't getting passed the field as arguments[0]. How are you launching you snippet? This code will only work if you're running it as a custom field renderer.
  • edited October 14, 2015
    oops, my bad thanks for pointing it out Mattย :)
  • edited May 24, 2018
    You guys rock! Thanks, this really helped :-)
  • edited February 5, 2016
    Hey team, just wondering how I would modify this snippet to render radio buttons instead of the picklist dropdown?

    Seems so simple but I haven't been able to figure it out :-)

    Thank you in advance!
  • edited May 24, 2018
    I asked this in a comment above, though have not found a reply and am still stuck with it. How do I modify the snippet to render radio buttons instead of the standard picklist dropdown?

    Thank you!
    var $ = skuid.$;  var field = arguments[0],  value = skuid.utils.decodeHTML(arguments[1]);  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);  var select = field.element.find('select');    if (select.length) {       $.each(select.children('option'),function(){          if ($(this).val() === 'Survey Complete - Qualified' || $(this).val() === 'Sent' || $(this).val() === 'Responded' || $(this).val() === 'Survey Complete - DNQ' || $(this).val() === 'Appointment Booked'){              $(this).remove();          }       });    }
  • Jack SanfordJack Sanford San Antonio, TX ๐Ÿ’Ž๐Ÿ’Ž
    edited May 25, 2016
    I'm also trying to figure out how to render a picklist created by a custom field renderer snippet as Radio Buttons.ย 

    I know based on this post:ย http://help.skuidify.com/m/11720/l/214170-skuid-ui-basic-renderersย that I need to use renderas 'RADIO_BUTTONS' in some way, but not sure how.ย 

    my snippet:
    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.edit(field,value);      }   else{          //use the default renderer skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value); }
  • Jack SanfordJack Sanford San Antonio, TX ๐Ÿ’Ž๐Ÿ’Ž
    edited May 25, 2016
    ask and ye shall receive, h/t toย https://community.skuidify.com/skuid/topics/readonly-picklist-radio-buttons-can-still-be-changed-on-...

    add a lineย 
    field.options.type = 'RADIO_BUTTONS';
    before your skuid.ui.fieldRenderers

    So Robin you might try
    value = skuid.utils.decodeHTML(arguments[1]); field.options.type = 'RADIO_BUTTONS'; skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode]
  • Rob HatchRob Hatch ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited September 1, 2016
    Thanks Jack!ย 
  • edited February 29, 2016
    Hi Jack and Rob,ย 

    Amazing, thank you for the help! I can now render the options as radio buttons (YAY!). As an added challenge, I would still like to remove unwanted entries. The existing script as suggested earlier does not seem to do the trick any more once the list is rendered as radio buttons. How would I have to modify this code to remove them?

    Thank you in advance!
    if (select.length) { ย 
    ย  ย $.each(select.children('option'),function(){
    ย  ย  ย  ย  if ($(this).val() === 'Survey Complete - Qualified' || $(this).val() === 'Sent' || $(this).val() === 'Responded' || $(this).val() === 'Survey Complete - DNQ' || $(this).val() === 'Appointment Booked'){
    ย  ย  ย  ย  ย  ย  $(this).remove();
    ย  ย  ย  ย  }
    ย  ย });
  • edited January 12, 2018
    I am trying to render a custom picklist but the picklist is just rendering as normal and there are no errors in the console. Can some one point in the rightย 

    Objective:
    If the running user has a sales category of "Retail Sales" then only show the "Retail Sales" option in the picklist. Otherwise render as usual.

    Notes:
    This is a dependent picklist.

    Code:
    var $ = skuid.$;  var field = arguments[0],      value = skuid.utils.decodeHTML(arguments[1]);      var isRetailSales = [];        var userModel = skuid.model.getModel('RunningUser'),      quoteModel = skuid.model.getModel('QuoteDetail'),      userRow = userModel.getFirstRow(),      userSalesCategory = userModel.getFieldValue(userRow,'AC_Sales_Category__c'),      isRetailSales = (userSalesCategory == "Retail Sales");                  skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);      var select = field.element.find('select');                if (isRetailSales && field.mode == 'edit') {        if (select.length) {       // Remove unwanted entries        $.each(select.children('option'),function(){          if ($(this).val() === 'Retail Sales'){              $(this).remove();          }         });        }  }
  • Matt SonesMatt Sones ๐Ÿ’Ž๐Ÿ’Ž๐Ÿ’Ž
    edited February 10, 2017
    Tami,

    It looks like your code is removing the value is the value === 'Retail Sales', but from your description, you're looking to do the opposite. Try changing === to !==
    if (isRetailSales && field.mode == 'edit') {
    if (select.length) { // Remove unwanted entries $.each(select.children('option'),function(){ if ($(this).val() !== 'Retail Sales'){ $(this).remove(); } }); } }
  • edited December 2, 2016
    Thanks for that catch Matt, but it still is not working. Even when it was "===Retail Sales", retail sales was not removed.ย 
  • edited December 2, 2016
    ^bump^
  • Jack SanfordJack Sanford San Antonio, TX ๐Ÿ’Ž๐Ÿ’Ž
    edited June 3, 2016
    have you tried triple === in the lines
    isRetailSales = (userSalesCategory == "Retail Sales"); 
    and/orย 
    if (isRetailSales && field.mode == 'edit') {
    I'm no JS whiz but I think that first one might need ===

    Also you might try inserting some console.log lines at various junctures. Like after isRetailSales definition, console.log('Is Retail Sales? '+isRetailSales), and maybe also after select.length to get the length of your picklist and make sure that is coming in right, and maybe also in the $.each loop, log the value of each picklist value so you can see what it's capturing. That might help you find an error somewhere.ย 

    Hope that helps!
     
  • edited December 2, 2016
    Jack,

    THANK YOU! the issue was with the "==" was why the picklist was not rendering properly. When I changed to "===" ย the rendering is honored. I will remember this going forward with JS!

    However now, ย the value is not saved.ย 'Retail Sales' is the only option therefore chosen by default (I assume).ย I checked the object data in the console and the value indeed is not saving.ย 

    I tried adding this line with no success. Any idea why the value is not being accepted?
    select.append($('<option value="Retail Sales">Retail Sales</option>'));
  • Jack SanfordJack Sanford San Antonio, TX ๐Ÿ’Ž๐Ÿ’Ž
    edited June 3, 2016
    You said it's a dependent picklist, is the controlling picklist value set correctly? Is there a record type issue, with Retail Sales not valid for the particular record type you're using? What error are you getting that it won't save? I wouldn't think your custom field renderer snippet would need to handle saving
  • edited December 2, 2016
    The controlling value picklist value is set correctly it defaults to the correct value. There is not a record type issue and I am not getting any error on save.ย 

    I made the field required to see what happens on save and it saves with the first value in the picklist not the value that is being displayed.

    So for example I am only showing "Retail Sales" based on the snippet. When I hit save "Account Sales - Institutional Sales" is the value being saved which is the first value in the picklist.
  • Jack SanfordJack Sanford San Antonio, TX ๐Ÿ’Ž๐Ÿ’Ž
    edited June 3, 2016
    I've run into similar issues with picklists, particularly for reference fields, where it will default to a value I don't want and shouldn't even be available based on filters or security settings.ย 

    Is it possible in your use case to default the value to Retail Sales on the model via a condition or model action? Or, if you make it required, can you add None as an option so people have to choose one?ย 

    Also, I think it might work better to instead of removing all the values that don't equal Retail Sales, use a render snippet that declares these are all the values of my picklist: 'Retail Sales'.ย 

    Just had another idea that might be a way around this:
    Add two picklists, one that renders if the running user's Sales Category = 'Retail Sales', and one that renders if it doesn't. On the retails sales picklist, you could override the metadata so that the only option is Retail Sales, and make it required.ย 

    Unless your field is in a table...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!