首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Salesforce LWC - Multiple Apex Callout必须失败

Salesforce LWC - Multiple Apex Callout必须失败
EN

Stack Overflow用户
提问于 2020-08-24 20:23:57
回答 1查看 513关注 0票数 1

我正在尝试从FOR循环强制向Apex发出多个标注。但奇怪的是,只有前6笔交易取得了成功。其余的都失败了。开发人员文档中没有提到从LWC到Apex的标注数量限制。

采用这种方法的原因是每个标注都将遵循一组新的Apex限制。

代码语言:javascript
复制
import { LightningElement, api, wire, track } from 'lwc';
import insertRecords from '@salesforce/apex/FileUploaderXCtrl.insertRecords';

export default class FileUploadExample extends LightningElement {

    file;
    filename;
    filecontent;
    output = [];
    buttonVisible = false;
    position = 0;

    get acceptedFormats() {
        return ['.csv'];
    }

    uploadFiles(event) {
        console.log('Hey! No of Callouts: ----------->' + this.output.length);
        for (let index = 0; index < this.output.length; index++) {
            console.log('Making Callouts Now! Watch Out --------');
            insertRecords({ jsonObjInput: this.output[index] })
            .then(() => { console.log('*******\n\nHurray! Its Working....\n\n********'); })
            .catch((error) => { console.log('Go Home...'); });
            
        }
    }

    handleUploadFinished(event) {
        if (event.target.files.length > 0) {
            this.filename = event.target.files[0].name;
            this.file = event.target.files[0];
            var reader = new FileReader();
            var jsonObj = [];
            reader.readAsText(this.file, "UTF-8");
            reader.onload = (evt) => {
                console.log('File Name: ----------->' + this.filename);
                this.filecontent = evt.target.result;
                let rows = this.filecontent.split('\n');
                let header = rows[0].split(',');
                rows.shift();
                console.log('Header: ----------->' + header);
                rows.forEach(element => {
                    let data = element.split(',');
                    let obj = {};
                    for (let index = 0; index < header.length; index++) {
                        obj[header[index].trim()] = data[index].trim();
                    }
                    jsonObj.push(obj);
                });
                let result = new Array(Math.ceil(jsonObj.length / 10000)).fill().map(_ => jsonObj.splice(0, 10000));
                result.forEach(element => {
                    this.output.push(JSON.stringify(element));
                });
                console.log('Apex Input Parameter: ----------->' + this.output);
                console.log('No of Callouts: ----------->' + this.output.length);
            }
            reader.onloadend = (evt) => {
                this.buttonVisible = true;
            }
            reader.onerror = (evt) => {
                if (evt.target.error.name == "NotReadableError") {
                    console.log('An Error Occured Reading this File!!');
                    alert('An Error Occured Reading this File!!');
                }
            }
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2020-08-25 03:07:41

stackexchange上已经有几篇关于这方面的好文章了。这就是所谓的boxcarring,由于浏览器能够限制并行调用的数量,lwc会执行boxcarring。

https://salesforce.stackexchange.com/questions/263382/how-to-turn-boxcarring-off-for-lwc-imperative-apex-method-calls?atw=1

https://salesforce.stackexchange.com/questions/293025/boxcaring-is-removed-from-lwc-components

您应该在“为什么salesforce在lwc中使用boxcarring”下查看它们。

简而言之,您可以选择异步使用promises来避免这些,否则其他解决方法将是setTimeout。

这两种方法都不会提供很好的用户体验,相反,您应该探索其他替代方法来实现这一点,包括以下内容

在Heroku等应用程序上,

  • 对所有data operations
  • Built UI界面使用Bulk APIv2.0,并使用python或Node进行处理,而不是使用apex进行资源密集型工作。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63560998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档