Calculated field and functions?

Hi,
I’m just looking into using calculated fields to concatenate various fields to be used in my reports. However I’m not sure that my concatenations can include the use of functions as I keep getting errors about variable being undefined. Could someone please help with the following:-

function authortitle()
{
	return AddPeriod(fullauthor)+AddPeriod(mtitle);
}

/**
 * @param {String} cstring This is the value to check if a period should be added.
 * @return {String}
 * 
 * @properties={typeid:36,uuid:"362C1112-1FE4-4EA1-9C82-63AA6B3AAA4A"}
 */
function AddPeriod(cstring) {
	if (cstring == null) return "";

	cstring = utils.stringTrim(cstring);
	if (cstring == "") return "";
	
	if (utils.stringRight(cstring, 1) != ".")  cstring += ".";
	cstring += "   "
	return cstring;
}

For some reason in the AddPeriod function the line

cstring = utils.stringTrim(cstring);

has the error

Reference to undeclared variable or property cstring

Am I doing something that is not allowed??

I suppose that full author and title are vars with string value

/**
 * @properties={typeid:24,uuid:"CFAA318C-368B-44DE-BFE1-7EC490BDFC38"}
 */
function authortitle() {
	var autor = addPeriod("fullauthor")
	var title = addPeriod("mtitle");

	return autor + " " + title
}

/**
 * @param {String} cstring
 *
 * @properties={typeid:24,uuid:"639FD8D9-3C6E-4B3D-A495-C94B323551D0"}
 */
function AddPeriod(cstring) {
   if (cstring == null) return "";

   cstring = utils.stringTrim(cstring);
   if (cstring == "") return "";
   
   if (utils.stringRight(cstring, 1) != ".")  cstring += ".";
   cstring += "   "
   return cstring;
}

You can try this, I hope helps!

Thanks for the reply pentamsi.
In fact fullauthor and mtitle are table fields.
The problem appears to be that the new function AddPeriod is not allowed, it gives the error

AddPeriod is not a function, it is null.

Within a tables calculated field is there a way to call other functions?

I don’t know if this is the reason, but you must try to name the function with first character in lowercase, like “AddPeriod” → “addPeriod”

You can call all the other functions in a calculation field, but you can’t use faoundsets and similars.

What do you want exactly? if you only want create a calculation to join two text fields you can do this:

/**
* @properties={typeid:24,uuid:"CFAA318C-368B-44DE-BFE1-7EC490BDFC38"}
*/
function authortitle() {
	
	// Vars of the DB
	var autor = fullauthor
	var title = mtitle
	
	// Var for return
	var result = null
	
	if(autor == null && autor == ""){
		result = title
	}else if(title == null && title == ""){
		result = autor
	}else {
		result = autor + " " + title
	}

	return result
}

And set the name of this function on your dataprovider

The problem appears to be with defining and calling a function which passes a variable when written within a calculated function. Even when I change the first letter to lowercase I get the error:

addPeriod is not a function, it is null.

when I run the report that uses the calculated field!?!

Hi Caroline.

Is AddPeriod function defined in a calculation js file? That might be the problem, why don´t you try to create AddPeriod as a global method and call it from the calculation?

Hi Caroline,

You should be able to use the function inside the calculation (like you can in methods) like so:

function authortitle()
{
    return AddPeriod(fullauthor)+AddPeriod(mtitle);
    
    function AddPeriod(cstring) {
       if (cstring == null) return "";

       cstring = utils.stringTrim(cstring);
       if (cstring == "") return "";
       
       if (utils.stringRight(cstring, 1) != ".")  cstring += ".";
       cstring += "   "
       return cstring;
    }
}

Of course this means that the function is only accessible for this specific calculation method. If you want to use it for other (calculation) methods you should make it a global method.

Hope this helps.

Many thanks Juan that did the trick - though the interface implies that you can’t use a global function if you just add it it works :D