How do I auto Populate a field. For example is I tag a customer I want it to populate the address field.
9212997488 Crum Hi Mark. If i understood you correctly, you should try this code:
$(document).on('knack-view-render.view_XX', function(event, view) {
var customer = document.querySelector("select#view_XX-field_YY.select"); // view_XX is your Form view. field_YY is your connection (Customer) field
function get_address(id, callback){
let headers = {'X-Knack-Application-Id': 'Your Application ID', 'X-Knack-REST-API-Key': 'knack', 'Authorization': Knack.getUserToken()};
let api_url = 'https://api.knack.com/v1/pages/scene_A/views/view_B/records/' + id; // This should point to a table view in your app where at least the one of the columns is for field_YY and the view should 'technically' be available to the logged-in user
$.ajax({
url: api_url,
type: 'GET',
headers: headers,
success: function(response) {
callback(response);
}
})
}
function put_address(data){
if(data.field_ZZ !== "" && data.field_ZZ !== undefined){ // field_ZZ is your Address field from the Customer record
document.querySelector("#kn-input-field_WW #street").value = data.field_ZZ_raw.street; // field_WW is the Address field in your Order form
document.querySelector("#kn-input-field_WW #street2").value = data.field_ZZ_raw.street2;
document.querySelector("#kn-input-field_WW #city").value = data.field_ZZ_raw.city;
document.querySelector("#kn-input-field_WW #state").value = data.field_ZZ_raw.state;
document.querySelector("#kn-input-field_WW #zip").value = data.field_ZZ_raw.zip;
} else {
alert("No address found! ");
console.error("No address found! ");
}
}
function clear_address(){
document.querySelector("#kn-input-field_WW #street").value = "";
document.querySelector("#kn-input-field_WW #street2").value = "";
document.querySelector("#kn-input-field_WW #city").value = "";
document.querySelector("#kn-input-field_WW #state").value = "";
document.querySelector("#kn-input-field_WW #zip").value = "";
}
$("select#view_XX-field_YY.select").on('change', () => {
if(customer.value === "" || customer.value === undefined){
clear_address();
console.log("No Customer selected!");
} else {
get_address(customer.value, put_address);
}
});
});
I am not sure why? Here is what I want to do! Photos are inserted
![](upload://jRcd0U9tNupYNwMxyBWWgEutnjt.png)![](upload://awBaStGZqZrNp6PBJpJYlBC9eZZ.png)
Hi Mark - default field values not enough?
I think you might have more complex requirements that needs further explanation?