Auto populating fields?

  • 1
  • Question
  • Updated 5 years ago
  • Answered
The "Assets" object in Salesforce requires the "Name" field to be saved. What's the best practice if I'm creating a new row within the table component so that the user doesn't have to enter anything in this arbitrary field? I have a Workflow rule that is supposed to fill in the field if it's Null, but it doesn't seem to get fired before Skuid updates.

Perhaps there is a way to auto-populate this field within a Skuid custom render snippet?

Any thoughts would be appreciated.
Photo of David Stith

David Stith

  • 220 Points 100 badge 2x thumb

Posted 5 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Official Response
David, one of my esteemed colleagues pointed out that my previous suggestion was a bad approach, because the other side of the story is, that having this Condition on your Table will make it so that your Table will only ever pull in existing records whose Asset Name is "DummyValue" --- probably not what you want :)

So, I think you have to go one of two directions:

(1) Populate the Asset Name field in an Apex "before insert" Trigger, so that the Asset Name field's Required validation will not be a problem.

(2) Use a custom field renderer in your Skuid table, which automatically provides a value for Asset Name if one does not exist already.

For the custom field renderer, since you are treating Asset Name like a dummy value, you probably would (ideally) rather not have it in your Table, and just rely on the users selecting a Product.

So, what you can do is to have a Custom Field Renderer for the Product field, which makes it so that, when you update the Product field for a particular row, it will take the selected Product's Name and dump it into the Asset Name field, so that the user never even has to think about the Asset Name. Just add a new Inline Snippet through the JavaScript Resources area, call it "ProductRenderer", and use this as the body of the Snippet:



var field = arguments[0],
value = arguments[1],
displayType = field.metadata.displaytype;

skuid.ui.fieldRenderers[displayType][field.mode](field,value);

if (field.mode == 'edit') {
// If our Product is changed,
// get the product's Name, and use that as the new Asset Name
var listener = new skuid.ui.Field(
field.row,
field.model,
null,
{fieldId: 'Product2Id',register:true}
);
listener.handleChange = function() {
field.model.updateRow(
field.row,
'Name',
field.model.getFieldValue(field.row,'Product2.Name'),
{initiatorId: field._GUID}
);
};
}


Then, go to your Product field, and for "Field Renderer", change to "Custom", and for "Render Snippet", enter "ProductRenderer".

The result will be a table like you had, only you don't need the Asset Name column anymore. You'll probably want to make the Product column REQUIRED, so that your users don't get hit with the "Asset Name is Required" error (and otherwise, what's the use of this table... :)