javascriptsharepointtypeerrorlowercasepeoplepicker

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined when adding object to field in Sharepoint


I want to fill a nintex form people picker field automatically using JavaScript on a SharePoint page.

enter image description here

The problem is that I keep getting an error:

enter image description here

When I click on the first link, it shows this

enter image description here

In the formMain.js, the error is caused by this line:

ins.add(object);

I've been trying for hours to fix this issue, but I can't find a solution. As you can see in the console, ins and the object are both defined.

Here's the JavaScript I use to add the user to the field:

var personProperties;

function onChangeProductManagement() {
    
    var curvalue = NWF$('#' + TeilnehmerStatus10).find("input:checked").val();
    var ins = new NF.PeoplePickerApi('#' + TeilnehmerName10);


    if (curvalue == "offen") {
        
        SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
            // Make sure PeopleManager is available
            SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function() {

                // Replace the placeholder value with the target user's credentials.
                var targetUser = "opmain\\eoblae";

                // Get the current client context and PeopleManager instance.
                var clientContext = new SP.ClientContext.get_current();
                var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

                // Get user properties for the target user.
                // To get the PersonProperties object for the current user, use the
                // getMyProperties method.
                personProperties = peopleManager.getPropertiesFor(targetUser);

                // Load the PersonProperties object and send the request.
                clientContext.load(personProperties);
                clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
            });
        });

    } else {
        console.log("Test2");
        console.log("NWF value: " + NWF$('#' + TeilnehmerName10).val());
    }
}


// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
    var accountName = personProperties.get_accountName();
    var displayName = personProperties.get_displayName();
    var email = personProperties.get_email();

    var object = { value: accountName, label: displayName, type: "user", email: email };
    console.log("object: ", object);

    var ins = new NF.PeoplePickerApi('#' + TeilnehmerName10);

    console.log("ins: ", ins);
    ins.add(object);
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    console.log("Error: " + args.get_message());
}

Any help is appreciated!


Solution

  • Adding the id in var object fixed the issue.