Expand my Community achievements bar.

SOLVED

Defining a JavaScript function and calling it from a field event

Avatar

Former Community Member

Hello!

I'd like to create a function that will be invoked when a numeric field is calculted.

This function will write a number as text, similar to what WordNum does, but in Portuguese.

Please, I'd like to know:

- Where do I define the function and LC Designer syntax?

- How do I invoke it from the field event?

Thank you for any hints!

Marcos

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Marcos,

I saw your earlier thread, so I think I understand what you are looking for.

If you right-click on the root node you can select "Insert a script object" from the menu.

This will create the script object in the hierarchy, which you can then name. Say, "translate".

Script objects will only accept Javascript, so your solution to translate would need to be in this language. If you select the script object and go to the script editor, you can declare the function, name it, and enclose it is curley brackets:

function Engligh2Portuguese()

{

     // your function script

} // close function

You can call the function from an event in an object. Let us say you have a textField, which you want to contain the value of an amount field ("amount"), but in words.

In the calculate event of the textField you would call the function and pass the value of the numerfield:

translate.English2Portuguese(amount.rawValue); 

Then  the function in the script object would need a variable to contain the passed value. So in the script object the function would look like this:

function English2Portuguese(vAmount)

{

     // your function script

}

I am not sure what your actual function is going to look like. I hope that you have a solution for the translation part.

Good luck,

Niall

View solution in original post

7 Replies

Avatar

Level 10

First you need to insert a Script Object: right-click on the top most element in the form Hierarchy (default is form1 in English) and select Insert Script Object.

You'll then get an entry in the Hierarchy called Variables and in there will be "(untitled Script Object)" - rename that to whatever you want, I'll use "utils" for the example.

In that Script Object you can define your functions, you can have as many different functions as you want.

function numberToWords(oField, nValue)

//inside the brackets are variables to hold the field object and value you are passing

{

//insert your code here

//use oField.rawValue to return the new value

}

On the Calculate event of the field you would then pass the field object and value to the function:

utils.numberToWords(fieldName, fieldName.rawValue);

I think I've got all the syntax correct!

Avatar

Correct answer by
Level 10

Hi Marcos,

I saw your earlier thread, so I think I understand what you are looking for.

If you right-click on the root node you can select "Insert a script object" from the menu.

This will create the script object in the hierarchy, which you can then name. Say, "translate".

Script objects will only accept Javascript, so your solution to translate would need to be in this language. If you select the script object and go to the script editor, you can declare the function, name it, and enclose it is curley brackets:

function Engligh2Portuguese()

{

     // your function script

} // close function

You can call the function from an event in an object. Let us say you have a textField, which you want to contain the value of an amount field ("amount"), but in words.

In the calculate event of the textField you would call the function and pass the value of the numerfield:

translate.English2Portuguese(amount.rawValue); 

Then  the function in the script object would need a variable to contain the passed value. So in the script object the function would look like this:

function English2Portuguese(vAmount)

{

     // your function script

}

I am not sure what your actual function is going to look like. I hope that you have a solution for the translation part.

Good luck,

Niall

Avatar

Former Community Member

Hi there both of you, thank you for the input!

Well, you've posted solutions two different syntax. So I'm confused.  

So, let me see if I get it, using my example here...

On my form I have two fields, one is a numeric field called total, the other is a text field called extenso.

So, on the extenso field calculate event, I have:

form1.#subform[0].extenso::calculate - (JavaScript, client)
utils.porExtenso(total.rawValue);

The function is set as this:

form1.#variables[0].utils - (JavaScript, client)
function porExtenso(c)
{

      c = "testing";
      return c.rawValue;
}

Of course, I used this function just to learn how to deal with this. I've tried to return "testing" to the text field...

But it is not working... still trying to get some info from the LC Designer help...

Now, maybe a link is missing,... how do I get the value returned by the function inside the extenso field?

Thank you again for your help!

Marcos

Avatar

Level 10

I don't know if return works with script objects, hopefully somebody who knows more about it than me can speak to that!

In my example I passed two values to the function, the field that gets the result and the value to be worked on.