How to reference a specific field value in javascript code

Hi guys,

In order to run power bi embedded inside my knack app, I need to use a dynamic token from time to time.

I already created the flow to always get the updated token value and it's being recorded to field_1934 inside my knack app.

This field_1934 is inside an object with only two fields. The increment field and a paragraph field and there is only one line on it. The increment 1 and the token info which is updated every 5 minutes.

So I need to insert in my javascript code the reference for this specific paragraph field where the token is updated.

Can anyone tell me the reference for the value inside this specific field_1934 value so I can add it to my javascript code?

Thanks in advance!

Will do Matt,

What does the browser console report? What's being shown on your screen?

(Chrome - right click, Inspector > console)

It'll tell us what line is throwing an error.

Another option might be to make a copy of the app and share it with me.

Hi Brad, No luck yet, but thanks for all your effort. I feel uncomfortable using so much of your time, so feel free to draw a line in the sand on this :)

No, I think there's an error in my code Matt.

2nd line should be:
var pdflink = Knack.models['view_1257'].toJSON().field_668.url;

And 3rd line:
$('iframe').attr('src') = pdflink;

Try keeping all your richtext code as is, but change the iframe line to:

<iframe src="" height="800" width="400" scrolling="no">

If that doesn't work then try again with exactly the same code you listed above for entire richtext view, including the hardcoded file and see if that works...

Hi Brad, didn't quite work, but I think we're getting close. I changed the iframe reference in the rich text box to src=, and I get this error:

{"error":"Application not found","application":"pdflink"}

By the way, one reason that I didn't put the script in the rich text box is that I think Knack are disabling scripts other than ones that are 'whitewashed' from running in side the pages - but anything goes in the javascripting section.,

Cheers

Matt

No worries Matt,

There's a few ways to do this include inserting the iframe purely in code, but then it gives you less control over where it's placed. So keeping your current views here's one way i think will work.

$(document).on('knack-scene-render.scene_677', function(event, scene) {
var pdflink = Knack.models['view_1257'].field_668.url;
$('iframe').attr('src', pdflink);
});

Keep in mind I haven't tested at all so let's see how it goes...

Brad, you're a champ.

At the top of these images I have the javascript. Below that, you can see a page, coming off a table (so there is a record linked to it), with 2 elements - the first, scene 1257 is a details thingy, and the 3rd field in that is hyperlink to a pdf shared in Google drive. The javascript is supposed to load that into the variable called pdflink, so that in the second element, a rich text box, I can open that very same file with an iframe. Note that there are 2 iframe references in the rich text box, the 2nd one is just a test, and has a hard-coded file reference, and works perfectly.

I am sure I am doing something blatantly stupid - I'm just bluffing my way through ths :)

Cheers Matt

![](upload://xOylWhL56y7OhxGR0yo7Sz39Iob.jpeg)

No sweat Matt, confident we'll work this out.

Can you post your current code and a screenshot of the console so we can look at what the data object includes?

Hi Brad,

Thanks for that advice. I think the field is all present and correct, but for some reason the variable is not being populated. I wish I knew some local javascriptors :) Thanks

Matt

Hi Matt,

Yes I admit I haven't participated in the Slack group much...

The url for the pdf should be contained in the data returned from the view-render - you might need to inspect the data closely to get it right though.

$(document).on('knack-view-render.view_1252', function (event, view, data) {
console.log(data); //Pay close attention to field_668
var pdflink = data.field_668_raw[0];
});

Hi Brad,

Great to hear from you! (I did also try a similar post in our slack group, but I am not sure that anyone is hanging out in there anymore).

Is there a way that I can get the variable link I need from a field displayed, rather than from the object field numbe?

Cheers

Matt

Hey Matt,

If you're trying to get pdflink when the 'view opens' try inside a view-render event.

And remove the extra (. If thise doesn't work try console.log(data); to see what's there and you might need to add that field to the view if field_668 isn't listed.

$(document).on('knack-view-render.view_1252', function (event, view, data) {

var pdflink = data.field_668_raw[0];
});

Hi guys, I have a really similar question. I want to grab a field variable (actually it is url link to a shared file on google drive) so that I can use that link in an iframe. The link is not shown in the view - it is in field 668 of the object. I have used this instruction, but it is not working. I have no knowledge of javascript, I am just making it up based on examples I can find :). one problem might be that I am trying to make this happen with the view open, but the form is not submitted?

$(document).on('knack-record-update.view_1252', function (event, view, record) {

var pdflink = (record.field_668_raw[0];
});

I really hope someone can help on this,

Cheers Matt

Remove [[[ and '

var embedConfiguration = {type: 'report',accessToken: myToken,id: '3c0ae9a0-550f-4920-b4c0-a26a29b76ee9',embedUrl: myURL};

If you need some help with your project, you can find me in the Knack Builder Network, or contact me

sergi@hoipoi.es
https://hoipoi.es/?lang=en

Its just to show you where the value of the token and url should be added. Can you please remove the [[ ]] and add the correct code to use the fields value that we searched before it? Tks!

Ok, if field_1936 is a text field with an url value simply add:

var myToken = data.records[0].field_1934); // HERE YOU GET THE TOKEN VALUE

var myURL = data.records[0].field_1936); // This line get the url value

And then in your embed configuration,

var embedConfiguration = {type: 'report',accessToken: '[[[myToken]]]',id: '3c0ae9a0-550f-4920-b4c0-a26a29b76ee9',embedUrl: '[[[myURL]]]'};

I'm not sure why the [[[...]]] are used in the field reference. I don't know if it will give you problems or you should just use the variable names without triple [[[

Let me know if it works for you, if not, I would have to do tests with the code in your live app...

Yes, field_1936 is a url field and is in the same object

In your knack object, field_1936 is an url field? I can see in yor screenshot only two fields, autoincrement and a paragraph field with the token value?

383433730112, thank you!

If I also need to get the url from field_1936, how is going to look the code?

Also, I need to insert the value from field_1934 and 1936 in this line:

var embedConfiguration = {type: 'report',accessToken: '[[[myToken]]]',id: '3c0ae9a0-550f-4920-b4c0-a26a29b76ee9',embedUrl: '[[[FIELD_1936]]]'};

How is going to look the final code?

Thanks a lot for your help!