How do I reference a database in triggered email?

I am fairly new to this so I am hoping to gain some insights into understanding how I can get this to work.

We have a collection of reports that we want send to users upon email input. I have gotten it to create contact and send the triggered email but I can not figure out how to set the variable in the email to have it link to the dynamic PDF file that is in the database.

I tried following this code to call on the database but when I reference as a variable for the triggered email I get an error that the variable “downloadLink” is not defined.

This is the tutorial that I am working from: https://codequeen.wixsite.com/email-download/the-code But instead of an image URL it’s a PDF file.

Any help would be appreciated!

import wixLocation from ‘wix-location’;
import wixCRM from ‘wix-crm’;

let picture ;
let url ;
let filename ;

$w. onReady (function () {
let link = wixLocation.url;
let item = $w(’ #dataset1 ').getCurrentItem();
picture = item. file ;
url = picture.split(“/”)[3];
filename = item.title;
let downloadLink = " https://static.wixstatic.com/media/$ { url }?dn=${ filename }" ;

Hello,

I see you’re defining the downloadLink variable as a template literal but the syntax is incorrect. Try enclosing it within back ticks like this:

let downloadLink = `https://static.wixstatic.com/media/${url}?dn=${filename}`;

See if that works for you!

Best regards,

Hey Miguel, I really appreciate you getting back to me. I changed the syntax per your suggestion and it’s still coming back as “not defined”:

import wixCRM from ‘wix-crm’ ;
import wixLocation from ‘wix-location’ ;

$w.onReady( function () {
$w( “#dataset1” ).onReady( () => {
let item = $w( ‘#dataset1’ ).getCurrentItem();
let link = wixLocation.url;
let picture = item.file;
let url = picture.split( “/” )[ 3 ];
let filename = item.title;

let downloadLink = https: //static.wixstatic.com/media/${url}?dn=${filename};

})
});

export function downloadButton_click(event) {
wixCRM.createContact({
“firstName” : $w( ‘#firstName’ ).value,
“lastName” : $w( “#lastName” ).value,
“emails” : [$w( “#emailInput” ).value]

})
.then((contactId) => {
wixCRM.emailContact( “S8hEdWy” , contactId, {
“variables” : {
“name” : $w( ‘#firstName’ ).value,
“downloadLink” : downloadLink
}
})
.then(() => {
// do something after the email was sent
})
. catch ((err) => {
// handle the error if the email wasn’t sent
});
});
}

@zhenwxie

DO NOT SHOW CODE IN AN IMAGE!
DO IT IN CODE-TAGS!

Either no-one will write all this code from the pic to help you.
I am surely to lazy for such unnecessary and time-killing processes.

And it is normal that your download-link is not defined, because…

Ohhh damn, i can’t show you, because i have NO CODE which i can use.

Set the downloadlink-variable as GLOBAL.
OR
Save it in local-storage. (not the best idea)
OR
Put your BUTTON-Event of “download-button” into the onReady-function.

You have surely enough possibilities, how to solve it.

@russian-dima Thank you for the tips! Noted for the future to include code. haha