Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据
背景: 今天项目组小伙伴问了一个问题,如果更新数据超过1万条的情况下,有什么好的方式来实现呢?我们都知道一个transaction只能做10000条DML数据操作,那客户的操作的数据就是超过10000条的情况下,我们就只能搬出来salesforce government limitation进行拒绝吗?这显然也是不友好的行为。
实现方案:
1. 代码中调用batch,batch处理的数据量多,从而可以忽略这个问题。当然,这种缺点很明显:
1)不是实时的操作,什么时候执行取决于系统的可用线程,什么执行不知道;
2)如果batch数据中有报错情况下,我们应该如何处理呢?全部回滚?继续操作?可能不同的业务不同的场景,哪样都不太理想。所以batch这种方案不到万不得已,基本不要考虑。
2. 前端进行分批操作,既然每一个transaction有限制,我们就将数据进行分拆操作,比如每2000或者其他的数量作为一批操作,然后进行后台交互,这样我们可以实时的获取状态,一批数据处理完成以后,我们在进行另外一批。正好博客中貌似没有记录这种的需求,所以整理一篇,js能力有限,抛砖引玉,欢迎小伙伴多多的交流沟通。
TestObjectController.cls: 获取数据,以及更新数据两个简单方法
public with sharing class TestObjectController {
@AuraEnabled(cacheable=true)
public static List<Test_Object__c> getTestObjectList() {
List<Test_Object__c> testObjectList = [SELECT Id,Name
from Test_Object__c
ORDER BY CREATEDDATE limit 50000];
return testObjectList;
}
@AuraEnabled
public static Boolean updateTestObjectList(List<Id> testObjectIdList) {
List<Test_Object__c> testObjectList = new List<Test_Object__c>();
for(Id testObjectId : testObjectIdList) {
Test_Object__c objItem = new Test_Object__c();
objItem.Id = testObjectId;
objItem.Active__c = true;
testObjectList.add(objItem);
}
update testObjectList;
return true;
}
}
testLargeDataOperationComponent.html
<template>
<lightning-button label="mass update" onclick={handleMassUpdateAction}></lightning-button>
<template if:true={isExecution}>
<lightning-spinner size="large" alternative-text="operation in progress"></lightning-spinner>
</template>
<div style="height: 600px;">
<lightning-datatable
hide-checkbox-column
key-field="Id"
data={testObjectList}
columns={columns}>
</lightning-datatable>
</div>
</template>
testLargeDataOperationComponent.js:因为测试环境的数据有 data storage 5M的限制,所以数据达不到1万条,这里将每批处理的数据进行设置小一点点,打开log,从而更好的查看展示效果。
import { LightningElement, track, wire,api } from 'lwc';
import getTestObjectList from '@salesforce/apex/TestObjectController.getTestObjectList';
import updateTestObjectList from '@salesforce/apex/TestObjectController.updateTestObjectList';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{label: 'Name', fieldName: 'Name',editable:false}
];
export default class testLargeDataOperationComponent extends LightningElement {
@track testObjectList = [];
@track columns = columns;
@track isExecution = false;
@wire(getTestObjectList)
wiredTestObjectList({data, error}) {
if(data) {
this.testObjectList = data;
} else if(error) {
console.log(JSON.stringify(error));
}
}
async handleMassUpdateAction() {
let listSize = this.testObjectList.length;
let allOperateSuccess = false;
this.isExecution = true;
//hard code, default operate 1000 per transcation
let offset = 200;
let idList = [];
for(let index = 0; index < listSize; index ++) {
idList.push(this.testObjectList[index].Id);
if(index === listSize - 1) {
console.log('*** index : ' + index);
const finalResult = await updateTestObjectList({testObjectIdList: idList});
if(finalResult) {
this.isExecution = false;
this.dispatchEvent(
new ShowToastEvent({
title: 'operation successed',
message: 'the records are all updated success, size : '+ listSize,
variant: 'success'
})
);
idList = [];
}
} else if(index % offset == (offset -1)){
console.log('---index : ' + index);
const processResult = await updateTestObjectList({testObjectIdList: idList});
if(processResult) {
idList = [];
} else {
this.dispatchEvent(
new ShowToastEvent({
title: 'failed',
message: 'operation failed',
variant: 'error'
})
);
this.isExecution = false;
break;
}
}
}
}
}
效果展示
1. 初始化数据2000条,状态都是false

2. 当点击按钮时,展示spinner,同时每200条数据准备好会在后台进行一次数据操作,结果返回以后,在进行前端的整理,周而复始,一个说不上递归的数据操作。
当全部完成以后,展示成功toast信息

这样操作就可以实现超过10000条情况下,也可以进行同步的更新操作了。当然,这个现在只是一个初版,有没有什么问题呢?肯定有,比如在执行某200条数据错误的情况下,如何所有的数据进行回滚呢?如何记录已有的已经操作的数据呢?我们的后台返回类型可能就不是一个布尔类型可以搞定的了,有可能需要一个wrapper去封装一下曾经操作过的数据的ID,如果真的有错误情况下,调用其他的方法进行数据回滚操作(业务上回滚,而不是 savePoint rollback操作,此种实现不了)。
总结:相信不同的老司机对这种需求处理方式会有不同,本篇抛砖引玉,欢迎大神们交流以及给更好的建议意见。篇中错误地方欢迎指出,有不懂欢迎留言。
Salesforce LWC学习(三十八) lwc下如何更新超过1万的数据的更多相关文章
- Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...
- Salesforce LWC学习(三十) lwc superbadge项目实现
本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...
- Salesforce LWC学习(三十六) Quick Action 支持选择 LWC了
本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_quick_act ...
- Salesforce LWC学习(二十八) 复制内容到系统剪贴板(clipboard)
本篇参考: https://developer.mozilla.org/zh-CN/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipb ...
- Salesforce LWC学习(三十四) 如何更改标准组件的相关属性信息
本篇参考: https://www.cnblogs.com/zero-zyq/p/14548676.html https://www.lightningdesignsystem.com/platfor ...
- Salesforce LWC学习(三十五) 使用 REST API实现不写Apex的批量创建/更新数据
本篇参考: https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_compo ...
- Salesforce LWC学习(三十二)实现上传 Excel解析其内容
本篇参考:salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容 上一篇我们写了aura方式上传excel解析其内容.lwc作为salesforce的新宠儿,逐渐的 ...
- 前端学习(三十八)vue(笔记)
Angular+Vue+React Vue性能最好,Vue最轻=======================================================Angular ...
- (三十八)从私人通讯录引出的细节II -数据逆传 -tableView点击 -自定义分割线
项目中的警告是不会影响app发布的,例如引入第三方类库很容易引入警告. 细节1:跳转的数据传递. prepareForSegue: sender: 方法是在执行segue后,跳转之前调用这个方法,一般 ...
随机推荐
- The Ultimate Guide to Buying A New Camera
[photographyconcentrate] 六级/考研单词: embark, thrill, excite, intimidate, accessory, comprehensive, timi ...
- 【leetcode】43. Multiply Strings(大数相乘)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- 08-认证(Authorization)
这又是一个非常实用的功能,对我们做接口测试来说,经常要处理登录认证的情况 .如果不用这个Authorization其实也能解决认证的问题,无非就是把要认证的数据按照要求在指定位置传入参数即可.比如我们 ...
- 设置linux下oracle开机自启动
1.修改配置文件,vi /etc/oratab orcl:/u01/app/oracle/product/11.2.0/db_1:Y 2.创建启动文件,/etc/init.d/ #!/bin/sh # ...
- Spring Boot发布war包流程
1.修改web model的pom.xml <packaging>war</packaging> SpringBoot默认发布的都是jar,因此要修改默认的打包方式jar为wa ...
- Selenium和PhantomJS
Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上, ...
- SpringBoot自定义控制层参数解析
一.背景 在Spring的Controller中,我们通过@RequestParam或@RequestBody就可以将请求中的参数映射到控制层具体的参数中,那么这个是怎么实现的呢?如果我现在控制层中的 ...
- Pythonweb采集
一.访问页面 import webbrowser webbrowser.open('http://www.baidu.com/') pip3 install requests import re ...
- 记一次AWD
有幸bjx师傅又让我参加了一次awd,算是第二次体验awd,又感觉学习到了很多东西. 第一次打这种模式的时候,我几乎什么都没有做,就给师傅们下载文件,上传文件了.(太菜了) 昨晚分的组,发现没有人是p ...
- 突出显示(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 当一个大的项目文件做好以后,查看全部内容,肉眼多少会有点吃不消,这时就需要"划重点".在Porect里 ...