Landing page Code HELP!!!

Hello everyone,
Just for the record, I don’t understand code at all. I need help!
I have my wix website. I already have a Landing page with a wix code form on it, connected to my database and a triggered email ready to go. I´ll promote my ebook trough the web so people could input name, email and a dropdown enquiry in exchange the Ebook.

It works on preview mode, the input goes to my database and a “thankyou” page opened after click on the button. BUT, when published, it send the triggered email, doesn´t open the “thankyou” page and neither save the input to my database.

I finded a few youtube tutorials, but had no luck.
Please, could someone help me to fix my landing page code to do that??

First of all you should be using a function rather and the onReady event. Second it doesn’t seem to be anything triggering a thank you page, the code seems a bit odd, it should be something this.

Create an onClick event and add this code to it, modify it as I tell you.

export function button1_click (event) {

let contactId

WixCRM.email contact.... // save contact
.then((contactId) =>{ 

console.log(contactId) //To make sure it saved properly
$w('#tahnkYouPage).show(); //Shoe your thank you page
//you can send your email here

}) .catch( (error) => {
    let errorMsg = error.message;
    console.log(errorMsg) //to see if you're getting eny error
  } );

}
1 Like

[@carlosgalvarez] Actually it is fine to declare an event handler in the onReady function in this way. Things can get confusing if there is a second one hooked up in the property panel though. So assuming there isn’t one connected to the property panel for the button this should run fine.

[@suinveste] You need to be aware that wixCRM May generate errors if you are already logged into the site. So you have to make sure you aren’t. If the user is logged in then you should use wixUsers to send the email.

I would suggest that you add a catch() after your then() function to see if you can get the error that is likely to be being generated.

So remove the ; at the end of line 16 and add this immediately after

.catch((error) => {
console.log(error.message);
});

See what that does.

Also please share you web site url then we can examine the behavior.

Cheers

1 Like

Hi Carlos!
Thank you for your reply.
Do I have to especify a place to save the contact? I would like to be save in my dataset, so how the code should be?

Giorgio

[@stevecropper] I am still working on it. Will do some chances tomorow and see what happens.
Thank you for your advice.

URL:
https://www.suinveste.com/eb1

[@stevecropper] I am still working on it. Will do some chances tomorow and see what happens.
Thank you for your advice.

URL:
https://www.suinveste.com/eb1

Hi [@stevecropper] Thank you very much !
I just ad the catch error and I am working some tests.
I am trying to solve the save in my dataset problem first and using the code that [@carlosgalvarez] sent to us.

URL :
https://www.suinveste.com/eb1

@suinveste Hi There: I have had a look at the code you have on your site. Some things to note:

  1. You need to check the type of the error message that is returned from the functions that you use. If you take a look at the wixCRM API the response that is returned is an Error message not an Error object
Fulfilled - The ID of the new or updated contact. 
Rejected - Error message

So Carlos’ suggestion would work if you were getting an object. So simply console.log(error) not error.message.

  1. You are using a custom field in your wixCRM.createContact() call named dropd.
    You need to make sure that the Contact Manager in your dashboard has a property called dropd.


If you don’t have a Custom field called dropd or you have a Custom field with a different name then you should use the name here. So your dropdown “Pretendo investir” returns an investment amount Ate U$ 100.000 for example. If you have a Custom field in your Contact Manager named “Investir” then your createContact would need to look like this:

$w.onReady(function () {
    $w("#button1").onClick(() => {
        wixCRM.createContact({
             "name": $w("#input1").value,
             "emails": [$w("#input2").value],
             "Investir": $w('#dropdown1').value // <<<<<<<<<<<<<<<
         })
         .then((contactId) => {
         // Use return here so we can catch ALL errors
             return wixCRM.emailContact("Ebook01", contactId, {
                 "variables": {
                     "name": $w("#input1").value,
                 }
             });
         }).catch((error) => {
            console.log(error); // <<<<<<<<<< Only display error
         });
    });
});

Lastly, also note that the Custom field has a type and you need to make sure that the type you assign to the field in your createContact call matches that type.

Steve

1 Like

@stevendc
Thank you again for your advice.
It is almost done. I put a new code on test and it´s working.
www.suinveste.com/eb3

The only challenge now it is to save on my Contact list.
I created a lable to get where the user came from. Now it´s saving on database but it´s not creating a new contact at my contact list, although my code says to do it.

Giorgio

@suinveste Hi Giorgio: I looked at you page and it seems you are having the same issue that I just helped someone else fix. Take a look at this post:

Pay attention to the need to create copies of the .value fields from your form. You are also binding these to a dataset which will reset the values after a save event so the triggered email will likely fail.

My suggestion is try to use my answer in this post and modify your code to fire using the onAfterSave event instead of using the onClick event as you have right now.

See if that fixes your problem.

Steve

1 Like

@stevendc hi steve. It seems like you have a lot of experience with code in wix. Could you help me with an issue i have. I posted in the forum more than a week ago but haven’t get any replies from moderators. An user suggested me something but it didn’t work. Any suggestions or feedback will be appreciated. Thanks

@stevendc Hey Steve,

As you writed before
“1. After a dataset save the field values linked to the data set are cleared. When the onAfterSave handler fires all values are available because the handler is called before the dataset operations have completed. The problem comes with the wixCRM create call. This is an asynchronous function call. So it doesn’t happen immediately it is queued to run when the dataset function completes. This means that the create call works fine BUT the subsequent email trigger fails because all of the form values are empty :-).”

I do understand the issue, but couldn´t fix it.

  • What´s hapenning:
    It´s saving on my Live database but not on my Contact List.
    It´s not sending the the triggered emails.

I update the code:
www.suinveste.com/eb3

Thank you so much for your help!
Giorgio

@suinveste Giorgio: There seems to be several issues with your code:

import wixCRM from ‘wix-crm’;

$w.onReady( function () { <<<<<<<<<<<<<<< onReady starts here
$w(“#dataset1”).onAfterSave( () => { <<<<<<<<<<<<<<< onAfterSave starts here
let name = $w(“#input1”).value;
let emails = $w(“#input2”).value;
let aporte = $w(‘#dropdown1’).value;
console.log(“After save”);
} ); <<<<<<<<<<<<<<< onAfterSave ends here so it essentially does nothing

/************************************************************************************
This code will run as soon as the onReady handler is executed just after the page
loads and never again because it is in the on ready handler scope
**************************************************************************************/
wixCRM.createContact( {
“name”: $w(“#input1”).value,
“emails”: [$w(“#input2”).value],
“aporte”: $w(‘#dropdown1’).value
} )
.then( (contactId) => {
return wixCRM.emailContact(“Ebook01”, contactId, {
“variables”: {
“name”: $w(“#input1”).value,
}
} );
} );
} ); <<<<<<<<<<<<<<< onReady ends here so

/**************************************
The following code is in the global scope and will execute immediately it is seen by the page when loading so could generate an error.


$w(“#dataset1”).save()
.then( (item) => {
let fieldValue = item.fieldName;
} )
. catch ( (err) => {
let errMsg = err;
} );

Giorgio: It is important to understand the scope concept in javascript and where the parenthesis ‘()’ and curly braces ‘{}’ need to go for your code to work. These are matched pairs and if you put code where it isn’t supposed to be then the code will not work.

I suggest you take a look at some on line tutorials like w3schools.com where you will learn the basics and be able to practise testing concepts in javascript out.

Cheers
Steve