Set field value through Javascript
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
-
Hi Kemal Sinanagic,
can you please answer two question for me to better understand your challenge?
- when should hit field (Company Name) be set? -> on form load, on form save or on other field change
- what exactly do you wanna set? Is it a lookup field or a choice field? where do you want to get the value from?
Regards
Matthias
0 -
Hi Matthias,
The field should be set on form load.
It's a lookup field. I'm getting the value from the metadata of the parent folder the item is located in, which has the same lookupfield.
Thanks in advance
Kemal
0 -
Hi Kemal Sinanagic,
I found an easier way using one of our functions to get the lookup value set.
Set the following initial value expression on the lookup field:
[[@Web.GetFirstValueForQuery('Folderlist', '<Where><BeginsWith><FieldRef Name=ServerUrl /><Value Type=Text>[[@Page.GetQueryString("RootFolder")]]</Value></BeginsWith></Where>', 'lookup')]].get_lookupId()+";#"
Simply replace the parameters in bold with your list and field name and make sure the expression type is set to Assignment.
Cheers
Matthias
0 -
Hey Matthias
I'm getting the following error:
FolderList = Claims
lookup = Company_x0020_Name
Thanks in advance
Kemal
0 -
When I replace the "[[@Page.GetQueryString("RootFolder")]]" with my own value like "/sites/GY/Lists/Claims/FolderName" then it works. Maybe the parser is messing something up here?
So this works below (but not a solution):
=[[@Web.GetFirstValueForQuery('Claims', '<Where><BeginsWith><FieldRef Name=ServerUrl /><Value Type=Text>/sites/GY/Lists/Claims/Folder</Value></BeginsWith></Where>', 'Company_x0020_Name')]].get_lookupId()+";#"
Thanks in advance
Kemal
0 -
This works now:
[[@Web.GetFirstValueForQuery('Claims', '<Where><BeginsWith><FieldRef Name=ServerUrl /><Value Type=Text>'+[[@Page.GetQueryString("RootFolder")]]+'</Value></BeginsWith></Where>', 'Company_x0020_Name')]].get_lookupId()+";#"
Thanks for the help Matthias.
One more question: it's not possible to set a field value through javascript? Should I need it in the future.
0 -
Good to hear, but strange that it was working for me..
Regarding the question "how to set a field value trough javascript?"; it depends on how you get the value to be set.
- If you can get it within the context objects of the expression builder using our field placeholders or functions, you can set them as initial or calculated value or with a "Set field value" action.
- If you need to get them asynchronous, you need to write them to the html controls manually in the onsuccess method and this is different from field type to field type.
BR Matthias
0
Please sign in to leave a comment.
Comments
7 comments