Render and save reference field value

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:

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


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!

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!

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


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?

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;<br>var fieldId = field.id;<br>var fieldRelationshipName = fieldId.slice(0,-1) + 'r';<br>var updatesToMake = {};<br>updatesToMake[fieldId] = value;<br>updatesToMake[fieldRelationshipName] = { Name:&nbsp;skuid.utils.userInfo.name };<br>field.model.updateRow(field.row,updatesToMake,{initiatorId: field._GUID});

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) {<br />value = skuid&#46;utils&#46;userInfo&#46;userId;<br />var fieldId = field&#46;id;<br />var fieldRelationshipName = fieldId&#46;slice(0,-1) + 'r';<br />field&#46;model&#46;updateRow(<br />field&#46;row,<br />{<br />fieldId: value,<br />fieldRelationshipName: {Name: skuid&#46;utils&#46;userInfo&#46;name}<br />},<br />{initiatorId: field&#46;_GUID}<br />);<br />}

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 :slight_smile:

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 :slight_smile:

Definitely! :slight_smile: Thanks for the thorough explanation, Zach.