[Question] KeyNotFoundException when using dynamic buttons
Pre-flight checklist
- [x] I posted a question about this issue on Stack Overflow using the tag
botframework. - [x] I have searched the existing issues before logging a new one.
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 are you using .NET or Node SDK?
.Net
Any update on this issue?
please let us know if you are still experiencing this issue.