Controll the order in which things happen

I have made a form that sends an email via triggered mails forms, and it is working fine, however i also want to make a form dataset collect the info into a collection too. Probblem is, when All is set and I click submit, the dataset runs first, and since it resets every input after submiting, the emails are arriving empty. is there a way to make the code that send the triggered email form run before the dataset start collecting, or add a delay to the dataset function?

I’ve been studying it, but if anyone can point me through the right direction, it will be much appreciated.

import { contacts } from 'wix-crm';
import { triggeredEmails } from 'wix-crm';

$w.onReady(function () {

    $w("#submit").onClick(() => {

      const contactInfo = {
            name: {
                first: $w("#name").value,
            },
            emails: [{
                email: $w("#name").value
            }]
        };
        contacts.appendOrCreateContact(contactInfo)
            .then((resolvedContact) => {
                if (resolvedContact) {
                    $w("#thanksText").show();
                    //send email to admin

                    const MY_ID = "<add admin ID here>";
                    triggeredEmails.emailMember('Tprof', MY_ID, {
                            variables: {
                                name: $w("#name").value,
                                endereco: $w("#endereco").value,
                                cidade: $w("#cidade").value,
                                estado: $w("#estado").value,
                                funcao: $w("#funcao").value,
                                email: $w("#email").value,
                                empresa: $w("#empresa").value,
                                cep: $w("#cep").value,
                                rg: $w("#rg").value,                                                     
                                resposta1: $w("#resposta1").value,
                                resposta2: $w("#resposta2").value,
                                resposta3: $w("#resposta3").value,
                                resposta4: $w("#resposta4").value,
                                resposta5: $w("#resposta5").value,
                                resposta6: $w("#resposta6").value,
                                resposta7: $w("#resposta7").value,
                                resposta8: $w("#resposta8").value,
                                resposta9: $w("#resposta9").value,
                                resposta10: $w("#resposta10").value,
                                resposta11: $w("#resposta11").value,
                                resposta12: $w("#resposta12").value,
                                resposta13: $w("#resposta13").value,
                                resposta14: $w("#resposta14").value,
                                resposta15: $w("#resposta15").value,
                                resposta16: $w("#resposta16").value
                            }
                        })
                        .then(() => {
                            console.log("email sent")
                        })
                        .catch((err) => {
                            console.log("error", err)
                        })
                }
            })
    })
});

Hi @lucassmdesign ,

Before submitting the data, store them into a JS object, then submit them, after that, it doesn’t matter if the data from the input fields have been deleted since they’re already stored in a variable, you can then use the variable to get the data into the dataset.

If you need any help, just let me know.

Hey man, thanks for answering. Took me some while trying to research it since I dont wanna botter you, but fact is, I’m at a loss here :sweat_smile:

Could you point me a direction on were can I learn how to do it, or show me how it is done? It would be verry kind

Following Ahmad’S suggestion, it could look like …

import{contacts} from 'wix-crm';
import{triggeredEmails} from 'wix-crm';

$w.onReady(function(){$w("#submit").onClick(()=>{
	let myObjectCopy = { };
	//your already generated code here.....
	//your already generated code here.....
	//your already generated code here.....

	$w("#submit").onClick(()=>{		
		myObjectCopy.name = $w("#name").value;
		myObjectCopy.endereco= $w("#endereco").value;
		myObjectCopy.cidade= $w("#cidade").value;
		//..continue...

		//your already generated code here.....	
		//your already generated code here.....	
		//your already generated code here.....	

		.then(()=>{console.log("email sent");
			console.log("My-Object-Data: ", myObjectCopy);
		})
		.catch((err)=>{console.log("error",err)})
});

Also take a look onto → CONSOLE.

Hey man, so I’ve been trying to apply this to the code, but it is not working. I am certainly dooing it wrong. The way I’m dooing it, it is stoping everything to work. Could you give it a look? I thank you very much for your help!
(note: i did not quoted it as code snippet because it is breaking the lines in a weird way)


import { contacts } from ‘wix-crm’ ;
import { triggeredEmails } from ‘wix-crm’ ;

$w . onReady ( function () {

        $w ( "#submit" ). onClick (() => { 
            **let**  myObjectCopy  = {}; 
            **const**  contactInfo  = { 
                name : { 
                    first :  $w ( "#name" ). value , 
                }, 
                emails : [{ 
                    email :  $w ( "#name" ). value 
                }] 
            }; 

        $w ( "#submit" ). onClick (() => { 
                    myObjectCopy . name  =  $w ( "#name" ). value ; 
                    myObjectCopy . endereco  =  $w ( "#endereco" ). value ; 
                    myObjectCopy . cidade  =  $w ( "#cidade" ). value ; 
                    myObjectCopy . estado  =  $w ( "#estado" ). value ; 
                    myObjectCopy . funcao  =  $w ( "#funcao" ). value ; 
                    myObjectCopy . email  =  $w ( "#email" ). value , 
                    myObjectCopy . empresa  =  $w ( "#empresa" ). value ; 
                    myObjectCopy . cep  =  $w ( "#cep" ). value ; 
                    myObjectCopy . rg  =  $w ( "#rg" ). value ; 
                    myObjectCopy . resposta1  =  $w ( "#resposta1" ). value ; 
                    myObjectCopy . resposta2  =  $w ( "#resposta2" ). value ; 
                    myObjectCopy . resposta3  =  $w ( "#resposta3" ). value ; 
                    myObjectCopy . resposta4  =  $w ( "#resposta4" ). value ; 
                    myObjectCopy . resposta5  =  $w ( "#resposta5" ). value ; 
                    myObjectCopy . resposta6  =  $w ( "#resposta6" ). value ; 
                    myObjectCopy . resposta7  =  $w ( "#resposta7" ). value ; 
                    myObjectCopy . resposta8  =  $w ( "#resposta8" ). value ; 
                    myObjectCopy . resposta9  =  $w ( "#resposta9" ). value ; 
                    myObjectCopy . resposta10  =  $w ( "#resposta10" ). value ; 
                    myObjectCopy . resposta11  =  $w ( "#resposta11" ). value ; 
                    myObjectCopy . resposta12  =  $w ( "#resposta12" ). value ; 
                    myObjectCopy . resposta13  =  $w ( "#resposta13" ). value ; 
                    myObjectCopy . resposta14  =  $w ( "#resposta14" ). value ; 
                    myObjectCopy . resposta15  =  $w ( "#resposta15" ). value ; 
                    myObjectCopy . resposta16  =  $w ( "#resposta16" ). value ; 

                contacts . appendOrCreateContact ( contactInfo ) 
                    . then (( resolvedContact ) => { 
                        **if**  ( resolvedContact ) { 
                            $w ( "#thanksText" ). show (); 
                            //send email to admin 

                            **const**  MY_ID  =  "8f3718a1-8f09-4882-8b9f-72a067661b74" ; 
                            triggeredEmails . emailMember ( 'Tprof' ,  MY_ID , { 
                                    variables : { 
                                        name :  myObjectCopy . name , 
                                        endereco :  myObjectCopy . enderec , 
                                        cidade :  myObjectCopy . cidade , 
                                        estado :  myObjectCopy . estado , 
                                        funcao :  myObjectCopy . funcao , 
                                        email :  myObjectCopy . email , 
                                        empresa :  myObjectCopy . empresa , 
                                        cep :  myObjectCopy . cep , 
                                        rg :  myObjectCopy . rg , 
                                        resposta1 :  myObjectCopy . resposta1 , 
                                        resposta2 :  myObjectCopy . resposta2 , 
                                        resposta3 :  myObjectCopy . resposta3 , 
                                        resposta4 :  myObjectCopy . resposta4 , 
                                        resposta5 :  myObjectCopy . resposta5 , 
                                        resposta6 :  myObjectCopy . resposta6 , 
                                        resposta7 :  myObjectCopy . resposta7 , 
                                        resposta8 :  myObjectCopy . resposta8 , 
                                        resposta9 :  myObjectCopy . resposta9 , 
                                        resposta10 :  myObjectCopy . resposta10 , 
                                        resposta11 :  myObjectCopy . resposta11 , 
                                        resposta12 :  myObjectCopy . resposta12 , 
                                        resposta13 :  myObjectCopy . resposta13 , 
                                        resposta14 :  myObjectCopy . resposta14 , 
                                        resposta15 :  myObjectCopy . resposta15 , 
                                        resposta16 :  myObjectCopy . resposta16 
                                    } 
                                }) 
                                . then (() => { 
                                    console . log ( "email sent" ); 
                                    console . log ( "My-Object-Data: " ,  myObjectCopy ); 
                                }) 
                                . **catch** (( err ) => { 
                                    console . log ( "error" ,  err ) 
                                }) 
                        } 
                    }) 
            }) 
        }) 

});