0
I have this code that goes in the Master Data VTEX (bank) and makes a query in a given email. If you have this email he has to bring the customer’s phone number.
How do I save the result of the object in a variable? It only appears on the console. The way I did not show the result in the code.
function ContactCreateByEmail(storeName, dataEntity, CriteoEmail) {
var cl_url = "https://api.vtexcrm.com.br/|||||||||||||||||/dataentities/|||||||||||||||||/search?_fields=homePhone&_where=email={{CriteoEmail}}";
$.ajax({
headers: {
"Accept": "application/vnd.vtex.ds.v10+json",
"Content-Type": "application/json"
},
type: 'GET',
url: cl_url,
success: function(data) {
var retorno_ajax = data[0]; //or something similar
if (retorno_ajax == null) {
console.log('nenhum dado retornado');
}
else {
console.log('retorno_ajax');
}
},
});
__blc['id'] = "61ab36efda06b8c498209f4d0c725948";
try {
lc.sendData({
evento: "sms_transacional",
transactionId: "{{transactionId}}",
customer_id: "{{CriteoEmail}}",
numero: (retorno_ajax),
});
} catch (e) { }
}
ContactCreateByEmail('duloren', 'CL', '{{CriteoEmail}}') <
But you’re already saving, look at that line:
var retorno_ajax = data[0]
.– Ricardo Pontual
You can declare this variable at the beginning of the code
javascript
, out of function to gain access to other parts of the code– Ricardo Pontual
Correct, but it only appears on the console... Downstairs I’m calling the variable to appear as a result in the API and it does not appear...and if I give an Alert with the variable it does not show the phone...it appears "Object Object"
– Carlos Nogueira
This variable is declared inside the block
success
of the callAjax
, therefore cannot be accessed elsewhere. Do as I mentioned above, declare it out of thefunction ContactCreateByEmail
, then you should be able to access it both in the return of Ajax and in another part of your javascript.– Ricardo Pontual
@Ricardopunctual I did and it didn’t work. Maybe I did it wrong. Please edit and send me?
– Carlos Nogueira
You can put your complete code, and where you are using the variable?
– Ricardo Pontual
@Ricardopunctual this is the complete code...this is a transactional email code that I have to return some values, then I am adding it in the event of finalizing purchase. I am adding via Tag Manager. This one is complete
– Carlos Nogueira