4

I (finally) finished my lightning component to enable & disable a scheduled job for an AppExchange app I'll publish shortly.

I added it to a VF page and all the functionality worked with the exception of toasts, as documented in this response: Toast event in Lightning not working

As the toast isn't critical, I'm looking to either have conditional logic in the Lightning Component that only fires a toast if not in VF, or to suppress the error in the Visualforce / Lightning container (I'm not sure which one displays the error).

Thanks!

VF:

<apex:page controller="updateUsingLightningCheckboxController">
<apex:includeLightning />
<div id="lightning"/>
<script>
    $Lightning.use("c:LightningAdoptionTrackerSetup", function() {
        $Lightning.createComponent("c:LightninAdoptionTrackerSetup",
                                   { },
                                   "lightning", 
                                   function(cmp) {
                                       //do some stuff
                                       });
        });
</script>
</apex:page>

Example of the toast in my component's helper:

    activateApp: function(cmp,evt) {
    var action = cmp.get("c.startScheduler");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var toastEvent = $A.get("e.force:showToast");
            cmp.set("v.isRunButtonDisabled", false);

            toastEvent.setParams({
                "message": response.getReturnValue(),
                "type": "success",
                "duration": 1500
            });
            toastEvent.fire();
        }//if
    });
    $A.enqueueAction(action);
}
3

When your component runs within one/one.app container(i.e in LEX and SF1), all force:xxx event such as $A.get("e.force:showToast"); would return an Object.

Since the component is inside VF page, the call to $A.get("e.force:showToast"); would return undefined

So all you have to do is have an if to check if the event exist or not.

//true only if the component runs within one/one.app container
if(toastEvent){
    toastEvent.setParams({
        "message": response.getReturnValue(),
        "type": "success",
        "duration": 1500
    });
    toastEvent.fire();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.