有没有办法将多个(和不同的)参数从闪电网络组件(LWC)中的 JS 传递给 Apex 控制器类?

我目前遇到一个问题,希望这里有人可以帮助我。我当然也希望这是问它的正确地方。


我正在尝试在触发事件时创建具有相应发票行记录的自定义发票记录。我已经有一些逻辑来收集 JS 中选定行的 ID。


我已经能够创建发票记录(使用 LDS)和发票行记录(使用 Apex),但似乎无法为发票行记录传递发票 ID。我知道我能够创建记录,因为当我使用硬编码的发票 ID 测试它时它可以工作。


是否可以将 List 和 String 的多个参数传递给 LWC 中的 Apex 方法?


我将不胜感激任何帮助。提前致谢!


JS


    selectedRowsEvent(event) {


    ...some codes here...


    this.selectedRecords = Array.from(conIds);

    }



    handleSave() {


         **invId;**


         ...some codes here...


        createRecord(recordInput)

        .then(invoice => {

            **this.invId = invoice.Id;**  

            **createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})**

        }).catch(error => {


          ...some codes here...


          });

    }

控制器


@AuraEnabled

    public static void createInvLines(list<Id> lstConIds, string invoiceId){

        if(lstConIds.size() > 0){

            List<OpportunityLine__c> oppLst = new List<OpportunityLine__c>([SELECT Id, Description__c FROM OpportunityLine__c WHERE Id = :lstConIds]);

            try {

                List<InvoiceLine__c> lstInvLinesToInsert = new List<InvoiceLine__c>();

                for(OpportunityLine__c idCon : oppLst) {

                    lstInvLinesToInsert.add(new InvoiceLine__c(**InvoiceId__c = invoiceId**, Description__c = idCon.Description__c));

                }

                if(!lstInvLinesToInsert.isEmpty()) {

                    insert lstInvLinesToInsert;

                }

            }

            catch(Exception ex) {

                throw new AuraHandledException(ex.getMessage());

            }

        }

    }  


MM们
浏览 124回答 2
2回答

MYYA

是的,您可以将复杂的参数传递给标记为@AuraEnabled. 在客户端,它将是一个具有正确字段名称的 JSON 对象,就像您已经拥有的{ lstConIds : this.selectedRecords, invoiceId : this.invId}. 在 Apex 方面,它可以是具有多个参数或只有 1 个参数的函数(一些辅助包装类,同样具有正确的字段名称)。Salesforce 将在调用您的代码之前为您“解包”该 JSON 并放入正确的字段中。您的偏好会更清洁。我倾向于使用包装器。如果您有一个可重用的类似服务的功能,并且您想稍后添加一些可选参数 - 您只需将新字段放入包装类并完成工作。向其他顶点代码中使用的函数添加新参数可能并不容易,有点混乱。(在您的情况下,我肯定会尝试将发票和行项目创建为 1 个调用,因此如果有任何失败 - 正常的交易回滚将帮助您。如果其中一个项目失败 - 您不想只留下发票标题,正确的?)您看过https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex吗?这是一堵文字墙,但它提到了有趣的例子,在那里搜索“apexImperativeMethodWithParams”。在这里查看 JS 文件:https ://github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/default/lwc/apexImperativeMethodWithComplexParams 看看它是如何调用的ApexTypesController {&nbsp; &nbsp; @AuraEnabled(cacheable=true)&nbsp; &nbsp; public static String checkApexTypes(CustomWrapper wrapper) {...CustomWrapper在哪里public with sharing class CustomWrapper {&nbsp; &nbsp; class InnerWrapper {&nbsp; &nbsp; &nbsp; &nbsp; @AuraEnabled&nbsp; &nbsp; &nbsp; &nbsp; public Integer someInnerInteger { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; @AuraEnabled&nbsp; &nbsp; &nbsp; &nbsp; public String someInnerString { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; @AuraEnabled&nbsp; &nbsp; public Integer someInteger { get; set; }&nbsp; &nbsp; @AuraEnabled&nbsp; &nbsp; public String someString { get; set; }&nbsp; &nbsp; @AuraEnabled&nbsp; &nbsp; public List<InnerWrapper> someList { get; set; }}

手掌心

问题是插入是异步的,并且您正在同步触发它们。因此,这意味着您正在尝试在父记录完成之前插入行。// CREATE THE INVOICE RECORD&nbsp; &nbsp; createRecord(recordInput)&nbsp; &nbsp; &nbsp; &nbsp; .then(invoice => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **this.invId = invoice.Id;**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Call the next function here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// CREATE THE INVOICE LINE RECORDS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.then(result => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...some codes here...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.catch(error => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...some codes here...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript