Storing the value of a detail field in a variable

Hi,
I need a variable to be populated with the information from a detailed field. This information will then be pulled into another variable - see below.
Any suggestions will be greatly appreciated :)
Many Thanks
Kevin
// get get detailed field value
var INV = $('#view_218-field_184').text();
// Define the fields to be updated
var data = {
field_279: INV
};

Hi Brad,

Many thanks for the link - will keep you updated :)

Check out this post Kevin https://support.knack.com/hc/en-us/community/posts/360019026412-Select-Default-Dropdown-on-Page-load-Javascript

I reckon Sunny posted some methods here.

This is not correct but illustrates my current thinking :)

$('#field_281 option').map(function () {
if ($(this).val() == INV) return this;
}).attr('selected', 'selected');

Hi Brad,

I am currently looking at doing something similar to this https://studentboxoffice.in/dotnet/article/4/119

With the value of #field_281 set to the value of INV or field 146. - still very early days :)

Hey Kevin, what have you tried so far mate?

Hi Brad,

You have been super helpful, but I have one more question;

If looking to update a (single select) connection field, how would one go about setting the value of the connected field to the same value as INV or field_146 (the connected field contains an identical value as one of the select options) ?

Any suggestions greatly appreciated :)
Many Thanks
Kevin

Hi Brad,

Works like a charm - you are an absolute legend :)

Many Thanks
Kevin

Your code to set the variable INV will result in 'undefined' as it's not properly formed JQuery.

Try using the data variable returned from the view-render:

INV = data.field_146;

Notice you don't need to re-initialise the variable with the var statement either.

Next step would be to check your variables work along the way with judicious use of console.log() statements - check out this for help on debugging JS https://www.w3schools.com/js/js_debugging.asp

Hi Brad,

Thank you so much for taking a look at this for me. Unfortunately your suggestion does not seem to work either.
What I am trying to do is, grab the field value from a detail view, then use that to update multiple table fields (https://support.knack.com/hc/en-us/community/posts/222365967-Update-multiple-records-with-a-checkbox). - below is my code in full.
Any suggestion great appreciated :)
Many Thanks
Kevin
var INV = [];
$(document).on('knack-view-render.view_218', function(event, view, data) {
var INV = ('field_146').value;
});
var addCheckboxes = function(view) {
// add the checkbox to to the header to select/unselect all
$('#' + view.key + '.kn-table thead tr').prepend('<th><input type="checkbox"></th>');
$('#' + view.key + '.kn-table thead input').change(function() {
$('.' + view.key + '.kn-table tbody tr input').each(function() {
$(this).attr('checked', $('#' + view.key + '.kn-table thead input').attr('checked') != undefined);
});
});
// add a checkbox to each row in the table body
$('#' + view.key + '.kn-table tbody tr').each(function() {
$(this).prepend('<td><input type="checkbox"></td>');
});
}
/**** CHANGE VIEW_ID TO YOUR OWN VIEW ID ****/
$(document).on('knack-view-render.view_544', function(event, view) {
// Add an update button
$('<button id="update""><b>Invoice</b></button>').insertAfter('.kn-records-nav');
// Add checkboxes to our table
addCheckboxes(view);
// Click event for the update button
$('#update').click(function() {
// We need an array of record IDs
var record_ids = [];
// Populate the record IDs using all checked rows
$('#' + view.key + ' tbody input[type=checkbox]:checked').each(function() {
record_ids.push($(this).closest('tr').attr('id')); // record id
});
Knack.showSpinner();
// Define the fields you want to update
var data = {
field_279: INV
};
// seet the delay to prevent hitting API rate limit (milliseconds)
var myDelay = 100;
//call updateRecords function
$(function() {
updateRecords(record_ids.shift(), record_ids, data);
});
var selectedRecords = record_ids.length + 1
function updateRecords(id, records, data) {
$.ajax({
//CHANGE OBJECT_ID TO YOUR OWN OBJECT ID
url: 'https://api.knack.com/v1/objects/object_20/records/' + id,
type: 'PUT',
/***** CHANGE TO YOUR OWN APPID AND API KEY HERE *****/
headers: {
'X-Knack-Application-ID': '??????????',
'X-Knack-REST-API-Key': '?????????'
},
data: data,
success: function(response) {
if (record_ids.length > 0) {
// Every time a call is made, the array is shifted by 1.
// If the array still has a length, re-run updateRecords()
setTimeout(updateRecords(record_ids.shift(), record_ids, data), myDelay);
} else {
alert(selectedRecords + " Updated");
Knack.hideSpinner();
location.reload();
}
}
})
}
})
});

Hi Kevin,

Is what you're doing not working? Is INV undefined?

Another option is to grab the data on scene-render (happens after all the views are rendered) using the Knack.models method.

E.g.

var INV = Knack.models['view_218'].toJSON().field_184;