Custom Code Fundamentals

Introduction

In this tutorial, you will learn to add and use custom code in Momen.

Prerequisites

Have a certain degree of JavaScript foundation, familiar with the definition of variables, functions, loops, conditional judgment, and other conventional code writing.

Adding Custom Code

Currently, the "Custom Code" in Momen's action flow only supports the JavaScript language.

If you want to write JS code to realize more complex business logic, you can add code blocks in the action flow, click the node where you want to add a code block and select the code block.

Configure Custom Code

After adding a code block, you can click on the code block to change its name. You can also directly write JS code in the code editing area on the right side. Or you can click on the Expand button in the upper right corner of the code editor to expand the code editor interface and use a bigger window to edit code.

Configuring and Getting Inputs

If you have a code block that needs to process user input (the uppermost "Input " of the action flow) or data generated by a higher-level node, you first need to have the required [Input] in the code block.

Take the example of processing cell phone number encryption:

First, configure the input parameter in the "action flow", the purpose here is to call the behavior flow on the page, you can enter parameters into the behavior flow.

Configure the input in the "Custom Code", the purpose here is to tell the code block what the specific value of the input is.

Note: The Input name has to be the same as the one in the function's parentheses that gets the Input in step 3!

To get the input parameter in "code", you need to use Momen built-in context.getArg("input parameter name"); function to get the input parameter.

Note: Names must be wrapped in semi-colonized "single quotes" or "double quotes.

const thing1 = context.getArg('thing1');
const thing4 = context.getArg('thing4');
const date2 = context.getArg('date2');
const accountId = context.getArg('accountId');
const data = { 'thing1':thing1, 'date2':date2, 'thing4':thing4
};
const gql = `mutation publishWechatMessage($data: Map_String_StringScalar,$accountId:Long!){
  publishWechatMessage(
    accountId: $accountId
    data: $data
    miniprogramState: FORMAL
    page: "pages/lil7gbne/lil7gbne"
    lang: CN
    templateId: "RQH7FafxS4_jQsRSN6ujLLKUoWuXpC3uLvYP1WL75Ys"
  )
}`;
context.runGql('publishWechatMessage', gql,{data,accountId}, { role: 'admin'});

Configuring and Returning Output

If you need to take the result of the code block to other nodes below the action flow, you need to configure the output parameter of the code block and write the code to return the parameter.

To return the Output in the Code, you need to use Momen's built-in context.setReturn("name of the Output", content of the Output); function to return the Output.

const phone_number = context.getArg('phone_number'); 
const result = `${phone_number.substring(0,3)}****${phone_number.substring(7,11)}`; 
context.setReturn('result_phone', result) ; 

Configure the output in "Custom Code", which tells the subordinate node the name and type of the output.

Note: The name of the output should be the same as the name in the context.setReturn() function in the code.

Configure the output in the action flow, the function here is to return the data in the parent node to be used by the front-end to the front-end. After this step, when the front-end page calls this action flow, there will be result data for the page to value/use.

If you encounter any issues during the process, feel free to join our Discord community for assistance.​​​

About Momen​​​​​

Momen is a no-code web app builder, allows users to build fully customizable web apps, marketplaces, Social Networks, AI Apps, Enterprise SaaS, and much more. You can iterate and refine your projects in real-time, ensuring a seamless creation process. Meanwhile, Momen offers powerful API integration capabilities, allowing you to connect your projects to any service you need. With Momen, you can bring your ideas to life and build remarkable digital solutions and get your web app products to market faster than ever before.​​

Last updated