Setting Dropdown to first value after returning results from SP

Hello,

I'm using the code below to retrieve data from a SP list and display it on a dropdown field. How do I make it so that after the values are retrieved from SP, the first value display on the dropdown (index 0)? I tried the code fd.field('DropDown1').widget.select(0); but it is not working. It works on a regular dropdown but not on a dropdown whose value are retrieved from SP.

Also, how can I change it so that if no value is found, it displays a particular text instead of "No Data Found". I need to be able to differentiate if the result is blank or not and act accordingly.

fd.rendered(function() {

fd.field('DropDown1').widget.setOptions({dataTextField : 'text', dataValueField: 'value'});

var widget3 = fd.field('DropDown1').widget;
widget3.text('');
widget3.setDataSource({
transport: {
read: 'YYYYYYYYY==&info=5523EI39',
// success: alert('hi')
}
});
widget3.setOptions({dataTextField: 'fields.Title', dataValueField: 'fields.Title'});

Validator()

function Validator(){

fd.field('DropDown1').widget.select(0);

}

Also, how can I modify the code above so that it pushes the value to a text field instead of a dropdown? Also if the returned value is blank I would like to push my own value to the textbox, not just display blank. Please assist.

Hello @adasilva,

You can retrieve SharePoint list values, check if any value is retrieved, and populate the dropdown or single-line fields with the title of the first item using this code:

var url ='Function URL';

var req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.onload  = function() {
   var jsonResponse = req.response;
   //check if items are retrieved 
   if(jsonResponse.length !=0) {
       fd.field('SingleLineText0').value = jsonResponse[0].fields.Title;
   }
   else{
       //do something else
   }
};
req.send(null);
1 Like