0

I have a list .

I want update special item of it with javascript.

I created a js file and add this code to it :

 var context = new SP.ClientContext.get_current();
 var web = context.get_web();
 var list = web.get_lists().getByTitle("Sends");
 var salesItem = list.getItemById(90);
 salesItem.set_item("Status", "Closed");
 salesItem.update();

It is work for text field but don't work for Choice field.

How do update a item with javascript?

3 Answers 3

1

Hi for update list item using ECMA script you have to use SP.js

in your code i don't see SP.js reference

use this code i am update task list status column which is choice field

ExecuteOrDelayUntilScriptLoaded(updateProduct, "SP.js"); 
function updateProduct() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("Tasks");
var product = list.getItemById(1);
product.set_item("Status", "Completed");
product.update();
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success()
{
alert('Done');
}
function failed()
{
 alert('problem');
}
5
  • my code is like this. for example:When i use my code for 'Title' it is work, but when i use for choice filed don't work.
    – ar.gorgin
    Aug 28, 2012 at 5:43
  • dear i update my choice field... U see it's status field of task list ....Please make sure u enter value of choice is in list otherwise it not accept..Thanks Aug 28, 2012 at 5:46
  • also u see the alert??? "Done" or "Problem"?? Aug 28, 2012 at 5:47
  • Yes, 'Closed' is a item of choice field. When i run, get "Done".
    – ar.gorgin
    Aug 28, 2012 at 5:57
  • I delete column then add again, now when i update a item it set defualt value for chioce column :(
    – ar.gorgin
    Aug 28, 2012 at 7:29
0

Have you verified the Internal name of the Status field is "Status". I think the Internal Field Name is specified wrong in the code.

1
  • No, i use Internal Name.
    – ar.gorgin
    Aug 28, 2012 at 5:58
0

Please try these things:

  1. Validate Internal Name of the column that is used
  2. Validate the choice values of the column. sometimes there might be spaces before or after the values.
  3. Use a success function and check the error message that is returned after the async query.

Hope this helps you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.