botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] KeyNotFoundException when using dynamic buttons

Open radennis opened this issue 9 years ago • 4 comments

Pre-flight checklist


System Information (Required)

SDK Platform: WebChat SDK Version: 3.5.2 Development Environment: Azure App Service Code: .Net

Issue Description

Describe the issue here. We are using Field.SetDefine to define in real time field buttons based on the value of a previous field. To be specific, we have a Cluster field and a Database field, the buttons for the Database field are defined based on the given cluster name. When a user changes the cluster name after already choosing a database, and then wants to see the form status, he gets the following exception:

Failure to complete form. Exception: The given key was not present in the dictionary.

Microsoft.Bot.Builder.FormFlow.FormCanceledException`1[KusBot.Forms.ChangePermissionForm]: The given key was not present in the dictionary. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.ValueDescription(IField`1 field, Object value, String format)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.ExpandTemplate(String template, String currentChoice, String noValue, T state, IField`1 field, Object[] args, IList`1& buttons)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.Prompt(T state, IField`1 field, Object[] args)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.ExpandTemplate(String template, String currentChoice, String noValue, T state, IField`1 field, Object[] args, IList`1& buttons)
   at Microsoft.Bot.Builder.FormFlow.Advanced.Prompter`1.Prompt(T state, IField`1 field, Object[] args)
   at Microsoft.Bot.Builder.FormFlow.FormDialog`1.<MessageReceived>d__14.MoveNext()
   --- End of inner exception stack trace ---

We think this probably happens because the FormFlow code is trying to access the value of Database but the list of buttons changed and now this value no longer exists in the list. This is the method we use to define the Database buttons:

    private static Task<bool> DefineDatabaseOptions<T>(T state, Field<T> field) where T : SupportRequestFormBase
    {
        var databaseNames = state.GetDatabaseNames(state.ClusterName).Where(x => !state.m_databasesToFilter.Contains(x)).ToList();
        if (databaseNames.SafeFastAny() && databaseNames.Count <= c_maxDatabasesToShow)
        {
            foreach (var database in databaseNames)
            {
                field.AddDescription(value: database, description: database).AddTerms(value: database, terms: database);
            }
            field.SetType(null);
            return Task.FromResult(true);
        }

        field.SetType(typeof(string));
        return Task.FromResult(true);

}

Is there a way to force the Database field to be called again after the Cluster field is changed? Maybe this can help us avoid this exception.

Example Code

The complete bot code that reproduces the issue.

private static Task<bool> DefineDatabaseOptions<T>(T state, Field<T> field) where T : SupportRequestFormBase
        {
            var databaseNames = state.GetDatabaseNames(state.ClusterName).Where(x => !state.m_databasesToFilter.Contains(x)).ToList();
            if (databaseNames.SafeFastAny() && databaseNames.Count <= c_maxDatabasesToShow)
            {
                foreach (var database in databaseNames)
                {
                    field.AddDescription(value: database, description: database).AddTerms(value: database, terms: database);
                }
                field.SetType(null);
                return Task.FromResult(true);
            }

            field.SetType(typeof(string));
            return Task.FromResult(true);
}

Steps to Reproduce

1. 2. 3.

Expected Behavior

What you expected to happen.

Actual Results

What actually happened. Please give examples and support it with screenshots, copied output or error messages.

radennis avatar May 25 '17 09:05 radennis

@radennis are you using .NET or Node SDK?

nwhitmont avatar May 25 '17 16:05 nwhitmont

.Net

radennis avatar May 25 '17 16:05 radennis

Any update on this issue?

radennis avatar Jun 12 '17 12:06 radennis

please let us know if you are still experiencing this issue.

JasonSowers avatar Oct 05 '17 19:10 JasonSowers