Search code examples
anypoint-studiodataweavecloudhubmulesoft

Global function works in preview but fails when code is run


I have a global function named 'finalPrice' which is defined in my configuration.xml file. The function takes in a value - does stuff to it - and returns the final value. I reference the function from within DataWeave. When I click 'preview' I can see the correct output in the preview window. However when I run it I get the error:


Message : Exception while executing:

There is no variable named 'finalPrice'.


I have run the code on my local machine and in CloudHub and I get the same result

XML Code:

<configuration doc:name="Configuration">
        <expression-language>
            <global-functions>
                <!-- This function is called by the 'Validate and Transform' dataweave component in the 'main' flow-->
                def finalPrice(incoming_value) 
                {
                    import java.lang.String;
                    import java.math.RoundingMode;

                    // Do Stuff

                    return strFinalNumber;
                }
            </global-functions>
        </expression-language>
</configuration>

DataWeave Code:

//Refer to "finalPrice" Global Function in the main.xml configuration file
DB_FINL_PRCE: "field_missing" when payload01.DB_FINL_PRCE == "" otherwise finalPrice(payload01.DB_FINL_PRCE)

Any help appreciated


Solution

  • It's an issue with the comments in the global-functions. So remove or modify the line:

     <!-- This function is called by the 'Validate and Transform' dataweave component in the 'main' flow-->
    

    and just have:

    <configuration doc:name="Configuration">
                <expression-language>
                    <global-functions>
                        def finalPrice(incoming_value) 
                        {
                            import java.lang.String;
                            import java.math.RoundingMode;
    
                            // Do Stuff
    
                            return strFinalNumber;
                        }
                    </global-functions>
                </expression-language>
        </configuration>
    

    Or modify your comments to //

    <configuration doc:name="Configuration">
            <expression-language autoResolveVariables="true">
                <global-functions>
                    //This function is called by the 'Validate and Transform' dataweave component in the 'main' flow
                    def finalPrice(incoming_value) 
                    {
    
                        // Do Stuff
    
                        return "somethingelse";
                    }
                </global-functions>
            </expression-language>
    </configuration>