Replacing symbols in the script rule in Vantage does not work properly for field type Number

Symptoms

Replacing symbols in the script rule in Vantage does not work properly.

In skill, there is a column of Number data type:


There is an advanced script rule that replaces some trash symbols like slashes, spaces, etc. with an empty substring:

var field = Context.GetField("New Table/Withdrawal");
if (field != null)
{
var x = field.Text
if (x.length < 1)
  {
   field.Value = 0.00
  }
else
  {
    x = x.replace(/,/g,'')
    .replace(/\s/g,'')
    .replace(/\//g,'')
   .replace(/\^/g,'');   

       field.Value = Number(x);
  }
}

However, it does do anything. Symbols are not getting replaced.

Cause

This is caused by the way optimization of automatic data type casting works.

The Number is a primitive data type in JavaScript.

Vantage is able to automatically parse text values and convert them into numbers in fields. If to hover the instance of the field with the mouse pointer there will be a pop-up with a numeric value:



That means, despite the text representation containing a slash character, Vantage was able to cast it to the number and understand that the numeric representation is 30. The same happens to field instances containing commas and spaces:



When using:

field.Value = Number(x);

it sees that on the left was a primitive value 30 (as a result of auto-casting) and on the right is Number("30") -> giving the same 30. As the Number is a primitive data type, 30 on the left is the same for JavaScript as 30 on the right. For the sake of optimization, it just doesn't do anything i.e. doesn't do an assignment of a variable to the same primitive value. If to change the line to:

field.Value = Number(x) + 1;

the value on the left won't be equal to the value on the right and the assignment will take place.

Resolution

To solve the issue, something like this can be written:

field.Value = null;
field.Value = Number(x);

Have more questions? Submit a request

Comments

2 comments

  • Avatar

    Felix Findeisen

    Hey everyone,

    I have quite a similar problem that is not solved by this post. 

    I am using the default Purchase Order Model. I want to extract the Quantity of an item position. The quanitity is in the format 1.000 instead of 1000. I need to replace the "." with nothing and this is NOT done automatically by Abbyy. My Scripting also does not work and throws an error without any information.

    It looks like this: 

    Context.GetField("Line Items/Quantity").Value = Number(Context.GetField("Line Items/Quantity").Value.toString().replace(".", ""));
    Hardcoding works here:
    Context.GetField("Line Items/Quantity").Value = Number("10");
     
    What is the best way to proceed here? Most of the purchase Orders do not have a "." included in the Quantity.
    0
  • Avatar

    Jakub Porowski

    Hi,
    I have a similar problem.
    I have written a custom script to normalise a number in a LineItem quantity field. I always want 4 digits in the Quantity field. My script is working correctly. If I have a value of 1.12345 it gets 1.1234. The problem is with the zeros, if my value is 1.12001 it gets 1.12 in the result without two zeros. Why are the zeros automatically removed and nowhere do I have the option to edit this? Is it possible in the basic invoice skill to change the data type in the line items?

    0

Please sign in to leave a comment.