Advanced User Defined Function

Embed Size (px)

DESCRIPTION

SAP PI Training

Citation preview

Advanced User-Defined FunctionsUseAdvanced user-defined functions can access more than just individual field values. Instead, you can import a complete context or an entire queue for a field as an array before your function is called. This enables you, for example, to perform calculations on all field values of a context as well as to divide up the contexts themselves further by inserting context changes.IntegrationWhen the instance for a source structure is parsed, a message mapping works with queues.

There is a queue for each hierarchy level. A queue can have the following entries:Possible Queue EntriesValueMeaning

(empty string)This is a queue for a structure field. In the queue there is an entry with an empty string each time the field appears in the XML instance. In the example above there is such a queue forand.

(string)Value of a value field

ResultList.CCConstant that shows a context change

ResultList.SUPPRESSConstant that causes a field and its subnode to be ignored during processing

(empty context)Context that does not contain any values (see the graphic below).

You can create special structure mappings by adding or removing context changes. The standard functionsSplitByValueandremoveContexts()work by this principle.See also:Structure Mappings by Setting the Context.The following basic rule applies for nested structures: The number of contexts of a queue must be the same as the number of values (empty strings) of the upper-level queue.

FeaturesAdvanced user-defined functions can import either just one context into the input arrays, or complete queues. Make your selection by selecting or deselecting theCache Entire Queuecheckbox in the function editor.Working with Contexts or QueuesInformation in CacheImplications

ContextAdvanced functions that only import one context do not have an identifiable context change. You can of course insert a context change into the results list.

QueueSince one or more entire queues are imported in this case, this option is more memory-intensive and is not suitable for very large messages.

The input arrays do not contain the context change at the start and end of the context (or the queue). These context changes are implicitly always available and cannot be identified or deleted from the user-defined function.Activities1.Create an advanced user-defined function (seeUser-Defined Functions).2.Process the values from the input arrays in the Java source text and create a results list by using theResultListobject.

ResultList ObjectUseThis object is used in advanced user-defined functions to return the result of the function. If you save the entire queue before the call in the user-defined function, use this object to either return a queue or to return the context of a queue.FeaturesMethods of theContainerObjectMethodUse

void addValue(String value);Appends a value to the results list

void addContextChange();Appends a context change to the list. This can also be achieved if you append the constantResultList.CCby usingaddValue().

void addSuppress();Appends the constantResultList.SUPPRESSto the list. The generation of the target field and its sub nodes is suppressed for such entries.

void clear();Deletes all previously appended values from the list.

You can use the ResultList.CC constant to find context changes in the input array that the function was transferred to, for example:a[index].equals(ResultList.CC)The following context changes are always available implicitly and are not part of the input array.If you only import one context into an input array before you call advanced functions, then the array only contains values and no context change.If you import an entire queue into an input array before you call the advanced functions, then there is no context change at the start and the end of the array.ExampleThis example shows the use of ResultList.SUPPRESS, in that the standard functioncreateIf()is programmed as an advanced user-defined function. This standard function also works internally with the SUPPRESS constant:If you specifytrue, an empty string is created for the field to be generatedIf you specifyfalse, createIf() creates the constant ResultList.SUPPRESSIf you implement this function as an advanced user-defined function, you just need to import a constant frommyCreateIf()prior to the call. The function has the following source text:public void myCreateIf(String[] a,ResultList result,Container container){for ( int i = 0; i < a.length; i++ ){if( a[i].equals("true"))result.addValue( );elseresult.addSuppress();}}