Best answer

custom formatting function not working as expected


Userlevel 1

I’m updating fattura24 App for Zapier. In my scripting.js file I added a function to format numbers like this:

 

function numberFormat(number) {
var strResult = +number
.toString()
.replace(/,/g, '.')
.replace(/\.(?=.*\.)/g, '')
.replace(/[^0-9.]/g, '');

return Number(strResult);
}

I sent two test values: first “10,00”, second “1,000.00”. In the first scenario I should get “10” as a result, but I actually got “1000”; in the second I should get “1000” but I got a NaN. Any suggestions? Thanks

icon

Best answer by Danvers 1 June 2020, 17:28

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

12 replies

Userlevel 7
Badge +8

Hey there @Davide - Thank you for your message! I’ve sent this to Dev App support to ensure you’re getting sound advice about your function to format numbers.

Stay tuned!

Userlevel 7
Badge +12

Hi @Davide - Just tried your function and it seems to work as intended.

 

Userlevel 1

I created a Zap Test using Google sheets and passing as price value (“50,00”) I obtain 5000 instead of 50

Userlevel 7
Badge +12

@Davide - Strange, the function would return 50 if you pass “50,00” to it. Have you checked that the value in your spreadsheet is not transformed somehow to “5000”? It might be worth it if you checked the zap history to make sure you’re passing “50,00” to the function.

Userlevel 1

 

Here the data I used as sample. 

Userlevel 1

Here below the bundle log of my latest try:

 

Zap.create_document_v2_post_write ran at April 9, 2020, 2:40 p.m. as 7c754a19

the bundle:

{
"response": {
"status_code": 200,
"headers": {
"Server": "Apache-Coyote/1.1",
"Content-Length": "157",
"Date": "Thu, 09 Apr 2020 14:40:37 GMT"
},
"content": null
},
"zap": {},
"auth_fields": {
"api_key": "**********************************"
},
"request": {
"url": "https://www.app.fattura24.com/api/v0.3/SaveDocument",
"method": "POST",
"auth": null,
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json"
},
"params": {
"apiKey": "********************************",
"xml": "<?xml version='1.0' encoding='UTF-8'?><Fattura24><Document><Rows><Row><Description>prodotto bove</Description><Qty>5</Qty><Price>819.6721311475409</Price><Discounts>0</Discounts><VatCode>22</VatCode></Row></Rows><Date>2020-04-9</Date><DocumentType>I-force</DocumentType><SendEmail>false</SendEmail><VatDescription>22</VatDescription><CustomerName>Ingrassia Ciccio</CustomerName><CustomerFiscalCode>aaa</CustomerFiscalCode><payment>[object Object]</payment><customer>[object Object]</customer><general>[object Object]</general><TotalWithoutTax>-851.6393442622953</TotalWithoutTax><VatAmount>901.6393442622953</VatAmount><Total>50</Total><Payments><Payment><Paid>No</Paid><Amount>50</Amount><Date>2020-04-9</Date></Payment></Payments></Document></Fattura24>"
},
"data": null,
"files": {},
"allow_redirects": false
},
"action_fields": {
"customer": [
{
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa"
}
],
"general": [
{
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": 1.0,
"Total": 5000.0
}
],
"payment": [
{
"Paid": false
}
],
"products": [
{
"Discounts": 0.0,
"Description": "prodotto bove",
"Qty": 5,
"Price": 1000.0,
"ProductPriceIncludesTaxes": true,
"ProductVatValue": 22.0,
"VatDescription": "22",
"ProductVatType": "PercentageRate"
}
]
},
"action_fields_full": {
"Discounts": "0",
"Paid": "No",
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": "1",
"Total": "\u20ac 50,00",
"Description": "prodotto bove",
"Qty": "5",
"Price": "\u20ac 10,00",
"ProductPriceIncludesTaxes": true,
"ProductVatValue": "22",
"VatDescription": "22",
"ProductVatType": "PercentageRate",
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa",
"payment": [
{
"Paid": false
}
],
"products": [
{
"Discounts": 0.0,
"Description": "prodotto bove",
"Qty": 5,
"Price": 1000.0,
"ProductPriceIncludesTaxes": true,
"ProductVatValue": 22.0,
"VatDescription": "22",
"ProductVatType": "PercentageRate"
}
],
"customer": [
{
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa"
}
],
"general": [
{
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": 1.0,
"Total": 5000.0
}
]
},
"action_fields_raw": {
"Discounts": "0",
"Paid": "No",
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": "1",
"Total": "{{83637924__COL$G}}",
"Description": "{{83637924__COL$D}}",
"Qty": "{{83637924__COL$F}}",
"Price": "{{83637924__COL$E}}",
"ProductPriceIncludesTaxes": true,
"ProductVatValue": "22",
"VatDescription": "22",
"ProductVatType": "PercentageRate",
"CustomerName": "{{83637924__COL$B}} {{83637924__COL$A}}",
"CustomerFiscalCode": "aaa"
},
"meta": {
"frontend": true
}
}
the results:

[
{
"description": "Operation completed successfully",
"documentId": "9519107",
"documentNumber": "60-2020"
}
]
the logs:

no logs recorded!



Zap.create_document_v2_pre_write ran at April 9, 2020, 2:40 p.m. as 47fb679f

the bundle:

{
"request": {
"url": "https://www.app.fattura24.com/api/v0.3/SaveDocument",
"method": "POST",
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json"
},
"params": {
"apiKey": "************************************"
},
"data": "{\"customer\": [{\"CustomerName\": \"Ingrassia Ciccio\", \"CustomerFiscalCode\": \"aaa\"}], \"general\": [{\"DocumentType\": \"I-force\", \"SendEmail\": false, \"AmountsFormat\": 1.0, \"Total\": 5000.0}], \"payment\": [{\"Paid\": false}], \"products\": [{\"Discounts\": 0.0, \"Description\": \"prodotto bove\", \"Qty\": 5, \"Price\": 1000.0, \"ProductPriceIncludesTaxes\": true, \"ProductVatValue\": 22.0, \"VatDescription\": \"22\", \"ProductVatType\": \"PercentageRate\"}]}",
"files": {}
},
"zap": {},
"auth_fields": {
"api_key": "**********************************"
},
"url_raw": "https://www.app.fattura24.com/api/v0.3/SaveDocument",
"action_fields": {
"customer": [
{
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa"
}
],
"general": [
{
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": 1.0,
"Total": 5000.0
}
],
"payment": [
{
"Paid": false
}
],
"products": [
{
"Discounts": 0.0,
"Description": "prodotto bove",
"Qty": 5,
"Price": 1000.0,
"ProductPriceIncludesTaxes": true,
"ProductVatValue": 22.0,
"VatDescription": "22",
"ProductVatType": "PercentageRate"
}
]
},
"action_fields_full": {
"Discounts": "0",
"Paid": "No",
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": "1",
"Total": "\u20ac 50,00",
"Description": "prodotto bove",
"Qty": "5",
"Price": "\u20ac 10,00",
"ProductPriceIncludesTaxes": true,
"ProductVatValue": "22",
"VatDescription": "22",
"ProductVatType": "PercentageRate",
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa",
"payment": [
{
"Paid": false
}
],
"products": [
{
"Discounts": 0.0,
"Description": "prodotto bove",
"Qty": 5,
"Price": 1000.0,
"ProductPriceIncludesTaxes": true,
"ProductVatValue": 22.0,
"VatDescription": "22",
"ProductVatType": "PercentageRate"
}
],
"customer": [
{
"CustomerName": "Ingrassia Ciccio",
"CustomerFiscalCode": "aaa"
}
],
"general": [
{
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": 1.0,
"Total": 5000.0
}
]
},
"action_fields_raw": {
"Discounts": "0",
"Paid": "No",
"DocumentType": "I-force",
"SendEmail": false,
"AmountsFormat": "1",
"Total": "{{83637924__COL$G}}",
"Description": "{{83637924__COL$D}}",
"Qty": "{{83637924__COL$F}}",
"Price": "{{83637924__COL$E}}",
"ProductPriceIncludesTaxes": true,
"ProductVatValue": "22",
"VatDescription": "22",
"ProductVatType": "PercentageRate",
"CustomerName": "{{83637924__COL$B}} {{83637924__COL$A}}",
"CustomerFiscalCode": "aaa"
},
"meta": {
"frontend": true
}
}
the results:

{
"url": "https://www.app.fattura24.com/api/v0.3/SaveDocument",
"method": "POST",
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json"
},
"params": {
"apiKey": "***********************************",
"xml": "<?xml version='1.0' encoding='UTF-8'?><Fattura24><Document><Rows><Row><Description>prodotto bove</Description><Qty>5</Qty><Price>819.6721311475409</Price><Discounts>0</Discounts><VatCode>22</VatCode></Row></Rows><Date>2020-04-9</Date><DocumentType>I-force</DocumentType><SendEmail>false</SendEmail><VatDescription>22</VatDescription><CustomerName>Ingrassia Ciccio</CustomerName><CustomerFiscalCode>aaa</CustomerFiscalCode><payment>[object Object]</payment><customer>[object Object]</customer><general>[object Object]</general><TotalWithoutTax>-851.6393442622953</TotalWithoutTax><VatAmount>901.6393442622953</VatAmount><Total>50</Total><Payments><Payment><Paid>No</Paid><Amount>50</Amount><Date>2020-04-9</Date></Payment></Payments></Document></Fattura24>"
},
"data": "{\"customer\": [{\"CustomerName\": \"Ingrassia Ciccio\", \"CustomerFiscalCode\": \"aaa\"}], \"general\": [{\"DocumentType\": \"I-force\", \"SendEmail\": false, \"AmountsFormat\": 1.0, \"Total\": 5000.0}], \"payment\": [{\"Paid\": false}], \"products\": [{\"Discounts\": 0.0, \"Description\": \"prodotto bove\", \"Qty\": 5, \"Price\": 1000.0, \"ProductPriceIncludesTaxes\": true, \"ProductVatValue\": 22.0, \"VatDescription\": \"22\", \"ProductVatType\": \"PercentageRate\"}]}",
"files": {}
}
the logs:

> "INFO\tZID: d8a6eb0b66e0 ZRUN: 2 RSSMEM: {\"rss\":71929856,\"heapTotal\":44863488,\"heapUsed\":24845600,\"external\":325152}"



Zap.test_api_key_post_poll ran at April 9, 2020, 2:38 p.m. as 641bfe72

the bundle:

{
"response": {
"status_code": 200,
"headers": {
"Server": "Apache-Coyote/1.1",
"Content-Length": "155",
"Date": "Thu, 09 Apr 2020 14:38:36 GMT"
},
"content": null
},
"zap": {},
"auth_fields": {
"api_key": "**************************************"
},
"request": {
"url": "https://www.app.fattura24.com/api/v0.3/TestKey",
"method": "GET",
"auth": null,
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json"
},
"params": {
"apiKey": "**************************************"
},
"data": null
},
"trigger_fields": {},
"meta": {
"auth_test": true
}
}
the results:

[
{
"returnCode": "1",
"description": "Complimenti, la tua API KEY &egrave; corretta."
}
]
the logs:

> "INFO\tZID: d8a6eb0b66e0 ZRUN: 1 RSSMEM: {\"rss\":37183488,\"heapTotal\":10772480,\"heapUsed\":6933456,\"external\":577762}"
> "ERROR\tentity not found:&egrave; \r@#[line:1,col:76]"


if you look at <Price> you’ll find the value 

819.6721311475409 but that should be 8.19……

 

 

Userlevel 7
Badge +12

@Davide - Can you share a screenshot of the output of the Google Sheets step that grabs that data? I want to see how “Prezzo” and “Totale” fields output looks like.

Userlevel 1

 

Here it is. As you see Price is 1000 and Total 5000 (!!)

Userlevel 7
Badge +12

@Davide - What I actually meant was a screenshot of the output of the Google Sheets step, but what you posted was from your app. :)

Userlevel 7
Badge +12

@ikbelkirasan Does that screenshot give you what you were looking for to help with this one? Thanks!

Userlevel 1

here below the output from Google Sheets

 

 

Userlevel 7
Badge +12

It looks like our Partner team reached out on this one and said:

 

I created a separate developer app and tested your numberFormat function in isolation. It worked as expected.

This leads me to believe the issue is not with the numberFormat function, but somewhere else.

Is it possible the numberFormat function is being called correctly, but then the result that gets returned from numberFormat is being changed through another function or operation?