Skip to main content

FaaS connector-delete form instances in batches

Note: We do not recommend that you use this OpenAPI directly in the front-end data source as an HTTP connector. For more information, see configure FaaS connectors in this case. Otherwise, systemToken parameters will be leaked, threatening the data security of the application.

1. Usage scenarios

The development language of this example is NodeJS

This example describes how to use the DingTalk open platform OpenAPI to delete form instances in batches.

2. Implement functions

2.1. Apply for DingTalk open platform application credentials and interface permissions

If you have applied, you can ignore this step.

宜搭自定义连接器鉴权凭证申请及接口权限申请 | 钉钉宜搭·帮助中心

2.2. Create and configure FaaS connectors

The connector used in this example:批量删除表单实例 - 开放平台

Note:

  1. If you select not to execute validation rules, associated business rules, and third-party service callbacks (executeExpression:false), then the number of form instances that can be deleted in batches at a time is 500.
  2. If you select execute validation rules, associated business rules, and third-party service callbacks (executeExpression:true)
    1. If you select synchronization (asynchronousExecution:false), 50 entries are allowed at a time.
    2. If asynchronous (asynchronousExecution:true), 300 entries are allowed at a time.

For more information, see:2022.04.21 版本更新-OpenAPI 全新升级 | 钉钉宜搭·帮助中心

2.2.1. Create FaaS connector

2.2.2. Configure connector basic information and development language

2.2.3. Configure connector execution actions

Note that the Body is parsed and the success flag bit is configured.

{
"formUuid" : "String",
"appType" : "String",
"asynchronousExecution" : true,
"formInstanceIdList" : "Array",
"executeExpression" : false
}
{
"success": true,
"error": ""
}

2.3. Develop FaaS connectors

2.3.1. Go to Cloud IDE development anddingOpenApiUtil.jsAdd code to the file

You need to save it after adding it.

Windows:Ctrl + S Mac:Command + S

batchRemoveFormData: async function (batchRemoveFormDataInputs) {
let client = createClient();
let batchRemovalByFormInstanceIdListHeaders = new $dingtalkyida_1_0.BatchRemovalByFormInstanceIdListHeaders({});
batchRemovalByFormInstanceIdListHeaders.xAcsDingtalkAccessToken = innerGetAccessToken();
let batchRemovalByFormInstanceIdListRequest = new $dingtalkyida_1_0.BatchRemovalByFormInstanceIdListRequest(batchRemoveFormDataInputs);
try {
const result = await client.batchRemovalByFormInstanceIdListWithOptions(batchRemovalByFormInstanceIdListRequest, batchRemovalByFormInstanceIdListHeaders, new $Util.RuntimeOptions({}));
return result;
} catch (err) {
if (!Util.empty(err.code) && !Util.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
return {
code: err.code,
message: err.message
}
}
throw err;
}
return null;
},

2.3.2. Replace the code in the faasEntry.js file completely.

Modify the fixed parameters according to the actual situation.

The application credentials applied for with appKey and appSecret as 2.1.

systemToken can be found directly in YIDA application settings-deployment O & M.

const { openApi: OpenAPIUtil, yidaConnector: YidaConnectorUtil, httpUtil: HttpUtil } = require('./utils');
const { ConnectorTypeEnum } = YidaConnectorUtil;
// 注意:涉及数据安全的参数(如:appKey、appSecret、systemToken)一定要在服务端发送,不可在前端数据源里传入。
const appKey = 'XXXXXXXXXXXXXX';
const appSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
const systemToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

module.exports = async function (faasInputs, context) {
// 这里的 inputs 跟你配置连接器时的 Body 参数及字段格式保持一致
const { inputs = {}, yidaContext = {} } = faasInputs || {};
const userId = yidaContext ? yidaContext['userId'] : ''; // 从连接器上下文获取调用人的 userId
const corpId = yidaContext ? yidaContext['corpId'] : ''; // 从连接器上下文获取调用组织的 corpId
const accessToken = yidaContext ? yidaContext['accessToken'] : '';
const consumeCode = yidaContext ? yidaContext['consumeCode'] : '';
OpenAPIUtil.setAccessToken(accessToken);
await OpenAPIUtil.getCustomAccessTokenThenCache(appKey, appSecret); // 获取调用连接器的 accessToken
YidaConnectorUtil.setConsumeCode(consumeCode);
inputs['systemToken'] = systemToken;
inputs['userId'] = userId;
return await batchDeleteFormData(inputs).then((result) => {
const { message = '' } = result;
return {
success: !message,
content: null,
error: message,
};
}).catch((error) => {
return {
success: false,
content: null,
error: error,
};
});
};

async function batchDeleteFormData(inputs) {
return await OpenAPIUtil.batchRemoveFormData(inputs);
}

2.4. Deploy FaaS connector

Save the added code.

When the code is successfully deployed, the FaaS Connector Cloud IDE development is completed.

2.5. Return to the connector configuration and click save

2.6. On the YIDA page, configure data sources and use data sources

2.6.1. Get the data displayed in the table

For more information about how to delete data, see the following example.

自定义页面表格实现数据管理页功能 | 钉钉宜搭·帮助中心

2.6.2. Select the connector you just created in the data source

2.6.3. Configure batch delete confirmation pop-up window

The configuration is as follows:

2.6.4. Configure the top operation and bind the following functions

// 批量删除弹窗
export function onBatchDeleteDataDialog() {
const { selectedRowKeys = [] } = this.state;
if (!selectedRowKeys.length) {
this.utils.toast({
title: '当前未选中任何数据',
type: 'warning',
});
return;
}
this.$('dialog_lpjg5aha').show(() => {
this.$('numberField_lpjg8ky1').setValue(selectedRowKeys.length);
});
}

2.6.5. Configure batch delete pop-up actions and bind the following functions

// 确认批量删除
export function onBatchDeleteDataOk() {
const { selectedRowKeys = [] } = this.state;
this.$('dialog_lpjg5aha').set('confirmState', 'LOADING'); // 开启对话框加载状态
// 批量删除表单数据
this.dataSourceMap.batchDeleteData.load({
inputs: JSON.stringify({
body: {
formUuid, // 数据表formUuid
appType: pageConfig.appType, // 应用 appType
formInstanceIdList: selectedRowKeys, // 需要删除的数据实例id列表
asynchronousExecution: true, // 是否需要异步执行该任务
executeExpression: false, // 是否需要触发表单绑定的校验规则、关联业务规则和第三方服务回调
},
}),
}).then((res) => {
this.$('dialog_lpjg5aha').set('confirmState', 'NORMAL'); // 关闭对话框加载状态
const { success, error } = res;
if (success) {
this.$('dialog_lpjg5aha').hide();
this.utils.toast({
title: '批量删除成功',
type: 'success',
});
setTimeout(() => {
this.setState({
loading: true,
selectedRowKeys: [],
});
this.getData(); // 获取数据
}, 1000);
} else {
this.utils.toast({
title: error,
type: 'success',
});
}
}).catch(({ message }) => {
this.utils.toast({
title: message,
type: 'error',
});
this.$('dialog_lpjg5aha').set('confirmState', 'NORMAL'); // 关闭对话框加载状态
});
}

3. Effect

4. Try it online

This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
Copyright © 2024钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4