Snippet: Capture field from 1 model: Update field in separate model

  • 1
  • Question
  • Updated 4 years ago
My Javascript is very "beginner" at this point.  Could someone tell me the syntax for a snippet that would add a row to a table (Model B), capture data from a field editor field (Model A), and update the new row with said data?  It will actually be multiple fields, but I think I can adjust the code if someone can give me a head start... thanks so much for any help on this!
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Something like this will work... I left in the fields that I was using to give you an idea of how it works:

var $ = skuid.$;var modelA = skuid.model.getModel('ModelA');
var modelB = skuid.model.getModel('ModelB');
var row = modelA.getFirstRow();
modelB.createRow({
additionalConditions:[
{field: 'Utility_Account__c', value: row.Utility_Account__c, nameFieldValue: row.Utility_Account__r.Name},
{field:'Utility_Account_Number__c', value: row.Utility_Account_Number__c},
{field:'LDC2__c', value: row.LDC2__c, operator:'=', nameFieldValue: row.LDC2__r.Name},
{field:'Billing_Group__c', value: row.Billing_Group__c},
{field:'Opportunity__c', value: row.Opportunity__c}
]
}); 
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Though I'm grateful for Moshe's sharing, I think you are jumping to code more quickly than you need to.  I think you can create this functionality with the action framework and not have to use any code.  

First make sure you have at least version 5.13 of skuid installed.  Some key features were introduced with that version. 

Drag a page title component into the vicinity of your field editor.  This page titile shoudl be associated with the same model as your field editor (Model A). Add a button to the page title of type "Run multiple actions" 

Action1:  Create a new row in model.   Choose Model B. 
You will notice a new icon by the action type labled "Add Default Value".   Add as many of these actions as you need to prepopulate your data.

 

Action 2:  Save Model B
Action 3:  Requery Model B

Hope this works for you. 
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Hey Rob sorry for not promoting the action framework... I really need to start using it more. That add default value button looks great!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
No worries Moshe. The code solution works just as well.  When you are comfortable with a tool there is nothing wrong with using it...
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb
Hey Guys - 
First off, thanks for the help!
I must not have 5.13 yet, because I don't have access to the default option.  I will be upgrading soon, but for now I need the JS.  I've tried Moshe's code, but I can't seem to get the value of the field I need.  Any suggestions?  Does the field name need to be different in each model?  I seem to only be capturing the ID number of the field...

var $ = skuid.$;var modelA = skuid.model.getModel('modRFQ');var modelB = skuid.model.getModel('modVendor');
var row = modelA.getFirstRow();
modelB.createRow({
additionalConditions:[
{field:'Hanger__c', value: row.Hanger__c, operator:'=', nameFieldValue: row.Hanger__r.Name}
]});
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Is Hanger__c a lookup field? What context are you running the snippet from (page title, row action, mass action..) ? What do you mean the " ID number of the field"? Try this to help you debug:

var $ = skuid.$;var modelA = skuid.model.getModel('modRFQ');var modelB = skuid.model.getModel('modVendor');
var row = modelA.getFirstRow();
var newRow = modelB.createRow({
additionalConditions:[
{field:'Hanger__c', value: row.Hanger__c, operator:'=', nameFieldValue: row.Hanger__r.Name}
]}); //use Ctrl + Shift + J to open the javascript console in chrome //see if anything is undefined or any errors etc console.log(newRow); console.log(row);
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb
Hanger__c is a lookup field in Model A, but it is only a text field in the table (Model B).  The Snippet is being run from a Page Title button attached to Model A.  As for the ID number, this is what I'm getting:  a1PL00000024ldzMAA...
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK that's a little tricky, If you only want the name to display in model B, you can do this...

var $ = skuid.$;var modelA = skuid.model.getModel('modRFQ');var modelB = skuid.model.getModel('modVendor');
var row = modelA.getFirstRow();
modelB.createRow({
additionalConditions:[
{field:'Hanger__c', value: row.Hanger__r.Name}
]});
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb
No, no... I definitely want the value selected for Hanger__c in Model A... right now I'm only getting the RecordID I believe.  Basically, Model A - Hanger__c has a value of Hanger001.  I need this value to default to Model B - Hanger__c...
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Ok make sure you selected Hanger__r.Name in model A and console.log(row.Hanger__r.Name) and see what you get...
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb
Got it!
Awesome... thanks Moshe!