JavaScript按钮转Lightning Component遇部署超时及代码创建错误
问题背景
我无法使用转换器转换JavaScript按钮。
我尝试使用Lightning Experience Configuration Converter将JavaScript按钮完整转换为Lightning Component,但在生产环境操作时每次都返回错误:"We were unable to deploy some metadata to your org due a timeout"。由于我可以预览组件和控制器代码,因此我打算直接将代码复制到沙箱后再推送部署。
但我通过Developer Console新建Lightning Component粘贴代码时出现报错,新建Apex Class作为控制器时也遇到了其他错误。
该按钮的设计作用为点击后为inscription记录分配唯一的订单编号。



相关代码
前端控制器代码
({ apexExecute : function(component, event, helper) { //Call Your Apex Controller Method. var action = component.get("c.updateOrderNumber"); action.setParams({ 'inscriptionId': ''+component.get('v.sObjectInfo.Id')+'' }); action.setCallback(this, function(response) { var state = response.getState(); console.log(state); if (state === "SUCCESS") { //after code if (''+component.get('v.sObjectInfo.Order_Number__c')+'' == '') { var result = response.getReturnValue(); window.location.reload(false); } else helper.showTextAlert(component, 'Order Number already Exist'); } else { } }); $A.enqueueAction(action); }, accept : function(component, event, helper) { $A.get("e.force:closeQuickAction").fire(); } })
组件代码
<aura:component controller="UpdateOrderNumberOnButtonClick" extends="c:LCC_GenericLightningComponent" > <aura:handler event="c:LCC_GenericApplicationEvent" action="{!c.apexExecute}"/> <aura:set attribute="partially" value="false"></aura:set> <aura:attribute name="showAlert" type="Boolean" default="false"/> <aura:attribute name="alertText" type="String"/> <aura:set attribute="isAdditionalObject" value="false"></aura:set> <div> <div class="slds-scrollable slds-p-around_medium slds-text-heading_small" id="modal-content-id-1"> <aura:if isTrue="{!v.showAlert}"> <p class="slds-hyphenate">{!v.alertText}</p> <aura:set attribute="else"> <div style="height: 6.75rem;"> <div role="status" class="slds-spinner slds-spinner_large slds-spinner_brand"> <span class="slds-assistive-text">Loading</span> <div class="slds-spinner__dot-a"></div> <div class="slds-spinner__dot-b"></div> </div> </div> </aura:set> </aura:if> <br/> </div> <footer class="slds-modal__footer"> <lightning:button class="slds-button_brand" onclick="{!c.accept}" label="Accept"/> </footer> </div> </aura:component>
原始JavaScript按钮代码
Here's your current JavaScript button code, to compare with our suggestion. {!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} var orderNumber = '{!Inscription__c.Order_Number__c}'; if(orderNumber == ''){ var result= sforce.apex.execute("UpdateOrderNumberOnButtonClick", "updateOrderNumber", {inscriptionId:'{!Inscription__c.Id}'}); window.location.reload(false); } else alert('Order Number already Exist');
报错解决方案
- 首先解决转换器部署超时问题:生产环境元数据过多时转换器容易触发超时,手动部署到沙箱再推生产的思路可行,不需要纠结转换器的部署报错。
- 修复手动粘贴代码的报错,按以下顺序操作:
- 先部署转换器依赖的基础组件:当前粘贴的组件继承了
c:LCC_GenericLightningComponent、依赖c:LCC_GenericApplicationEvent事件,这两个是Lightning Experience Configuration Converter自带的通用组件,需要先从转换器导出的完整元数据包里找到这两个组件的代码,先部署到沙箱,再粘贴当前的组件代码。 - 新建Apex控制器类
UpdateOrderNumberOnButtonClick,代码示例如下:
public class UpdateOrderNumberOnButtonClick { @AuraEnabled public static void updateOrderNumber(Id inscriptionId) { // 校验权限 if(!Schema.sObjectType.Inscription__c.isUpdateable()) { throw new AuraHandledException('没有修改Inscription记录的权限'); } Inscription__c ins = [SELECT Id, Order_Number__c FROM Inscription__c WHERE Id = :inscriptionId FOR UPDATE]; if(ins.Order_Number__c == null) { // 此处替换为你自己的唯一订单号生成逻辑,比如自动编号规则或者自定义编码逻辑 ins.Order_Number__c = 'ORD-' + String.valueOf(Datetime.now().getTime()); update ins; } else { throw new AuraHandledException('订单编号已存在'); } } }- 补全Helper方法:当前的JS控制器调用了
helper.showTextAlert方法,需要在组件的Helper文件里添加对应实现:
({ showTextAlert : function(component, message) { component.set('v.alertText', message); component.set('v.showAlert', true); } })- 配置快速操作:组件部署完成后,在Inscription对象的快速操作里新建一个Lightning Component类型的快速操作,选择刚创建的组件,把操作加到页面布局里即可使用。
- 先部署转换器依赖的基础组件:当前粘贴的组件继承了
- 如果不想依赖转换器的通用组件,也可以直接改写为不继承通用组件的独立Lightning组件,减少依赖:
改写后的组件代码示例:
对应的JS控制器调整代码:<aura:component controller="UpdateOrderNumberOnButtonClick" implements="force:lightningQuickAction,force:hasRecordId"> <aura:attribute name="showAlert" type="Boolean" default="false"/> <aura:attribute name="alertText" type="String"/> <aura:handler name="init" value="{!this}" action="{!c.apexExecute}"/> <div> <div class="slds-scrollable slds-p-around_medium slds-text-heading_small"> <aura:if isTrue="{!v.showAlert}"> <p class="slds-hyphenate">{!v.alertText}</p> <aura:set attribute="else"> <div style="height: 6.75rem;"> <div role="status" class="slds-spinner slds-spinner_large slds-spinner_brand"> <span class="slds-assistive-text">Loading</span> <div class="slds-spinner__dot-a"></div> <div class="slds-spinner__dot-b"></div> </div> </div> </aura:set> </aura:if> <br/> </div> <footer class="slds-modal__footer"> <lightning:button class="slds-button_brand" onclick="{!c.accept}" label="确定"/> </footer> </div> </aura:component>({ apexExecute : function(component, event, helper) { var action = component.get("c.updateOrderNumber"); action.setParams({ 'inscriptionId': component.get('v.recordId') }); action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { helper.showTextAlert(component, '订单编号生成成功'); setTimeout(function(){ window.location.reload(false); }, 1000); } else if (state === "ERROR") { var errors = response.getError(); if (errors && errors[0] && errors[0].message) { helper.showTextAlert(component, errors[0].message); } } }); $A.enqueueAction(action); }, accept : function(component, event, helper) { $A.get("e.force:closeQuickAction").fire(); } })
内容的提问来源于stack exchange,提问作者Raquel
相关产品推荐
相关产品推荐

