DropDownList push method strange behavior

Hi,
I met with strange behavior of push method.
Its result is very strange when I use push to append new items.
Having this function:

function getDeputies(approvalLevel) {
    console.log('In setDeputy');
    let person = fd.field(approvalLevel).widget.text();
    var ddlDeputies = [];
    ddlDeputies.push(person);
    let web = new Web('https://sharepoint/apps/transfers');
    web.lists.getByTitle('Deputies')
        .items.select('Manager','Manager/Title', 'Deputy', 'Deputy/Title')
        .expand('Manager', 'Deputy')
        .filter('Manager/Title eq \'' + person +'\'')
        .get().then(function (result) {
            result.forEach(function (resultItem) {
               ddlDeputies.push(resultItem.Deputy.Title);
            });
        });
    console.log(ddlDeputies);
    return ddlDeputies;
}

results in array of 0 items:
image

instead of something like this:
image

Regards

ok.
I have finally finished with code below. The array was needed to fill dropdown control so I did it.

function getDeputies(approvalLevel) {
    console.log('In setDeputy');
    let person = fd.field(approvalLevel).widget.text();
    let ddlControlName = 'ddl' + approvalLevel;
    var ddlDeputies = [];
    ddlDeputies.push(person);
    let web = new Web('https://sharepoint/apps/transfers');
    web.lists.getByTitle('Deputies')
        .items.select('Manager','Manager/Title', 'Deputy', 'Deputy/Title')
        .expand('Manager', 'Deputy')
        .filter('Manager/Title eq \'' + person +'\'')
        .get().then(function (result) {
            result.forEach(function (resultItem) {
               ddlDeputies.push(resultItem.Deputy.Title);
            });
            // FOLLOWING LINE IS GAME CHANGER
            fd.field(ddlControlName).widget.dataSource.data(ddlDeputies);
        });
}

Now I'm wonder how to set dropdown control to point to default first value from array.
Now it is empty:
image
But values are available :slight_smile: :slight_smile: And requirement is to set as default 'Lois Lane'.
image

Of course I will eventually add a validator to force user to chose one, but would rather like to show them first as default, so I can treat it as .selected and on save write this person to proper field - for Workflow.

Dear @Marcin,
All you need to do is set the value after you set the data source, like this:

// FOLLOWING LINE IS GAME CHANGER
fd.field(ddlControlName).widget.dataSource.data(ddlDeputies);
fd.field(ddlControlName).value = 'Brock Edward (something something)'

It needs to be exactly the same as the value that appears in the dropdown, then it will be selected.

Well, that's great, thanks.

Another issue related to this function.
Function like getDeputies is fired on change of Company field.


Trouble is that when control loads form and list of locations to Companies - doesn't matter if it is lookup, or choice - it initially has a value selected. Value is in fact empty. As on the picture above validator will work, but "on change" not. you can see that in this example "Location 4" is "pre" selected so when user chooses this item then 'on change' doesn't start.
Of course change to "Location 1" and then "Location 4" is effective.

How to deal with it?

Dear @Marcin,
So, what's the issue? The value is selected, but not displayed, right? So it doesn't appear, but if selected again, it doesn't trigger change, is that right?

Are you populating the field with data or is it a default SharePoint field? Why does it appear empty?

Dear Nikita,
I do apologize. It was my mistake in script. It works perfect.

Last question with regards to dynamic fields assignment.
How to make sure, that field is filled with data before we launch next function?
Suppose we read some data from other list with pnp.sp and assign
fd.field('locationDirector').value = result[0].Director.EMail;
Next we want to read it or use somewhere else with fd.field('locationDirector').widget.text() and receive:

"Uncaught (in promise) ReferenceError: text is not defined
at eval (eval at e._executeCustomJavaScript (spform.js:111:186082), :146:50)"

Regards

Currently I have solved it with "onchange" on this field and everything is working fine.
So if there is no other option we can close subject :slight_smile: .

1 Like