Replace() function not working in long makro? Why?

Hello Everyone!

In a short makro, the replace () function command works perfectly fine to replace the comma of a number with dots:

{
“Name”: “test”,
“CreationDate”: “2021-9-22”,
“Commands”: [
{
“Command”: “executeScript_Sandbox”,
“Target”: “var s = “1,5”; return s;”,
“Value”: “profit”,
“Description”: “”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “var mystring = ${profit}; var replaced = mystring.replace(/\,/g,’.’); return replaced”,
“Value”: “fprofit”,
“Description”: “”
}
]
}

It changes the variable profit from 1,5 to 1.5. It is tested and is working. I want to use this for a more complex makro to replace the comma in a currency text to a dot.

When I want to use this function in a long makro, to have a real use case, it gives me the following error:

  • [error]

[Line 183]: Error in executeScript_Sandbox code: mystring.replace is not a function

Here is the part of the code that is relavant:

{
  "Command": "executeScript_Sandbox",
  "Target": "var mystring = ${profit}; var replaced = mystring.replace(/\\,/g,'.'); replaced=Number(replaced);return replaced;",
  "Value": "fprofit",
  "Description": ""
},

Does Anyone know what I did wrong to have the same command work in the short makro, but tell me that mystring.replace is not a function in the longer, relavant makro? What can I do to fix it?

Thank you!

Compare 2 lines

“Target”: “var mystring = ${profit}; var replaced = mystring.replace(/\,/g,’.’); return replaced”,
“Target”: “var mystring = ${profit}; var replaced = mystring.replace(/\\,/g,‘.’); replaced=Number(replaced);return replaced;”,

you have use extra \ in second line in replace function

1 Like

Thank you for your help! I found the issue, which was not an extra \ (This was just a copy and paste mistake)

The actual issue was that the variable ${profit} was defined as a number and it seems like these types of function only work with strings.

I added the line
String(mystring);
which fixed the problem!

ok

you can also shorten code
replaced=Number(replaced);return replaced;”,

return Number(replaced);