Adding and Deleting Table Data with Scripts
Expected Results





Implementation Method (Add)
- Create a low-code application.
- Apply for a free trial or purchase a commercial instance by referring to Authorization of Users for Huawei Cloud Astro Zero Usage and Instance Purchases.
- After the instance is purchased, click Access Homepage on Homepage. The application development page is displayed.
- In the navigation pane, choose Applications. On the displayed page, click Low-Code or
.
When you create an application for the first time, create a namespace as prompted. Once it is created, you cannot change or delete it, so check the details carefully. Use your company or team's abbreviation for the namespace.
- In the displayed dialog box, choose Standard Applications and click Confirm.
- Enter a label and name of the application, and click the confirm button. The application designer is displayed.
Figure 6 Creating a blank application
- Create an object named product and add fields to the object.
- In the navigation pane, choose Data, and click + next to Object.
- Set Object Name and Unique ID of the object to product and click the confirm button.
Figure 7 Creating object product
- Click
to go to the object details page.
- On the Fields tab page, click Add and add the ProName field for the object.
Figure 8 Adding the ProName field
Table 3 Parameters for adding the proName field Parameter
Description
Example
Display Name
Name of the new field, which can be changed after the field is created.
Value: 1–63 characters.
proName
Unique ID
ID of a new field in the system. The value cannot be changed after the field is created. Naming rules:
- Max. 63 characters, including the prefix namespace.
- Start with a letter and use only letters, digits, and an underscore (_). Do not end with an underscore (_).
proName
Field Type
Click
. On the page that is displayed, select the type of the new field based on the parameter description.
Text
- On the Fields tab, click Add again to add the proId field.
Figure 9 Adding the proId field
- Create a script.
- In the navigation pane, choose Logic and click + next to Script.
- Create a script, set the name to sqlAdd, and click Add.
Figure 10 Creating the sqlAdd script
- In the script editor, enter the sample code.
The sample code implements the following function: When the Add button is clicked, data such as the name and ID entered at the frontend is obtained and inserted into the object. If the insertion fails, the failure information is recorded. In the example, Namespace __product__CST is the object created in 2.
//This script is used to create a work order. import * as db from 'db';//Import the standard library related to the object. import * as context from 'context';//Import the standard library related to the context. //Define the input parameter structure. @action.object({ type: "param" }) export class ActionInput { @action.param({ type: 'String', required: true, label: 'String' }) proId: string; @action.param({ type: 'String', required: true, label: 'String' }) proName: string; } //Define the output parameter structure. The output parameter contains one parameter, that is, the record ID of workOrder. @action.object({ type: "param" }) export class ActionOutput { @action.param({ type: 'String' }) id: string; } //Use the data object namespace __product__CST. @useObject(['Namespace__product__CST']) @action.object({ type: "method" }) export class CreateWorkOrder { //Define the API class. The input parameter of the API is ActionInput, and the output parameter is ActionOutput. @action.method({ input: 'ActionInput', output: 'ActionOutput' }) public createWorkOrder(input: ActionInput): ActionOutput { let out = new ActionOutput(); //Create an instance of the ActionOutput type as the return value. let error = new Error(); //Create an instance of the error type to save the error information when an error occurs. try { let productData = new Object(); productData['Namespace__proName__CST'] = input.proName; //Assign the input parameter to the productData variable for future use. productData['Namespace__proId__CST'] = input.proId; let s = db.object('Namespace__product__CST'); //Obtain the operation instance of the Namespace__product__CST object. let id = s.insert(productData); if (id) { out.id = id; } else { error.name = "WOERROR"; error.message = "Unable to create product!"; throw error; } } catch (error) { console.error(error.name, error.message); context.setError(error.name, error.message); } return out; } }
- Click
to save the script. After the script is saved, click
to activate the script.
- Create an object model.
- In the navigation pane, choose Page, and click + next to Standard Page.
- At the bottom of the standard page, click Model View.
- Click New, specify Model Name (for example, sqlAdd), select Services for Source, and click Next.
Figure 11 Creating a model
- Select the script created in 3 and click the confirm button.
Figure 12 Selecting the script
- Click Next and then click OK.
- Return to Designer View and bind the model with the widgets.
- On the standard page, drag two Input widgets and one Button widget, change the Label of two input widgets to name and id, and set the button Display Name to Add.
Figure 13 Final page setting effect
- Select the name input widget, choose Properties > Data Binding and click
next to Value Binding.
- Select the model (proName) created in 4 and bind the input widget with the script data.
Figure 14 Selecting the model
- Repeat the preceding operations to bind the model (proId) created in 4 to the id input widget.
Figure 15 Binding a model to the id input widget
- On the standard page, drag two Input widgets and one Button widget, change the Label of two input widgets to name and id, and set the button Display Name to Add.
- Add an event for the Add button.
- Select the Add button widget and click the Events tab.
- Click + next to on-click. The page for adding an action is displayed.
- Under Custom Action, enter the custom code and click Create.
Figure 16 Customizing an action
In this example, the customized JavaScript code is used to call the service after the submit button is clicked. The service calls the script to add records.
$model.ref('sqlAdd').run().then(function(data){ console.log(data); }).catch(function(error){ console.log('error is', error); });
- Return to the standard page and click
to save the page settings. After the page is saved, click
to preview the effect.
Implementation Method (Delete)
- Create a low-code application.
- Apply for a free trial or purchase a commercial instance by referring to Authorization of Users for Huawei Cloud Astro Zero Usage and Instance Purchases.
- After the instance is purchased, click Access Homepage on Homepage. The application development page is displayed.
- In the navigation pane, choose Applications. On the displayed page, click Low-Code or
.
When you create an application for the first time, create a namespace as prompted. Once it is created, you cannot change or delete it, so check the details carefully. Use your company or team's abbreviation for the namespace.
- In the displayed dialog box, choose Standard Applications and click Confirm.
- Enter a label and name of the application, and click the confirm button. The application designer is displayed.
Figure 17 Creating a blank application
- Create a deletion script.
- In the navigation pane, choose Logic and click + next to Script.
- Create a script, set the name to sqlDelete, and click Add.
Figure 18 Creating the sqlDelete script
- In the script editor, enter the sample code.
The script in this example is used to delete a data record based on the ID entered on the page through API. If an error is reported, the error information is recorded. In the example, Namespace __product__CST is the object created in 2.
//This script is used to delete work orders. import * as db from 'db';//Import the standard library related to the object. import * as context from 'context';//Import the standard library related to the context. //Define the input parameter structure. @action.object({ type: "param" }) export class ActionInput { @action.param({ type: 'String', required: true, label: 'String' }) id: string; } //Define the output parameter structure. The output parameter contains one parameter, that is, the record ID of workOrder. @action.object({ type: "param" }) export class ActionOutput { @action.param({ type: 'String' }) id: string; } //Use the data object namespace __product__CST. @useObject(['Namespace__product__CST']) @action.object({ type: "method" }) export class DeleteWorkOrder { //Define the API class. The input parameter of the API is ActionInput, and the output parameter is ActionOutput. @action.method({ input: 'ActionInput', output: 'ActionOutput' }) public deleteWorkOrder(input: ActionInput): ActionOutput { let out = new ActionOutput(); //Create an instance of the ActionOutput type as the return value. let error = new Error(); //Create an instance of the error type to save the error information when an error occurs. try { let id = input.id; let s = db.object('Namespace__product__CST'); //Obtain the operation instance of the Namespace__product__CST object. //Query condition let condition = { "conjunction": "AND", "conditions": [{ "field": "Namespace__proId__CST", "operator": "eq", "value": id }] }; let isDeleted = s.deleteByCondition(condition); if (isDeleted) { out.id = id; } else { error.name = "WOERROR"; error.message = "Failed to delete the work order!"; throw error; } } catch (error) { console.error(error.name, error.message); context.setError(error.name, error.message); } return out; } }
- Click
to save the script. After the script is saved, click
to activate the script.
- Create an object model.
- In the navigation pane, choose Page, and click + next to Standard Page.
- Enter the label and name of the page and click Add to create a standard page.
- At the bottom of the standard page, click Model View.
- Click New, specify Model Name (for example, sqlDelete), select Services for Source, and click Next.
Figure 19 Creating a model
- Set Service Type to Script. On the displayed Select Service page, select the script created in 2 and click OK.
Figure 20 Selecting the script
- Click Next and then click OK.
- Return to Designer View and bind the model with the widgets.
- Drag an Input widget and a Button widget to the standard page, set the Label of the input widget to id, and set the Display Name of the button widget to Delete.
- Select the input widget, choose Properties > Data Binding and click
next to Value Binding.
- Bind the input widget with the input parameter id of the sqlDelete model created in 3.
Figure 21 Selecting the model
- Add an event for the Delete button.
- Select the Delete button widget and click the Events tab.
- Click + next to on-click. The page for adding an action is displayed.
- Under Custom Action, enter the custom code and click Create.
Figure 22 Customizing an action
The custom JavaScript code in the example is used to call the backend service, and the backend service calls the script to delete data.
$model.ref('sqlDelete').run().then(function(data){ console.log(data); }).catch(function(error){ console.log('error is', error); });
- Click
in the upper part of the page to save the page settings.
- After the settings are saved, click
in the upper part of the page to preview the effect.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot