{"id":1562,"date":"2022-02-18T11:25:49","date_gmt":"2022-02-18T11:25:49","guid":{"rendered":"https:\/\/info.documotor.com\/?page_id=1562"},"modified":"2023-03-07T09:33:01","modified_gmt":"2023-03-07T09:33:01","slug":"custom-functions","status":"publish","type":"page","link":"https:\/\/info.documotor.com\/?page_id=1562","title":{"rendered":"Custom Functions"},"content":{"rendered":"\n<p>In DocuMotor it is possible to define custom functions. These custom functions apply the transformations defined. This reference contains a description of how a custom function is configured.<\/p>\n\n\n\n<p>It is possible to use custom functions in other transformations e.g. in Templates. When calling a custom function it is required to use <code>#<\/code> in front of the function name. For example if a function is named: <code>fullName<\/code> it should be refered in the transformations as <code>#fullName()<\/code><\/p>\n\n\n\n<h2>Parameters<\/h2>\n\n\n\n<p>To be able to pass on parameters to the custom functions it is needed to define the parameters in the <strong>parameters<\/strong> block of the function page. Each parameter needs a name that should be used when writing the function&#8217;s transformation in the <strong>body<\/strong> text editor. <\/p>\n\n\n\n<div class=\"wp-block-columns\">\n<div class=\"wp-block-column\" style=\"flex-basis:100%\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"905\" height=\"202\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/08\/UserFunction_Parameters.png\" alt=\"\" class=\"wp-image-2980\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/08\/UserFunction_Parameters.png 905w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/08\/UserFunction_Parameters-300x67.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/08\/UserFunction_Parameters-768x171.png 768w\" sizes=\"(max-width: 905px) 100vw, 905px\" \/><figcaption>Here two parameters named <em>firstName<\/em> and <em>lastName<\/em> are defined for the custom function.<\/figcaption><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>When referring to these parameters in the <strong>body<\/strong> text editor it is required to use <code>#<\/code> in front of the parameter names. So when referring to the parameters defined in above image it should look like this <code>#firstName<\/code> and <code>#lastName<\/code>.<\/p>\n\n\n\n<h3>By Value vs By Expression<\/h3>\n\n\n\n<p>A parameter can either be passed as <strong>by value<\/strong> or <strong>by expression<\/strong>. When a parameter is passed by value, it is evaluated when the function is called and the result is passed to the function. When a parameter is passed by expression it is not immediately evaluated on function call and instead evaluated in the scope where it is read in the function body. <\/p>\n\n\n\n<h2>Body<\/h2>\n\n\n\n<p>The <strong>body<\/strong> text editor is where the JMES transformation, that the custom function applies, is configured. This transformation should refer to the parameters defined in the <strong>parameters<\/strong> block. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"358\" height=\"66\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-body1-1.png\" alt=\"\" class=\"wp-image-1627\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-body1-1.png 358w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-body1-1-300x55.png 300w\" sizes=\"(max-width: 358px) 100vw, 358px\" \/><figcaption>Here the body text editor has defined a transformation using the join-function that joins the two parameters #firstName and #lastName.<\/figcaption><\/figure>\n\n\n\n<p>As seen in above image, the parameters are referred to by using <code># <\/code>in front of the parameter names. <\/p>\n\n\n\n<h2>Test Transformation<\/h2>\n\n\n\n<p>The <strong>Test Transformation<\/strong> text editor is used to test how to call the custom function. It is both possible to use hardcoded parameters or parameters from the <strong>Test Sample Data<\/strong> text editor. Below is two examples of how to test call the function with both hardcoded parameters and parameters from the <strong>Test Sample Data<\/strong>. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"372\" height=\"84\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation2.png\" alt=\"\" class=\"wp-image-1643\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation2.png 372w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation2-300x68.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><figcaption> Example of how to call the custom function with hardcoded parameters.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"353\" height=\"85\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation1.png\" alt=\"\" class=\"wp-image-1645\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation1.png 353w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testtransformation1-300x72.png 300w\" sizes=\"(max-width: 353px) 100vw, 353px\" \/><figcaption>Example of how to call the custom function with parameters from <strong>Test Sample Data<\/strong>. <\/figcaption><\/figure>\n\n\n\n<p>Note: When calling a custom function (e.g. in Templates) and referring to previously defined variables with the <code>$ operator<\/code>, it is important to keep in mind that this operator can end up referring to different variables inside the custom function&#8217;s own transformation. Therefore it is strongly recommended using a pipe expression. Relevant example can be seen in below image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1004\" height=\"155\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-usagewithcurrent-1.png\" alt=\"\" class=\"wp-image-1658\" srcset=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-usagewithcurrent-1.png 1004w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-usagewithcurrent-1-300x46.png 300w, https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-usagewithcurrent-1-768x119.png 768w\" sizes=\"(max-width: 1004px) 100vw, 1004px\" \/><figcaption>Here is an example of usage in a template where the #fullName() function is used in collaboration with a pipe expression. <\/figcaption><\/figure>\n\n\n\n<h2>Test Sample Data<\/h2>\n\n\n\n<p>The <strong>Test Sample Data<\/strong> text editor should contain an example of some sample data to test how to use data variables when calling the custom function. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"224\" height=\"108\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/image.png\" alt=\"\" class=\"wp-image-1691\"\/><figcaption>Here is an example of the test sample data. This is also the variables used for the second example in Test Transformation showing how to call the function using variables.<\/figcaption><\/figure>\n\n\n\n<h2>Test Result<\/h2>\n\n\n\n<p>The <strong>Test Result<\/strong> shows the output from the <strong>Test Transformation<\/strong>. The result of all the above examples would look like this: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"266\" height=\"89\" src=\"https:\/\/info.documotor.com\/wp-content\/uploads\/2022\/02\/functions-testresult.png\" alt=\"\" class=\"wp-image-1696\"\/><figcaption>The Test Result output from all above example.<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In DocuMotor it is possible to define custom functions. These custom functions apply the transformations defined. This reference contains a description of how a custom function is configured. It is possible to use custom functions in other transformations e.g. in Templates. When calling a custom function it is required to use # in front of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3160,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/1562"}],"collection":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1562"}],"version-history":[{"count":48,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/1562\/revisions"}],"predecessor-version":[{"id":2990,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/1562\/revisions\/2990"}],"up":[{"embeddable":true,"href":"https:\/\/info.documotor.com\/index.php?rest_route=\/wp\/v2\/pages\/3160"}],"wp:attachment":[{"href":"https:\/\/info.documotor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}