Santhosh - Thursday, April 7, 2011 9:00 AM:
Hi Community,
I'm facing with below given error when trying to update item in "onAfterUpdate" server event.
"Item has already been added. Key in dictionary: 'B5BA5891925A4E289D6059EC05DED3DF,Update' Key being added: 'B5BA5891925A4E289D6059EC05DED3DF,Update"
could somebody please help me what could be the reason behind this error message.
Thanks
Santhosh
Brian - Thursday, April 7, 2011 5:38 PM:
Hi Santhosh,
Please put a bit more explanation in all your posts about what you are trying to achieve.
This post does not really give enough information for anyone trying to answer unless they guess at what you are tyring to do.
I will guess that you are trying to change some of the values of properties after the item has been saved.
Have a look at the Programmers Guide which will explain about the onBeforeUpdate and onAfterUpdate as well as the other events you can trigger off.
When you save an item the system calls any methods that are triggered by "onBeforeUpdate" then calls the standard "onUpdate" method and then calls the "onAfterUpdate". If you want to change the values that are being saved in the item you do it in the "onBeforeUpdate". This is the information (DOM) as it is going to the server. When you are in the "onAfterUpdate" method you are dealing with the response from the server. So the information is coming back from the server after the save.
I suspect that if you triggered your method off "onBeforeUpdate" it would work the way you want but you need to explain what you are trying to do.
Cheers,
Brian.
Santhosh - Friday, April 8, 2011 1:12 AM:
Hi Brian,
Here is what I'm trying to do.
I have a item whenever the item is updated, I have to append the "generation" of the item to its name.
For Example:
If the Item name is "ABC_1" and "generation" is 1. After making some changes and updating the item, "generation" of the item would be 2.
Here I have to update the property name of the item as "ABC_2".
For achieving this I have written a server method and I'm triggering that server method in "onAfterUpdate" event of the item.
When I click on "Save" button of the item, this particular error is popped up. I'm not able to understand where I'm going wrong.
Hope this explains what I'm trying to do.
Thanks
Santhosh
Brian - Friday, April 8, 2011 2:18 AM:
Hi Santhosh,
It seems like an odd thing to be doing since you can see the generation whenever you click on the item in the Properties.
Anyway. I suspect that your server code calls ..apply() after you have changed the item details.
I'm thinking that since the item has not yet finished the normal "save" process it is trying to add the same item again at the same generation etc and it isn't allowed to do that.
There are two things I can think of to do.
1) In an onBeforeUpdate method retrieve the generation (this.getProperty("generation")) and add one to it on the assumption that the save will work properly then append this value to the "name" property of the Item. This way what you are saving will actually be the corrected name in the first place.
2) Use an onAfterGet method to append the new generation to the name that is being displayed to the user but don't actually store the name with the generation.
It depends on why you are doing this in the first place and whether you actually need to generation to be stored as part of the name. If not then I would do the second. If yes then go with the first option. If the save fails for some reason and you have used the first method you haven't changed anything anyway so it should all be OK.
Cheers,
Brian.
Santhosh - Friday, April 8, 2011 3:09 AM:
yeah Brian, I agree it is a odd thing to do but I have to to do it.
Even in "onBeforeUpdate" should I call "apply()" after I have changed the name?
Santhosh
Santhosh - Friday, April 8, 2011 7:58 AM:
Finally I got the solution........
Thanks Brian for your time and help
Thanks
Santhosh