CAL method does not returns a value

Hi all…

I’m having issue with a method I have written. The method used to calculate a sum value and return a decimal.

The method works fine when I add the method to the source expression of a text box.

but when I call it from code it returns 0.

Why is this?

The method is like this.

CalculateValues(GLParam : Record “G/L Entry”) Total : Decimal


Total += GLParam.Amount;


Can anyone help me…


Where did you call above function?

Is Total a global variable?

Well, it looks like you have 2 moving parts here - the function argument and the function return value. In order to isolate the cause of the inconsistent results you’re getting, you’ll want to analyze both parts in both of the environments that the function is being called from.

You mentioned that you got the results you expected when you made the function call from the Source Expression property of a textbox control, and that you got unexpected results when you made the call to the function from elsewhere in code.

The first step you might take would be to review the condition of the variable that you’re passing to the function in each case (from the SourceExp property, or from the code location). The function expects you to pass a G/L Entry record (t_17) as the only argument to the function. What variable are you passing to the function call in each case? By that I mean are you passing a global instance of t_17 or a local instance; is it defined as Temporary or not; and, is the current record in the copy of t_17 that you’re passing to the function the one you expect it to be, or is it possibly a blank record?

In the function call that you’re making from the Source Expression property, I’m gong to go out on a limb and guess that you’re using ‘rec’ as the t_17 argument, so that your SourceExp value looks something like: CalculateValues(rec). This would be as you’d expect it to be if the form or page that textbox control is on has a SourceTable property of G/L Entry. The main point of interest in this condition is that the form/page object manages the record pointer for you, so that whenever the SourceExp of the textbox control is evaluated, the “current” record being displayed will always be the one that’s getting passed to your custom function.

Regarding the function call that you’re making from some other place in code, the key point is that you may need to be managing the record pointer yourself, with either a GET, a Filter/Find or some other method for getting the right values into the record before you pass it to the function. So, in this case, take a good look at the definitions of the variables that you’re passing to the CalculateValues function to ensure that the record you’re expecting to send is the one you actually are sending. And, the other aspect of making this function call in open code is that you’re also responsible for handling the function’s return value. Making the function call from the textbox source expression, NAV handles the function return value for you by displaying it in the textbox. If you make the function call in open code, you have to tell NAV what to do with the function’s return value.

And all of that brings us to the second moving part in your function definition - the return value. As Mohana suggested, there may be some scope and persistence issues associated with that variable declaration. Once you’ve checked everything related to the table variable that you’re passing to the function, and you’re still having problems, then maybe you can attach some of the pertinent code from your problem object in your next post? That would go a long way toward helping us to narrow down the cause of the problem.