API Bug: Service.AddButtonMapping does not always return the new ButtonMapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
It appears that the result of Service.AddButtonMapping (stored in DBOperationResult.ReturnObject) is not always the newly created button mapping. I encountered this when trying to add button mappings programmatically. If one adds multiple identical button mappings to a service, the result is always the first button mapping that was added, even though it does add all the button mappings.
Partial code that should produce this result:
service = Database.Services[0]; // get a service
string buttonCode = "AButtonCode";
bool isEnabled = true;
alternateCodes = "45";
for (int i = 0; i < 4; i++)
{
DBOperationResult serviceResult = service.AddButtonMapping(buttonCode, isEnabled);
if (serviceResult.Status.Equals(ResultStatus.Succeeded))
{
ButtonMapping newButton = serviceResult.ReturnObject as ButtonMapping;
DBOperationResult buttonResult = newButton.SetAlternateCodes(alternateCodes);
}
}
And the result from running it, seen in multiple button mappings (I did the same thing with some other button codes also):
You can see that only the first button mapping has the alternate codes, which illustrates how the API is returning only the first button mapping. I have tried both identical alternate codes and different ones. The code above only shows the same alternate code for the sake of simplicity, but the same results happen regardless.
The GUI works properly, and allows one to add the same button code multiple times:
I believe this is a weakness of button mappings not having an unique identifier. It looks like CADmep stores in DBOperationResult.ReturnObject the first button mapping it finds that matches the given ButtonCode after Service.AddButtonMapping.
It is possible to get around this by iterating through the Service.ButtonMappings for the last entry and using that object for further operations, but that is just a workaround. Please help me either confirm this bug or set me straight in my understanding.