Have updateRow function chose field to update dynamically

  • 1
  • Question
  • Updated 8 months ago
  • Answered
Hello

Is there a way to have the updateRow function in the javascript pick the field that is being updated dynmically? 

It looks like I need to hard code in the salesforce fields that are being updated, so the line would look like:

accountModel.updateRow(row, {Custom_Field__c : value})

I have a variable that pulls in the name of the Salesforce field to be updated, but when I try to plug in that variable it won't work:

accountModel.updateRow(row, {salesforceFieldNameVar : value})

Is there anyway to make something like this work?
Photo of Joe Dobbels

Joe Dobbels

  • 526 Points 500 badge 2x thumb

Posted 8 months ago

  • 1
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,040 Points 10k badge 2x thumb
Hi Joe,

What happens when you try this? Is there an error message you can share? 
Photo of Joe Dobbels

Joe Dobbels

  • 526 Points 500 badge 2x thumb
There's no error message or anything, it just doesn't update the field. I'm saving the model right after, and it works if I just put in the name of a field in there. Since it's not like the createRow function that has the additional conditions which accepts the SF field names as strings, it's thinking that the name of the variable I'm trying to pass in is the name of the Salesforce field on the model I want to update.
(Edited)
Photo of Mike Dwyer

Mike Dwyer

  • 3,380 Points 3k badge 2x thumb
Joe, have you verified that the field name var is being populated correctly? (console.log(salesforceFieldNameVar ); ) Are you generating that name yourself, and failing to include the __c suffix?

Is the salesforceFieldNameVar getting a string, or an object?

Mark, is it necessary to create a string value for the field name, the colon, and the value -- everything between the {} curly braces?
Photo of Joe Dobbels

Joe Dobbels

  • 526 Points 500 badge 2x thumb
It is being populated correctly as a string. It's coming from the context row on a UI only model. They are all standard fields on the Account object so __c shouldn't be an issue. I suppose I could do a huge if-else statement, but the variable can be like 20 possible fields so I don't really want to do that.
Photo of Mike Dwyer

Mike Dwyer

  • 3,380 Points 3k badge 2x thumb
It's still possible you're getting an object instead of a string. Could you post the assignment statement where you get the field names?

I think I have done - or attempted - this somewhere, but I can't recall which of a couple hundred pages. We are reviewing those one by one for our v9.5 to v11.1 upgrade, however, so I'll look for it. I can't give you a timeframe however. Let's hope you get a quicker answer from someone else.
Photo of Joe Dobbels

Joe Dobbels

  • 526 Points 500 badge 2x thumb
I did console.log(typeof salesforceFieldNameVar ); and that showed that it's a string. And it's coming from a string field on a UI only model so I don't see how it could be anything else.
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,040 Points 10k badge 2x thumb
Hi Joe and Mike,

I have been trying to approach this, but haven't had success yet. I attempted to create a string that would define the contents of the {} mustaches, but didn't find a way to make that work. When I try to use a variable to dynamically define the field's name, the variable name is getting used instead of its value. 

I am not sure of your larger scenario but before digging in too deep on scripting, I wanted to recommend looking into action sequences and inputs. Those do allow for dynamic "reusable" sequences; you can define a number of inputs like field, value, model, etc. and then call the action sequence up from anywhere. As long as it receives those inputs, it should work. It may take some experimenting, but I wanted to endorse this as a possible approach because it's much more declarative, and easier to include in pages and other action sequences. 

https://docs.skuid.com/latest/en/skuid/action-framework/action-sequences/index.html?highlight=input
is a detailed overview of how this all works in general, and 
https://docs.skuid.com/latest/en/skuid/action-framework/action-sequences/inputs-how-to-condition-req...
is a how-to, that focuses on one example use-case.

Do you think this might fit into what you're working on?
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Joe,

Try this:
var obj = {};
obj[salesforceFieldNameVar] = value;
accountModel.updateRow(row, obj);

Thanks,

Bill
Photo of Mike Dwyer

Mike Dwyer

  • 3,380 Points 3k badge 2x thumb
Elegant!
Photo of Joe Dobbels

Joe Dobbels

  • 526 Points 500 badge 2x thumb
That worked Bill! Thanks!