## 6.5.6 Subflows and Conversations

Grails' Web Flow integration also supports subflows. A subflow is like a flow within a flow. For example take this search flow:

    def searchFlow = {
        displaySearchForm {
            on("submit").to "executeSearch"
        }
        executeSearch {
            action {
                [results:searchService.executeSearch(params.q)]
            }
            on("success").to "displayResults"
            on("error").to "displaySearchForm"
        }
        displayResults {
            on("searchDeeper").to "extendedSearch"
            on("searchAgain").to "displaySearchForm"
        }
        extendedSearch {
            subflow(extendedSearchFlow)   // <--- extended search subflow
            on("moreResults").to "displayMoreResults"
            on("noResults").to "displayNoMoreResults"
        }
        displayMoreResults()
        displayNoMoreResults()
    }

It references a subflow in the `extendedSearch` state. The subflow is another flow entirely:

    def extendedSearchFlow = {
        startExtendedSearch {
            on("findMore").to "searchMore"
            on("searchAgain").to "noResults"
        }
        searchMore {
            action {
               def results = searchService.deepSearch(ctx.conversation.query)
               if(!results)return error()
               conversation.extendedResults = results
            }
            on("success").to "moreResults"
            on("error").to "noResults"
        }
        moreResults()
        noResults()
    }

Notice how it places the `extendedResults` in conversation scope. This scope differs to flow scope as it allows you to share state that spans the whole conversation not just the flow. Also notice that the end state (either `moreResults` or `noResults` of the subflow triggers the events in the main flow:

    extendedSearch {
        subflow(extendedSearchFlow)   // <--- extended search subflow
        on("moreResults").to "displayMoreResults"
        on("noResults").to "displayNoMoreResults"
    }