Render and save reference field value

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I'd like to use a custom field renderer to set the value for a reference field if a value doesn't exists. Here's my code:

'renderUserId': function (field, value) {
if (!value) {
value = skuid.utils.userInfo.userId;
var fieldId = field.id;
var fieldName = fieldId.slice(0,-1) + 'r.Name';
field.model.updateRow(
field.row,
{
fieldId: value,
fieldName: skuid.utils.userInfo.userName
},
{initiatorId: field._GUID}
);
}
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field, value);
}

It's kind of working, but I'm getting the 18 digit record Id displaying in the field, instead of the Name. I tried changing 'value' to 'skuid.utils.userInfo.userName' in the renderer, but got the same result.

Help?
Thanks!
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Hmm, so it turns out my static resource wasn't updating correctly. Cached or something? Let me get that sorted before anyone tries to answer!
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Ok,

So, I'm getting the renderer to show the correct name, but the updateRow action isn't working the way I'd expect.

My goal is to create a generic renderer which doesn't have to know the id of the field. Here's the code:

'renderUserId': function (field, value) {
if (!value) {
value = skuid.utils.userInfo.userId;
var fieldId = field.id;
var fieldName = fieldId.slice(0,-1) + 'r.Name';
field.model.updateRow(
field.row,
{
fieldId: value,
fieldName: skuid.utils.userInfo.name
},
{initiatorId: field._GUID}
);
}
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field, skuid.utils.userInfo.name);
}

The trouble is, this is just creating a two fields in the .data object of the model called "fieldId" and "fieldName", not actually updating the fields themselves.

I have successfully used field.id in an updateRow action this way:
field.model.updateRow(field.row,field.id, value,{initiatorId: field._GUID});
But it seems like that's not possible in the object notation for the field an value arguments, which I think is required to pass more than one field/value pair in the same updateRow(), correct?

Any thoughts on how to accomplish this?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yes it is possible to pass a generic list of field/value pairs into updateRow, but the other part of this that you may be getting stuck on is setting the Reference field object --- rather than passing in something like "Some_Field__r.Name", you need to pass in a whole related object, e.g. "Some_Field__r" should be an object, like this:

if (!value) value = skuid.utils.userInfo.userId;
var fieldId = field.id;
var fieldRelationshipName = fieldId.slice(0,-1) + 'r';
var updatesToMake = {};
updatesToMake[fieldId] = value;
updatesToMake[fieldRelationshipName] = { Name: skuid.utils.userInfo.name };
field.model.updateRow(field.row,updatesToMake,{initiatorId: field._GUID});
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Yes! Thanks, Zach.

As a simple matter of curiosity, is there a performance difference between building the updatesToMake object before or within the updateRow() call?

To confirm, if I update my syntax as follows, is it the same as what you suggested?

if (!value) {
value = skuid.utils.userInfo.userId;
var fieldId = field.id;
var fieldRelationshipName = fieldId.slice(0,-1) + 'r';
field.model.updateRow(
field.row,
{
fieldId: value,
fieldRelationshipName: {Name: skuid.utils.userInfo.name}
},
{initiatorId: field._GUID}
);
}
(Edited)
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
There's no performance difference per-se, the problem is that your approach is not doing what you think it's doing and will not actually work :)

Your approach is telling Skuid:

Update the "fieldId" field to have a value of whatever's in the value variable. 
Update the "fieldRelationshipName" field to have a value of the object { Name: skuid.utils.userInfo.name }

So your row will always end up with actually having a field on it called "fieldId" , even if your fieldId variable evaluates to "User__c", "Parent__c", "Patient__c", your row will end up looking like this:

{
    "fieldId": "005000001238900AAA".
    "fieldRelationshipName": {
          "Name": "Matt Sones"
    }
}

The approach I showed is telling Skuid:

Update the field whose Id is stored in the fieldId variable to have a value of whatever's in the value variable
Update the field whose Id is stored in the fieldRelationshipName variable to have a value of the object { Name: skuid.utils.userInfo.name }

So if fieldId is "User__c", then your row will look like this:

{
    "User__c": "005000001238900AAA".
    "User__r": {
          "Name": "Matt Sones"
    }
}

Big difference :)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Definitely! :) Thanks for the thorough explanation, Zach.