Adding Project Variable - Bad Request

Hi,
I am trying to add a project variable to a project using a Powershell script but when try running it I get an error 400: Bad Request. Using Fiddler shows the error: “Changes to these variables could not be saved, because another user has made changes to the variables between when you started editing and when you saved your changes. Please reload or open a new tab to make your changes.”

I then closed all my windows and opened them back up again but the same error occurred again. One other person is accessing the project I am working on but I had him try running the script and closing everything but he got the same error. I then tried to add a project variable through the website and it worked. This lead me to believe that my script is wrong but I am not sure what is wrong with it. Could you take a look? I added a screen shot and text file of the script.

Thanks!

Script.txt (460 Bytes)

Hi Matthew,
It appears as though you have a hard-coded, hand-crafter variables object that you are trying to post back as an update. By using this process rather than retrieving the original values, modifying, then posting back you are missing a few very important parts of the entity.

First you are risking removing values that have been entered via other mechanisms (though admittedly maybe this is intended). More importantly, you are omitting the crucial Version property. A simple look at the network traffic for a normal request through the portal will show you what a typical VariableSet entity looks like. I have attached a screenshot as an example.

The Version property is used so that we can ensure that if one user hits save after another user has already updated the variables, then your project doesn’t encounter some problematic concurrency issues. Ideally it is recommended that you first load the existing variable values, modify the entity, then commit the new value. This ensures that your data maintains consistency.

Given you are using PowerShell, I would highly recommend loading the Octopus.Clients dll then you can use some helpful classes to make life a lot easier.
Here is an example of modifying some project variables through PowerShell using this library.

Hopefully this all puts you back on the right track. Let me know if you have any further questions or difficulties.
Cheers,
Rob

Hi Rob,
Thank you for the quick reply.
I am trying to avoid using Octopus.Clients dll for now so I can practice doing API calls. I am also trying to hard code the variable the first time I write the script, then make it load and modify the variables later on.

I have added the Version property and that fixed the error I had before. I did hardcode the Version property but I am going to change that once I get it to work once. I am now getting a new error: “Object reference not set to an instance of an object.” I tried setting my variable and scope to objects but that did not fix it. Could you take another look at it?

Thanks again!

Scriptv2.txt (812 Bytes)

Hello again,
After some messing around, I figured out what was wrong with my script. I was not including the scope values in the variable set I created which caused it to error. I then tried to set the scope values to an empty object but that caused it to error again saying that the environmental scopes have changed.

I fixed it by doing what you said to do, which was to get the variable set from octopus and modify it. I am adding a picture incase someone else needs it.

Thank you!

Matthew,
Glad to hear that you got this sorted in the end. Let me know if you run into any further problems.
Cheers,
Rob