带有自定义闪电组件的无效页面URL

时间:2019-05-14 12:28:39

标签: salesforce salesforce-lightning

我创建了一个新的自定义闪电组件和控制器,以在Salesforce中创建新的销售线索。当您正确输入所有字段并通过验证时,页面将按设计工作。但是,当某个字段验证失败,例如电子邮件格式不正确(abc.abc.com)时,我将收到以下错误消息。

页面不存在-输入有效的URL,然后重试。

我还从Salesforce收到了成功祝酒词,但未创建销售线索。我相信getState响应的控制器存在问题,但无法弄清楚我错了。

这里是组件

组件

<aura:component implements="lightning:actionOverride" access="global" controller="overrideStandabuttonwithLC">
    <aura:attribute name="lea" type="Lead" default="{'sobjectType': 'Lead',
                                                        'FirstName':'',
                                                        'LastName':'',
                                                        'Title':'',
                                                        'Email':'',
                                                        'Phone':''}" />    
    <div class="slds-m-around--large">
        <div class="slds-form--stacked">
            <div class="slds-form-element">  
                <div class="slds-form-element__control">
                    <lightning:input aura:id="leaFirstName" label="First Name" value="{!v.lea.FirstName}" class="slds-input"/>
                    <lightning:input aura:id="leaLastName" required="true" label="Last Name" value="{!v.lea.LastName}" class="slds-input"/>
                    <lightning:input aura:id="leaTitle" label="Title" value="{!v.lea.Title}" class="slds-input"/>
                    <lightning:input aura:id="leaEmail" label="Email" value="{!v.lea.Email}" placeholder="abc@email.com..." class="slds-input"/>
                    <lightning:input aura:id="leaPhone" label="Phone" value="{!v.lea.Phone}" class="slds-input"/>
                  </div>
            </div>
            <div class="slds-m-around--medium">
                <button class="slds-button slds-button--brand" onclick="{!c.createLead}">Save</button>
            </div>
        </div>
    </div>    
</aura:component>

JavaScript控制器

({
    createLead : function(component, event, helper) {
        var action = component.get("c.saveLead");
        action.setParams({
            "leaRec":component.get("v.lea")
        });
        action.setCallback(this, function(response){
            if(response.getState()==='SUCCESS'){
                var leaId = response.getReturnValue();
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "type":"Success",
                    "message": "Lead created successfully."
                });

                toastEvent.fire();


                var navEvt = $A.get("e.force:navigateToSObject");
                navEvt.setParams({
                    "recordId": leaId,
                    "slideDevName": "related"
                });
                navEvt.fire();
            }
        });
        $A.enqueueAction(action);
    },
})

顶点

public with sharing class overrideStandabuttonwithLC {

    @AuraEnabled
    public static ID saveLead(Lead leaRec){
        try{
            insert leaRec;
        }
        catch(Exception e){
            system.debug('e-->' + e.getMessage());
        }

        return leaRec.Id;
    }  

}

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是有道理的。您的控制器方法saveLead失败。永远不会插入潜在顾客记录。您正在将null返回到您的组件。您无法导航到空记录。

您需要更好的错误处理。如果leadId未定义/为空,则应该向用户显示错误。如果leadId有一个值,那么可以安全地导航到该记录。

或者,您可以从服务器方法中删除try / catch,并使回调如下所示

action.setCallback(this, function(response){
    var toastEvent = $A.get("e.force:showToast");
    if(response.getState()==='SUCCESS'){
        var leaId = response.getReturnValue();

        toastEvent.setParams({
            "title": "Success!",
            "type":"Success",
            "message": "Lead created successfully."
        });

        toastEvent.fire();


        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": leaId,
            "slideDevName": "related"
        });
        navEvt.fire();
    } else {
        let errors = response.getError();
        let message = 'Unknown error'; // Default error message
        // Retrieve the error message sent by the server
        if (errors && Array.isArray(errors) && errors.length > 0) {
            message = errors[0].message;
        }
        toastEvent.setParams({
            "title": "Error!",
            "type":"Error",
            "message": message
        });

        toastEvent.fire();
    }
});