KemalS

Set field value through Javascript

Discussion created by KemalS on Sep 6, 2017
Latest reply on Sep 7, 2017 by Matthias_Walter

Hi, I'm wondering if it's possible to set a field's value through Javascript?

I have the following code

 

´´´ javascript
var rf = [[@Page.GetQueryString("RootFolder")]];
function get_currentFolder(OnSuccess,OnError){
var context = SP.ClientContext.get_current();
var web = context.get_web();
var currentFolder = web.getFolderByServerRelativeUrl(rf);
var fields = currentFolder.get_listItemAllFields();
context.load(fields);
context.executeQueryAsync(
function() {
OnSuccess(fields);
},
OnError
);
}

get_currentFolder(function(fields){
console.log("success");

var id = fields.get_fieldValues().Company_x0020_Name.get_lookupId();
[[Company_x0020_Name]] = id;
},function(sender,args){
console.log(args.get_message());
});

´´´

The above code does not update the field. It does work when I manually set the select value:

´´´ javascript

   var s = document.querySelectorAll('[fieldname="Company_x0020_Name"]')[1].getElementsByTagName("select")[0];

   s.value = id;

´´´

But this method is not a good way because Control Status is not updated because it I assume it still thinks the value is empty.

 

Any help is appreciated.

Kind Regars

Kemal

Outcomes