使用LWC多个签名调用Apex方法

发布于 2025-01-21 20:58:28 字数 805 浏览 0 评论 0 原文

我注意到LWC中有一些有趣的行为,这些行为正在建立,并且无法找到有关该原因的太多信息。基本上,我有一个带有多个签名的Apex方法:

// myMethod with 2 param signature
@AuraEnabled
public static void myMethod(String param1, String param2) {
     // I expect this method to be invoked when called from the LWC code shown below
     ....
}

// myMethod with 3 param signature
@AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
     // However this method gets invoked instead
     ....
}

当我尝试从LWC调用 myMethod 时,只将两个参数传递到该方法中时,我希望将调用2个参数签名方法但是,发生的是调用3个参数签名方法,并且 null 的值作为第三个参数值传递。

runJob({param1, param2}).then(value => ... )

这是预期的行为吗?当我通过APEX调用方法时,正确的方法会被其签名调用,但是从LWC调用该方法时,情况并非如此。

我有没有办法从LWC调用正确签名的 myMethod APEX方法?

I've noticed some interesting behavior in an LWC that I am building and haven't been able to find much info on the cause. Basically I have an Apex method declared with multiple signatures:

// myMethod with 2 param signature
@AuraEnabled
public static void myMethod(String param1, String param2) {
     // I expect this method to be invoked when called from the LWC code shown below
     ....
}

// myMethod with 3 param signature
@AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
     // However this method gets invoked instead
     ....
}

When I attempt to call myMethod from an LWC, and only pass two parameters into the method, I would expect that the 2 param signature method would be invoked, however what happens is that the 3 param signature method is invoked, and the value of null is passed as the third params value.

runJob({param1, param2}).then(value => ... )

Is this expected behavior? When I call the methods through apex, the correct method is invoked by its signature, however this doesn't seem to be the case when calling the method from an LWC.

Is there a way for me to invoke the myMethod Apex method of the correct signature from an LWC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2025-01-28 20:58:28

编辑:

将在22夏季发行的编译时间禁止使用。

原始:

它比您想象的要差。 runjob({param2,param1})也可以正常工作。 “正确”的意思是他们的名字很重要,而不是位置!

您的内容总是作为对象传递,而不是参数列表。您可以拥有一个辅助Apex包装器类,并且(假设所有字段均为 @auraenabled ),它将正确映射它们。如果您有参数列表 - 好吧,即使在调用代码之前,也会发生一种拆箱和类型匹配。如果您将“ ABC”传递给日期变量 - 即使在代码运行之前就会丢弃JSON避难所错误,则您无需捕获它。

如果Apex方法被超载,则选择哪种方法的选择是
非确定性(有效地随机),并且通过的参数可能
现在或将来导致错误。不要超载@auraenabled Apex
方法。

所以...选择不同的名字?或将它们汇合,以便3-Param版本查看最后一个参数,并在需要时调用2-Param One,并且业务逻辑允许。通常 - 保持简单,留下一些评论和良好的单位测试,或者从现在起2个月后,贫穷的维护人员会挣扎。

(如果使用 apex pmd pmd pmd plagin href =“ https://forcedotcom.github.io/sfdx-scanner/” rel =“ nofollow noreferrer”> sfdx scanner )具有长参数列表的功能,无论如何: https://pmd.github.ioio

of

  • oio “ rel =“ nofollow noreferrer”> https://github.com/trailheadheadapps/lwc-recipes/lwc-recipes/issues/issues/issues/196
  • https://github.com/trailheadapps/lwc-recipes/issues/196 “ https://salesforce.stackexchange.com/questions/359728/359728/apex-method-overloading-is-not-working-when-called-from-lwc-javascript-controlle">问题/359728/Apex-Method-overloading-is-not-working-not-not-working-from-lwc-javascript-controlle

Edit:

it will be banned at compile time in Summer'22 release. https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_ValidationForAuraEnabledAnnotation.htm&type=5&release=238

Original:

It's worse than you think. runJob({param2, param1}) will work correctly too. "Correctly" meaning their names matter, not the position!

Your stuff is always passed as an object, not a list of parameters. You could have a helper Apex wrapper class and (assuming all fields are @AuraEnabled) it'll map them correctly. If you have list of parameters - well, a kind of unboxing and type matching happens even before your code is called. If you passed "abc" to a Date variable - a JSON deserialization error is thrown even before your code runs, you have no means to catch it.

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_method

If the Apex method is overloaded, the choice of what method to call is
non-deterministic (effectively random), and the parameters passed may
cause errors now or in the future. Don't overload @AuraEnabled Apex
methods.

So... pick different names? Or funnel them so the 3-param version looks at the last param and calls 2-param one if needed and the business logic allows it. Generally - keep it simple, leave some comments and good unit tests or 2 months from now poor maintenance guy will struggle.

And (if you use the Apex PMD plugin and/or sfdx scanner) functions with long parameter lists are frowned upon anyway: https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文