Dittofi
  • Tutorials
    • Tutorial Index
    • Quick Start
    • Dittofi Essentials
      • Part 1: Frontend terms and concepts
      • Part 2: Backend terms and concepts
      • Part 3: Connecting frontend and backend
      • Part 4: Dittofi app structure
    • Dittofi App Design
      • 🏗️App architecture 101
      • ◼️Backend app development 101
      • 🟪Frontend app development 101
      • 🔰(optional) Web developer beginner series
  • Learn Frontend
    • 👨‍💻Getting Started
      • Welcome to Dittofi
      • Intro to the Dittofi Design Studio (Frontend)
    • 🟩Pages
    • 🟪Elements
      • Types
        • Button Element
        • Div Block Element
        • Form Block Element
        • Link Block Element
        • List Element
        • Modal Box Element
        • Tabs Element
        • Columns Element
        • Image Element
      • Partials
      • Prefabs
      • Custom
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟨Events
      • 🟨External
        • HTTP Request
        • Run Endpoint
      • 🟨Flow Control
        • Condition
        • Run Action
        • Loop
        • Sleep
      • 🟨Maths
        • Add
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟨Navigation
        • Open Page
      • 🟨Notifications
        • Alert
        • Confirm
      • 🟨Other
        • Copy To Clipboard
        • Run Custom
      • 🟨Text
        • Concat
        • Contains
        • Ends With
        • I Contains
        • I Ends With
        • I Starts With
        • LTrim
        • RTrim
        • Text To Upper
        • Text To Lower
        • Trim
        • Starts With
      • 🟨Variables
        • Set Field
        • Remove Field
      • Validate
    • 🟦CSS Layouts
      • Holy Grail Layout
      • Side Bar Layout
      • Sticky Footer Layout
      • Sticky Header Layout
      • CSS Frameworks
    • 🔲CSS Frameworks
    • Component Libraries
    • Meta Attributes
  • Learn Backend
    • 🖥️Getting Started
      • Intro to Data
      • Intro to APIs
      • Build your first custom API
      • Actions & Events
    • 🟫Back-End Actions
    • 🟩Custom Models
      • 🟩Attributes
        • Array
        • Checkbox
        • Custom Model
        • Collection (Custom Model)
        • Collection (System Custom Model)
        • Date
        • Datetime
        • File
        • Number
        • Number With Decimal
        • Json
        • Text
    • ◼️Data Models
      • Database Fundamentals
        • Database relationships
      • ◼️Columns
        • Attachment
        • Auto Increment
        • Checkbox
        • Date
        • Datetime
        • Duration
        • Email
        • Long text
        • Link to another record field
        • Number
        • Number with decimal
        • Password
        • Phone number
        • Percent
        • Price
        • Rating
        • Single select
        • Single line text
        • URL
    • 🟥Events
      • 🟦Arrays
        • Append
        • Array Pop
        • At
        • Length
        • Prepend
        • Range
      • 🟦Conversions
        • Base64 Encode
        • Base64 Decode
        • Json Decode
        • Json Encode
        • Path Escape
        • Path Unescape
        • Query Escape
        • Query String Parse
        • Query Unescape
        • To Bool
        • To Float
        • To Int
        • To Str
        • Yaml Encode
        • Yaml Decode
      • 🟦Database Requests
        • Create
        • Create Or Update
        • Delete
        • Has Record
        • Read Event
        • Update
      • 🟦External
        • HTTP Request
        • SFTP Download
        • SFTP Upload
      • 🟦Files
        • CSV Reader Event
        • CSV Writer Event
        • File Read
        • File Save
        • File Size
        • File Type
        • JSON Reader
        • PDF Generator
      • 🟦Flow Control
        • Condition
        • Loop
        • Return
        • Run Action
        • Sleep
      • 🟦Notifications
        • Send Mail
        • Connect Websocket
        • Message Websocket
      • 🟦Maths
        • Add
        • Acos
        • Acosh
        • Asin
        • Asinh
        • Atan
        • Atanh
        • Cbrt
        • Ceil
        • Divide
        • Modulus
        • Multiply
        • Subtract
      • 🟦Security
        • Compare Password
        • JWT Decode
        • JWT Encode
        • Login
        • Logout Web User
        • Password Generator
        • Random Number
      • 🟦Time
        • Add Time
        • Day
        • Format Time
        • Minute
        • Nanosecond
        • Parse Time
        • Second
        • Time In
        • Year
        • Year Day
      • 🟦Text
        • Concat
        • Contains
        • Ends With
        • Execute Template
        • I Contains
        • I Ends With
        • I Starts With
        • Starts With
        • LTrim
        • RTrim
        • Replace
        • Regex Find All String
        • Regex Find String
        • Regex Match String
        • Trim
        • Text To Lower
        • Text To Upper
        • Text To Title
        • Url Getarg
      • 🟦Other
        • Assign
        • Run Custom
        • Redirect Web Page Event
        • Render Web Template Event
    • 🟧Variables
      • Array
      • Checkbox
      • Collection
      • Collection (Custom Model)
      • Collection (System Custom Model)
      • Custom Model
      • Date
      • Datetime
      • File
      • Json
      • Number
      • Number with decimal
      • Model
      • System Custom Model
      • Text
      • UUID
    • 🟪Webservices
  • Templates
    • Two sided marketplace template
      • Introduction
        • Part I: Installing and setting up the marketplace template
        • Part II: Understanding what features are included in the marketplace template
        • Part III: Customizing your marketplace frontend without coding
        • Exploring Dittofi's marketplace API
      • Listings
        • How to create a marketplace listing
    • How to build a CRUD app
  • Third Party APIs
    • OAuth 2.0 APIs
      • Introduction To OAuth 2.0
      • Google OAuth 2.0 - PART I
      • Google OAuth 2.0 - PART II
  • Dittofi Admin
    • Your Account
      • Workspace Admin
      • Manage Billing
      • Connect a custom domain
      • How to use a discount code to Dittofi Pro
  • FAQ
    • API Generator
    • How to write custom SQL?
    • Why does my image not display?
    • How can I set up Google Analytics?
    • Exporting and deploying my frontend code
    • What is a partial and what is a prefab?
    • How to set up a basic mulit tenant app on Dittofi
    • How to write a custom endpoint in Dittofi
    • How to integrate Font Awesome into Dittofi
    • How can I set up Dittofi to run on my own private server?
    • How to add roles such as Super Admin, Admin, Manager & User
    • How to prevent duplicate records being added to the database
    • What to do when relation cannot be created because their is violating data in the referenced table
  • Change Logs
    • Dittofi Change Logs
Powered by GitBook
On this page
  • Step 1. Add a new backend template
  • Step 2. Code your custom function
  • Step 3. Add a run custom code endpoint
  • Step 4. Add a run custom code action
  • Step 5. Build and test your code
  • (Optional) review code documentation

Was this helpful?

  1. Learn Backend
  2. Events
  3. Other

Run Custom

This article looks at how you can write a custom function and add this to your actions and events.

PreviousAssignNextRedirect Web Page Event

Last updated 2 years ago

Was this helpful?

The run custom code event allows you to add custom code into your Dittofi backend. Your custom backend code must be written in Google GO.

You can write to types of custom code (1) custom functions and (2) custom endpoints. In this user guide we are going to look at how to write and run custom functions inside Dittofi.

To learn how to write a custom endpoint read the article, .

WHY CUSTOM CODE?

There are two main reasons that you might want to use custom code in the Dittofi no-code platform:

  1. To add functionality to your backend where, using custom code would be easier than using the out of the box event types

  2. To create some functionality that is very specific to your app and where the event type does not already exist inside Dittofi. For example, many clients use the custom code section to write custom analytics.

You can add custom code to your app in 5 quick and easy steps.

  • Step 1. Add a new backend template

  • Step 2. Code your custom function

  • Step 3. Add a run custom code endpoint

  • Step 4. Add a run custom code action

  • Step 5. Build and test your code

Step 1. Add a new backend template

Custom code can be input into the system using the templates tab inside the Dittofi Design Studio. The templates tab is under the wrench icon in the purple side panel, as shown below.

From inside the templates tab, you must first add a new template into the folder Backend > Templates.

In the next step we are going to add in some custom code and add a custom function to add together two numbers.

Step 2. Code your custom function

After you've created your template you need to add the comment into your code "// @custom" at the top of the file, as shown below.

The "// @custom" comment tells the system that this is a custom file, rather than one of Dittofi's automatically generated ones.

NOTE

Automatically generated code for your backend is found in the folder Generated Code. All of this code can be exported from Dittofi, customized and debugged locally and the re-imported into Dittofi.

You also need to import the package "main" as shown below.

Finally, you can add any custom function written in GO. In this example we create the following function to add together two integers. Note that we need to tag this function "// @custom" as well.

// Custom function for addition
// @custom
func Add(left int, right int) (sum int, err error){

return left + right, nil
    
}

** IMPORTANT **

The final parameter of the function must always be of type error. You can see this above where we return a variable err of type error.

Step 3. Add a run custom code endpoint

Remember to save your endpoint and then head over to the actions tab.

Step 4. Add a run custom code action

Inside the action, give your action a name and link the action to the Run Custom Code Endpoint that we created in Step 3 of this article.

Next, add the Run Custom Code Event to your action as below.

In the parameters for the event, select your custom function from the list of dropdown options.

As you can see, the custom code event will update to show the function arguments that are available for the custom function and the return value that we wrote in Step 2. of this guide. You can see this below.

For the purpose of this example, we can create two new variables inside our event, and map these variables into our custom code event. The GIF below shows us adding the second of these variables which we call "Two" and will store the number "2".

Our other variable is the number one and is stored inside the Variable "One", as show below. This can be added in the same way as the variable above.

Next, we add map these variables into our custom Add function as shown below and set a return value.

Finally, under our Trigger Component we can set the Response Variable that will be returned to our Action. In this case we choose the variable "ReturnSum" that we set above.

Step 5. Build and test your code

If you've set everything up correctly, you will get the response code 200 and will see the number 3 returned in the endpoint response body. This is because our addition function is adding up the numbers 1 and 2 that we input into in Step 4. of this article.

(Optional) review code documentation

Dittofi auto generates real source code for your apps. To help stress this point, if you open up the help panel by clicking on the question mark icon inside your help panel, you will see the option for "Code Documentation".

Opening this up, you can see that the code that has generated now include the custom function "Add". This is the custom function that we just wrote.

If you scroll down the code documentation, you can see the custom code that has been injected.

You can also use the Dittofi code export function to get your full stack code and read and add to your go code directly in there.

Save your custom code by clicking on. This completes this step.

Next, we are going to add an endpoint. To do this head over to the endpoints tab, click and fill in the parameters for the endpoint as below.

Next, inside the actions tab, add a run custom code action. To do this, start by creating a new action. This can be done by clicking on in the top right of the screen.

The final step is to build your code and test it. To do this click . Next, head over to your endpoints tab, open your Run custom code endpoint and click run in the top right hand corner.

🟥
🟦
how to write a custom endpoint in Dittofi
Endpoint Response Body