Blog |

Parameterizing Page Scripting Tool Scripts in Microsoft Dynamics Business Central

Thursday, July 11, 2024
Reading time: 7 minutes

In the world of Microsoft Dynamics Business Central, efficiency and flexibility are key. One way to achieve this is by parameterizing your page scripts. This approach allows you to modify and reuse scripts easily without the need to change multiple instances of a specific value within the script. This BLOG is based on the preview version of the page scripting tool, version 24, so this BLOG post may very well not remain relevant in future versions, but for now, it works. So today, we’ll explore how to parameterize your scripts using a practical example.

What Does It Mean to Parameterize a Script?

Parameterizing a script involves defining variables that can be used throughout your script instead of hardcoding specific values. This makes the script more flexible and easier to maintain. For instance, if you have a script that filters a customer list by a specific customer number, parameterizing the customer number allows you to change the customer number in one place rather than updating it throughout the script.

Example: Parameterizing a Customer Filter Script

Let’s take a script that opens the list of customers, filters to customer 10000, opens the customer card, and validates that the correct customer has opened. We’ll then transform it into a parameterized script where the customer number is a parameter.

Original Script

Here’s the original script:
name: Cust10000
description: Test recording
start:
  profile: BUSINESS MANAGER EVALUATION
steps:
  - type: navigate
    target:
      - page: Business Manager Role Center
      - action: Customers
    description: Navigate to <caption>Customers</caption>
  - type: page-shown
    source:
      page: Customer List
    modal: false
    runtimeId: b4r3
    description: Page <caption>Customers</caption> was shown.
  - type: filter
    target:
      - page: Customer List
        runtimeRef: b4r3
    operation: add
    column:
      field: No.
      scope: filter
    description: Add filter for <caption>No.</caption> on <caption>Customers</caption>
  - type: input
    target:
      - page: Customer List
        runtimeRef: b4r3
      - scope: filter
        field: No.
    value: "10000"
    description: Input <value>10000</value> into <caption>No.</caption>
  - type: invoke
    target:
      - page: Customer List
        runtimeRef: b4r3
      - repeater: Control1
    invokeType: Edit
    description: Invoke row on <caption>Control1</caption>
  - type: page-shown
    source:
      page: Customer Card
    modal: false
    runtimeId: b5d1
    description: Page <caption>Customer Card</caption> was shown.
  - type: validate
    target:
      - page: Customer Card
        runtimeRef: b5d1
      - field: No.
    operation: =
    value: "10000"
    description: Validate <caption>No.</caption> <operation>is</operation>
      <value>10000</value>

Parameterized Script

Now, let’s parameterize the customer number in this script:

name: Cust10000
description: Test recording
parameters:
  CustomerNo:
    type: string
    default: "20000"
start:
  profile: BUSINESS MANAGER EVALUATION
steps:
  - type: navigate
    target:
      - page: Business Manager Role Center
      - action: Customers
    description: Navigate to <caption>Customers</caption>
  - type: page-shown
    source:
      page: Customer List
    modal: false
    runtimeId: b4r3
    description: Page <caption>Customers</caption> was shown.
  - type: filter
    target:
      - page: Customer List
        runtimeRef: b4r3
    operation: add
    column:
      field: No.
      scope: filter
    description: Add filter for <caption>No.</caption> on <caption>Customers</caption>
  - type: input
    target:
      - page: Customer List
        runtimeRef: b4r3
      - scope: filter
        field: No.
    value: =Parameters.'CustomerNo'
    description: Input <value>=Parameters.'CustomerNo'</value> into <caption>No.</caption>
  - type: invoke
    target:
      - page: Customer List
        runtimeRef: b4r3
      - repeater: Control1
    invokeType: Edit
    description: Invoke row on <caption>Control1</caption>
  - type: page-shown
    source:
      page: Customer Card
    modal: false
    runtimeId: b5d1
    description: Page <caption>Customer Card</caption> was shown.
  - type: validate
    target:
      - page: Customer Card
        runtimeRef: b5d1
      - field: No.
    operation: =
    value: =Parameters.'CustomerNo'
    description: Validate <caption>No.</caption> <operation>is</operation>
      <value>=Parameters.'CustomerNo'</value>

Step-by-Step Guide to Parameterize Your Script

  1. Record the Script: Use Business Central’s new Page Scripting Tool to capture or record the actions you want to automate.
  2. Export the Script: Once recorded, export the script to a YAML file.
  3. Add Parameters: Define your parameters at the top of the YAML file under the parameters section. In our example, we defined CustomerNo with a default value of “20000”.
  4. Replace Hardcoded Values: Replace the hardcoded values in the script with the parameter reference. For instance, replace value: “10000” with value: =Parameters.’CustomerNo’.
  5. Import the Script: Import the parameterized script back into Business Central.
  6. Run the Script: Now, when you run the script, you can specify different customer numbers without modifying the script itself.

Benefits of Parameterizing Your Scripts

Conclusion and Disclaimer

As mentioned in the beginning, this is based on version 24 of Business Central, and the Page Scripting Tool is still in Preview, which means that features may very well change in future, or Microsoft may make it easier for us to achieve the same result. For now this is how the page scripting tool is working. Give your feedback if you would like to see features in this tool and help shape the future of this tool. Be careful not to invest too heavily into developing scripts for this tool until the tool is in it’s final release, happy scripting.

Parameterizing your scripts in Microsoft Dynamics Business Central is a powerful technique to enhance your automation workflows. By following the steps outlined above, you can create flexible, maintainable scripts that save you time and effort. Start parameterizing your scripts today and experience the benefits of a more streamlined process!

Berny During

More about Business Central

dtnext

"Empowered Business Central is a welcome and most wanted piece of software to complement the Business Central functionality and it ...

Learn more