Set Form Field Value on Render with Javascript

The existing sample code in the documentation doesn’t work (below) for setting the value of a blank field on the form render. I am wondering if anyone knows how this can be done now.

$(document).on('knack-view-render.view_52', function(event, view, data) {
	// The connected record id value
	var connectionId = "5b2ac7fc66b6036b8f859c45";

	// Update the view and field ids for your application
	$('#view_52-field_3').val(connectionId);
	$('#view_52-field_3').trigger("liszt:updated");
});

Hi @brendanosu80224,

Thank you for sharing! Please consider reaching out to our support team to report this sample code as not working for your app. They will be able to troubleshoot this for you and report the issue to our engineering team if it ends up being a platform issue.

You can reach out to them via this form: Create Support Ticket

Hi Les,

I already reached out and they confirmed the sample code isn’t working and referred it to engineering.

Thanks

1 Like

Hi @brendanosu80224

Can you please post your code that you used.

We use this code quite a lot through our app.

Craig

hey,

the code posted in the first message is the code i am using.

thanks
brendan

Hi @brendanosu80224

If that’s the case then the issue is that the code is an example snippet it’s not to be copied and used.

There are 3 variables

  1. View Id= view_xyz this can be found when you select the view in the builder then look at the URL and the view_… Is your viewId
  2. Field Id= field_123 you can find this in the builder by going to data the select the object then just above the fields section is a toggle to turn field IDs on or off then find the field for the select.
  3. Connection Id - this is a little trickier to get dynamically but you can usually find this in the app URL somewhere depending on what you are trying to do if the id you’re after is not there then you can use a view based API calls to get this.

If you want more help with this then please DM me and we can set up a meeting to discuss further.

This is not an easy thing to do it took us about a year to figure it out and now we use this quite a lot in our app.

Craig

hey @CSWinnall,

i appreciate it. i understand how this code is supposed to work. i use javascript in my apps. the issue is that this method not working, which knack’s support has already confirmed for me and referred the issue to engineering because the sample code they use in the documentation no longer works. they also told me that they don’t support javascript issues, so referred me here in case people have had success using other methods.

thanks
brendan

We use this code all the time a lot in our app probably about 10 different forms in variations of this code. It does work so not sure why support would say that.

I’d be happy to take a look at your code to see if I can help.

I’m away for the weekend but can post some examples here on Monday

1 Like

Hi @brendanosu80224

Here are some examples of us using this code in our live app:

$('#view_4860-field_6144').val(newCareMngrId).trigger('liszt:updated');

function addConnectionIdToRecord (viewId, conxFieldId, connectionId = null) {
        const conxId = connectionId ? connectionId : getRecordID(); //Get Record Id from URL if no record Id passed in
        const viewElement = $(`#${viewId}`).length ? $(`#${viewId}`) : $(`#connection-form-view:has(input[value="${viewId}"])`);
        const assessConx = viewElement.find(`#kn-input-field_${conxFieldId}`);

        assessConx.addClass(CLASS_HIDDEN);
        assessConx.find('select').val(conxId).trigger('liszt:updated');
    }

Both theses examples use the code in the example snippet from the dev wiki and are working fine in our app. The function addConnectionIdToRecord is used multiple times throughout our app.

Craig

hey @CSWinnall,

i appreciate it. i just tested the method on a different account and it’s working there, so it appears to be an issue with my actual account or app. i’m just gonna make a copy of the problem app and see if that solves the problem.

thanks
brendan

1 Like