Using a custom renderer to conditionally set a picklist's value?

  • 2
  • Question
  • Updated 4 years ago
  • (Edited)
I have a picklist field, ReferralType__c, that I want to set to a certain value (that the user can see, but not change) if a condition is met. I've added a custom renderer for this and the value appears to be set the way I want in the UI, but when I save the record, Skuid says "Required field has no value: [Referral Type]" (I have the field marked as required in the Skuid page.

Here's my snippet:
var field = arguments[0];
var value = arguments[1]; var acc = skuid.model.getModel('Account').getFirstRow(); var referrerType = skuid.model.getModel('Account').getFirstRow().Type; if (referrerType !== 'Group Coordinator') { skuid.ui.fieldRenderers.PICKLIST.read(field,'Individual'); return; } else { skuid.ui.fieldRenderers.PICKLIST.edit(field,value); return; }
The if statement is the culprit here - if it goes to the else statement, the field is rendered as a picklist and everything works fine. How can I get the if statement to work correctly?

I've included a screenshot too, in case that helps - the shows the the hardcoded value of 'Individual' is being shown in the UI, but apparently not being set in the model.

Photo of Jonathan Gillespie

Jonathan Gillespie

  • 870 Points 500 badge 2x thumb

Posted 4 years ago

  • 2
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,470 Points 10k badge 2x thumb
Hi Jonathan,

When you send a particular value into one of Skuid's field renderers, this will only affect the UI, the models in the background will not be affected.  One approach that would work (probably the wrong one) would be to use Skuid's updateRow API to change the value of the field and then render it.  You could add this code inside the first block of your if statement and it would work.

var accModel = skuid.model.getModel('Account');
accModel.updateRow(acc,'Type','Individual');
While doing this probably wouldn't cause any issues in your scenario, I would say that it really isn't a best practice to be giving your custom field renderers "side effects" like this.  In the course of running your Skuid page, this renderer could be run many times.  In my opinion, the act of rendering a field should not affect the underlying models.

Another approach you could take would be to create an inline resource that simply adjusted the models right after page load.  Then your custom field renderer could still determine read/edit mode when it ran, but would not be responsible for setting values in your models.

Your inline resource would look something like this...

(function(skuid){	
    var $ = skuid.$;
    // Wait for the page to load before performing this action.
$(function(){
var accModel = skuid.model.getModel('Account'); var accRow = accModel.getFirstRow(); if (accRow.Type !== 'Group Coordinator') { accModel.updateRow(accRow,'Type','Individual'); }

});
})(skuid);
Photo of Jonathan Gillespie

Jonathan Gillespie

  • 870 Points 500 badge 2x thumb
Ben, this is great! Thanks so much for the help - I'll test both of these options out, but I think I agree with you that the second option (inline resource) might be the better approach.