Can a button affect the values in fields?

  • 4
  • Question
  • Updated 2 years ago
  • Answered
I have a picklist of a bunch of statuses (new, submitted, rejected, in progress, etc.), but ideally the user should only be able to choose one (submitted) or leave it alone (new, or approved). Is there a way to make a button on a Skuid page that changes the value in a field on that page to something specific (and then even saves the page, perhaps)? Then the user doesn't have to click on a field and then choose a value in the field, they can just click once.

Right now I'm contemplating adding a new picklist field that has only the two the values in it, then a workflow that can update the main status field once they save their changes, since there isn't a good way to limit which values the user can choose in a picklist field. Obviously that's hokey, though, and doesn't improve the UI experience, either.
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb

Posted 6 years ago

  • 4
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
This is doable with a JavaScript Snippet. If your button (Page Title, Table Row/Global/Mass Action, Wizard, or unattached) is set to the type of "Custom", then you can specify the name of a Snippet to execute. Supposing that the Object you want to update is Case, and you have a Model called "CaseData", with one row in it, and you want to update the "Status" field to be a certain value, e.g. 'Escalated', and then want to immediately save that Model, here's what your snippet code would look like:



var CaseModel = skuid.model.getModel('CaseData');
var CaseRow = CaseModel.getFirstRow();
CaseModel.updateRow(CaseRow,'Status','Escalated');
CaseModel.save();
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb
Awesome - so easy! Every time you post a solution like this it gets me more excited to see a reference for the Skuid object model, so I can figure more of these things out myself, and come up with cool enhancements. Extrapolating from this will become very handy.
Photo of Kaede Holland

Kaede Holland

  • 2,744 Points 2k badge 2x thumb
This is cool, you guys!
Photo of Thaddeus Ward

Thaddeus Ward

  • 736 Points 500 badge 2x thumb
Can I used this same method to update multiple fields? How would the code look for that?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
The best way to update multiple fields is to use a version of updateRow which passes in the field/value pairings as a JavaScript object:


var CaseModel = skuid.model.getModel('CaseData');
var CaseRow = CaseModel.getFirstRow();
CaseModel.updateRow(CaseRow,{
'Status' : 'Escalated',
'Priority' : 'High',
'My_Custom_Field__c' : 'Hello World',
'OwnerId' : skuid.utils.userInfo.userId
});
CaseModel.save();
Photo of Thaddeus Ward

Thaddeus Ward

  • 736 Points 500 badge 2x thumb
Excellent. This makes great sense.
Photo of Ashley Erhart

Ashley Erhart

  • 150 Points 100 badge 2x thumb
Is there any way to accomplish this same thing without javascript? Specifically, creating a button that each time you click it moves the record through the picklist options for Status. So first click, changes from "New" to "In Progress" and second click changes from "In Progress" to "Complete", etc
(Edited)
Photo of Dave

Dave

  • 5,538 Points 5k badge 2x thumb
Hi Ashley, you could do it quite simply actually

Using your example, Create 2 button 

1st button: add action to Change it to "In Progress" + save and a rendering condition to only make it appear it when field = "New"

2nd button: add action to Change it to "Complete" + save  and a rendering condition to only make it appear it when field= "In Progress"

And so on...

For the end user they will never see the difference, as only 1 button would appear at a time

Hope it helps :)
Photo of Ashley Erhart

Ashley Erhart

  • 150 Points 100 badge 2x thumb
Thank you!