繁体   English   中英

有没有办法从闪电 Web 组件(LWC)中的 JS 将多个(和不同的)参数传递给 Apex Controller class?

[英]Is there a way to pass multiple (and different) parameters to an Apex Controller class from JS in Lightning Web Components (LWC)?

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

我正在尝试在触发事件时创建具有相应发票行记录的自定义发票记录。 我已经有一些逻辑来收集 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...

          });
    }

Controller

@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());
            }
        }
    }  

是的,您可以将复杂的参数传递给标记为@AuraEnabled的方法。 在客户端,它将是一个 JSON object 具有正确的字段名称,就像您已经拥有{ lstConIds: this.selectedRecords, invoiceId: this.invId} 在 Apex 方面,它可以是 function 和多个 arguments 或只有 1 个参数(一些辅助包装器 class,同样具有正确的字段名称)。 Salesforce 将为您“解包” JSON 并在调用您的代码之前放入正确的字段中。

您的偏好会更清洁。 我倾向于使用包装器。 如果您有类似 function 的可重用服务,并且您想稍后添加一些可选参数 - 您只需将新字段放入包装器 class 并完成工作。 向其他顶点代码中使用的 function 添加新参数可能并不容易,有点混乱。

(在您的情况下,我肯定会尝试将发票和行项目创建为 1 个调用,因此如果有任何失败 - 正常的事务回滚将帮助您。如果其中一个项目失败 - 您不想只留下发票 header,正确的?)

你见过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 {
    @AuraEnabled(cacheable=true)
    public static String checkApexTypes(CustomWrapper wrapper) {
...

CustomWrapper在哪里

public with sharing class CustomWrapper {
    class InnerWrapper {
        @AuraEnabled
        public Integer someInnerInteger { get; set; }
        @AuraEnabled
        public String someInnerString { get; set; }
    }

    @AuraEnabled
    public Integer someInteger { get; set; }
    @AuraEnabled
    public String someString { get; set; }
    @AuraEnabled
    public List<InnerWrapper> someList { get; set; }
}

问题是插入是异步的,并且您正在同步触发它们。 因此,这意味着您正在尝试在父记录完成之前插入行。

// CREATE THE INVOICE RECORD
    createRecord(recordInput)
        .then(invoice => {
                **this.invId = invoice.Id;**
                // Call the next function here

                   // CREATE THE INVOICE LINE RECORDS
                **createInvLines({ lstConIds : this.selectedRecords, invoiceId : this.invId})**
                 .then(result => {
                   ...some codes here...
                  })
                 .catch(error => {
                   ...some codes here...

                 }); 
              );
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM