JavaScript Function

The default Business Function operation is the 'JavaScript Function' and displays as 'JavaScriptBusinessFunctionWithBinds' on the Function tab. This operation allows users to define their own functions using JavaScript while binding various STEP data for use.

To add the JavaScript to this operation, click the Edit Business Function link.

For more information on editing a business function, see the Editing a Business Rule topic in the Business Rules documentation here.

Configuration

In this section, you will find an example of how to configure a Business Function that displays the text of a referenced object's attribute value description.

  1. JavaScript Binds - This section allows users to bind various STEP objects to JavaScript.

Note: The dynamic binds available for business functions are 'STEP Manager' and 'Logger.'

For more information on STEP JavaScript Binds, see the JavaScript Binds topic (here) as well as the Adding a Bind topic (here) in the Resource Materials online help.

  1. Via the Messages section, it is possible to define localized error messages that can be returned when an error condition is encountered with this business function. For more information on translatable JavaScript messaging, see the Localized Messages for JavaScript Business Rules topic in the Execute JavaScript section here.
  1. The Input Parameter section is used for defining the parameters that can be passed to the function to produce an output. A number of STEP and Java types are available.

Note: Use caution when changing an input parameter type for an active JavaScript Function business rule. Business rules that call the modified function may also need updating.

Using the provided example, when a user calls this Business Function, they must provide an attribute, references, or a product, which are passed as objects.

  1. Via the Return Type section, it is possible to define what type of data the business function returns.

Note: Use caution when changing a return type for an active JavaScript Function business rule. Business rules that call the modified function may also need updating.
Also, it is important to note that JavaScript Business Functions configured to return either a 'Double' or an 'Integer' or lists of these types should be specified in the return command rather than relying on the JavaScript to Java type conversion. For example, to return an integer value of 3 use:

return new java.lang.Integer(3); 

instead of
return 3;

In the latter case, return values that are in fact integers may not be recognized as such and will produce errors.

  1. The JavaScript section is where the function logic must be entered. In this example, the values of attributes on referenced objects is returned. The full code used for this functionality is:
var referencesArray = product.getReferences(accReferenceType).toArray();
var desc = "";
for (var i = 0; i < referencesArray.length; i++){
	var accessoryDesc = referencesArray[i].getTarget().getValue(accDescAttribute.getID()).getSimpleValue();
	if (accessoryDesc != null){
		if (i != 0){
			desc += ", ";
		}
		desc += accessoryDesc;
	}
}
 
return "Compliant accessories: " + (desc.length == 0 ? "None" : desc);

See the Calling a Business Function from a JavaScript Business Rule topic for information on executing and testing business functions here.