Creating Custom Macros To Format Values in Word Printables for Creatio (formerly bpm’online)

In Word printables for Creatio, it’s possible to create all sorts of custom macros to query and add related data, bring in values such as the current user, or just to format values. This article will look at creating a custom macro to format currency values in a Word printable, however the concepts are the same for any type of macro you’d like to create for Creatio.

The way a custom Macro for Creatio works is, you create a Source Code schema with a class that implements the IExpressionConverter interface. You define an attribute, which will be your macro’s name. Then, you can add that macro name to any merge field and when the printable is run, the value for the merge field will be passed to your code, you can do whatever you want with it, such as, simply format it, or use it to run a query and look up some other data to return, etc.

First, let’s create the macro. Again, our example will create a macro that just receives a currency value, formats it as money, then returns the formatted value to display in the Word doc. Add a new Source Code schema, give it a name of “UsrMoneyConverter” and add the following code:

namespace Terrasoft.Configuration
{
    using System;
    using Terrasoft.Common;
    using Terrasoft.Core;
    using Terrasoft.Core.DB;
    using Terrasoft.Core.Entities;
    using Terrasoft.Core.Packages;
    using Terrasoft.Core.Factories;
    using System.Globalization;
     
    [ExpressionConverterAttribute("Money")]
    public class UsrMoneyConverter : IExpressionConverter
    {
        public string Evaluate(object value, string arguments = "") 
        {
            var result = string.Empty;
            if (value != null)
            {
                result = value.ToString();
                double currency;
                if (Double.TryParse(result, out currency))
                {
                    result = currency.ToString("C", new CultureInfo("en-US"));
                }
            }
            return result;
        }
    }
}

Notice the ExpressionConverterAttribute attribute on the class, it is defining the macro name as “Money” – this is the name of our macro and what we’ll refer to it as to use it. The code simply receives a value, converts it to a double to ensure it’s a currency value, then returns the formatted value.

To use it, we’ll need to add it to a merge field in a Word printable. In the report setup of a Word printable, add any currency field. Then click the pencil icon to edit it:

Then add [#Money#] at the end of the title. Basically, leave the title as it is, just paste that at the end. We’re using “Money” since that is what our macro is called. You’d obviously use [#MacroName#] where MacroName is the name of your macro, meaning the value specified in the ExpressionConverterAttribute attribute in your code. It will look like this:

Now, you can drag that into the Word document and when that merge field is processed, it will pass the value to your code for the custom Money macro and the returned value will be what displays there in the Word doc.

While this sample was to simply format a value, we could use this same concept for other purposes as well. For example, you could add an Id for a record as a merge field, then when the Id is passed into your macro, you run a query to retrieve some other data and return that instead.

You can read more about creating custom macros in the Academy

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

4 Comments

  1. Hi Ryan

    I have tried to replicate the same which you have mentioned above but my report is blank when I run it.

    Regards
    Sivanesan

    Reply
    • That would likely be caused by either some problems with the report itself, or how the data is setup in the printable setup, or an error in your macro.

  2. Dear Ryan,

    Is there a possibility to show a table based on a field value? I guest it might use features of Word to make it happen.

    Would you please give some insights about it?

    Reply
    • I don’t know of any way to return the proper Word or RTF codes for a table from a macro. I don’t believe it’s possible.

      What I typically do is create a database view that contains the logic for the table. Then, create an object for that view and expose that in the printable. This is a common way I include tables in printables that contain more complex logic or custom data of some kind.

      Ryan

Submit a Comment

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

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!