[SalesForce] Flow embedded within a Lightning Component, can’t navigate programmatically

I have a flow embedded within a custom lightning component where I'm trying to programmatically navigate the flow (hit the next button) if/when the user presses enter, here's the code I have so far:

Component:

<aura:component implements="lightning:availableForFlowScreens,flexipage:availableForRecordHome,force:hasRecordId,flexipage:availableForAllPageTypes" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <div onkeyup="{!c.onkeypress}">
        <lightning:flow aura:id="createNote"/>
    </div>
</aura:component>

Controller:

({ 
    init : function (component) {
        // Find the component whose aura:id is "createNote"
        var flow = component.find("createNote");
        // In that component, start the CreateNote_CustomAction2 flow with an initialized
        // input variable to pass the current recordId into the flow
        var input = [
            {
                name : "recordId",
                type: "String",
                value : component.get("v.recordId")
            }
        ]
        flow.startFlow("CreateNote_CustomAction2", input);
    },
    onkeypress : function (component, event, helper) {
        var navigate = component.get("v.navigateFlow");
        if (event.which === 13 || event.keyCode === 13) {
            navigate("NEXT");
        }
    }
})

The function to detect the enter key being pressed within the component is working fine, but it's not actually clicking the next button. Here's the error I get when I hit enter on the component:

Uncaught Action failed: c:CreateNote_LightningComponenet$controller$onkeypress [navigate is not a function]

onkeypress()@https://sandboxorg.lightning.force.com/lightning/r/Opportunity/0063J00000257PcQAI/components/c/CreateNote_LightningComponenet.js:27:13

The flow fires and runs fine if you click the next button within the component, but I can't seem to figure out how to correctly click the button programmatically on an enter press.

Best Answer

I think your approach might be the problem here. According to this article, you are supposed to put your component inside the flow. In what you have written, the flow is contained inside the component instead. That is a fundamental change on how the interface is supposed to work.

Here is the relevant part of the text

Since Aura components have access to a flow screen’s navigation actions, you can fully customize how the user moves between screens. For example, hide the default navigation buttons and have the flow move to the next screen when the user selects a choice

Try creating the flow, and placing the buttons to navigate inside it instead.