Nintex Workflow – Math Inline Functions

The Inline Function functionality has really opened up Nintex Workflow design. You can use any of the out-of-the-box inline functions (look up the Nintex Workflow help – get it at http://connect.nintex.com). But the really exciting thing is that Microsoft provides other static methods that you can add to Nintex Workflow and do even more in the product. I talked about some of this in my article Nintex Workflow – Trigonometry (why?….why not?)

But what if you want something else that you can’t find available in any of the Microsoft namespaces/classes?

That’s when you start doing some development and create your own Inline function.

What is an inline function?

The simple explanation, is that it is a static method, in a class, in a namespace, in an assembly in the GAC.

So what you need to do, is start a project in Visual Studio. I chose a Class Library. Add a new class to it and then add some static methods to that class that you want to expose to Nintex Workflow designers. Make sure your project is signed, as it will need to go into the GAC (and therefore needs to be strongly named).

Then you add the inline function reference to Nintex Workflow, using NWAdmin tool that is installed with Nintex Workflow. It has an -o AddInlineFunction operation. You just need to provide it with the details of your Inline function.

What has prompted me to look into this?

Recently I found that people would like to do some basic calculations inside the Build Dynamic String action. But it currently isn’t built to accommodate this type of functionality out-of-the-box.

Example :
You have a text string “PingPong.txt”, and you want extract the “PingPong” part. Yes you could do this with a Regular Expression action, but we want to do it in a Build Dynamic String.

You have the fn-SubString inline function, but that takes a 0-based start index and the number of characters you want to get. But if the text you expect changes length, then you can’t hard code that in.

Here’s our issue :

fn-SubString(PingPong.txt, 0, 8) = PingPong
fn-SubString(Tennis.txt, 0, 8) = Tennis.t

What we can’t do, is something like this :

fn-SubString(PingPing.text, 0, fn-Length(PingPong.txt)-4)

You can’t do the (subtract 4) calculation, as the Build Dynamic String action does not support this.

The ugly workaround :

1. Have a Build Dynamic String action that uses the fn-Length inline function and stores the result in a text variable

2. Add a Convert Value action to convert the length into a Number variable

3. Add a Math Operation action that would subtract 4 from that Number variable

4. Add another Build Dynamic String action, that would have this fn-SubString(PingPong.txt, 0, {WorkflowVariable:numLength}) – or something like this

Seems like a lot to do, when a simple calculation would make things so much easier.



Solution

I created a class called VTMath and added 4 static methods to do the most simplest of math operations. Add, Subtract, Multiply and Divide.

public class VTMath
{
    public static double Add(double x, double y)
    {
        return x + y;
    }
    public static double Subtract(double x, double y)
    {
        return x - y;
    }
    public static double Multiply(double x, double y)
    {
        return x * y;
    }
    public static double Divide(double x, double y)
    {
        return x / y;
    }
}

Now that we have the code, I make sure my class library is signed, built into a SharePoint feature (just because) and we are ready to deply.



Deployment of our new Inline Functions

Here are the deployment command lines.

stsadm -o AddSolution -filename VTMathInlineFunctions.wsp
stsadm -o DeploySolution -name VTMathInlineFunctions.wsp -immediate -allowgacdeployment
stsadm -o execadmsvcjobs

Now that the solution is deployed, we need to tell Nintex Workflow of our new Inline Function.

Go to the installation folder of Nintex Workflow, so you can run the NWAdmin.exe tool. Usually located : c:\program files (x86)\Nintex\Nintex Workflow 2007

Run the following in the command line prompt:

NWAdmin.exe -o AddInlineFunction -functionalias fn-VTAdd -assembly "VTMathInlineFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=05b6de0f79daedbe" -namespace VTMathInlineFunctions -typename VTMath -method Add

NWAdmin.exe -o AddInlineFunction -functionalias fn-VTSubtract -assembly "VTMathInlineFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=05b6de0f79daedbe" -namespace VTMathInlineFunctions -typename VTMath -method Subtract

NWAdmin.exe -o AddInlineFunction -functionalias fn-VTMultiply -assembly "VTMathInlineFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=05b6de0f79daedbe" -namespace VTMathInlineFunctions -typename VTMath -method Multiply

NWAdmin.exe -o AddInlineFunction -functionalias fn-VTDivide -assembly "VTMathInlineFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=05b6de0f79daedbe" -namespace VTMathInlineFunctions -typename VTMath -method Divide

What this means, is that now instead of all those actions and variables, all you have is one Build Dynamic String action.

fn-SubString(PingPing.text, 0, fn-VTSubtract(fn-Length(PingPong.txt),4))

As an example of what you can do now in the Build Dynamic String action, would be to make a calculation like this :

(((10 + 20) / 5) * 3) - 15

The Build Dynamic String would look like this :

fn-VTSubtract(fn-VTMultiply(fn-VTDivide(fn-VTAdd(10,20),5),3),15)

It may not be the neatest, but it’ll cut down your workflow design by a lot if you had to do something like this.



Useful Downloads

Solution File – VTMathInlineFunctions.wsp

Leave a Reply

Your email address will not be published. Required fields are marked *