cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Not applicable

Setting fields using jQuery

Jump to solution

I have following code and I am able to query a list and populate all the other field type (string, date/time, choice, lookup). However, I need to also copy Supervisor name from the query list and set it on the form. Am I missing anything or not taking the correct approach?

NWF$(document).ready(function () {
    var obj = NWF$('#' + vartestlookupID);
obj.change(function () {
    var clientContext = SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var oList = clientContext.get_web().get_lists().getByTitle("EmployeeData");
    var listItem = oList.getItemById(this.value);
    //clientContext.load(listItem, "FirstName", "LastName", "EmployeeName", "Title", "Department", "Location", "Classification", "Payroll");
clientContext.load(listItem);
    clientContext.executeQueryAsync(function () {
var varSupervisorID = listItem.get_item('SupervisorName').get_lookupValue();
alert((varSupervisorID));  // works and show Supervisor's Name
document.getElementById(varFirstNameID).value = listItem.get_item('FirstName');
document.getElementById(varLastNameID).value = listItem.get_item('LastName');
document.getElementById(DepartmentName).value=listItem.get_item('DepartmentName').get_lookupId();
document.getElementById(WorkLocation).value=listItem.get_item('WorkLocation').get_lookupId();
var edDate = new Date(listItem.get_item('EffectiveDate1'));
var edDateFormat = (edDate.getMonth() + "/" + edDate.getDate() + "/" + edDate.getFullYear());
document.getElementById(varEffectiveDateID).value = edDateFormat;
  // DID NOT WORK document.getElementById(SupervisorName).value=listItem.get_item('SupervisorName').get_lookupId();
// DID NOT WORK document.getElementById(SupervisorName).value=listItem.get_item('SupervisorName').get_lookupValue();
var varSupervisorName = new NF.PeoplePickerApi('#SupervisorName');
  varSupervisorName.search(varSupervisorID).done(function (data) {varSupervisorName.add(data[0]);});
//varSupervisorName.search(listItem.get_item('SupervisorName').get_lookupId()).done function(data){varSupervisorName.add(data[0]);
//document.getElementById(SupervisorName).value = varSupervisorName.search('fmi\\mahmed').donefunction (data) {varSupervisorName.add(data[0]);});
}, function () {        });
    });
});

Labels: (2)
Tags (1)
0 Kudos
Reply
10 Replies
Highlighted
Automation Master
Automation Master

Re: Set person field on the form based on quering person field from a list

Jump to solution

Hi Moe,

does your browsers developer tool show any errors?

Additionally, what is SupervisorName in your "DID NOT WORK" lines? Is that a variable I just can't find or am I missing something? If that is the actual ID, there should be quotation marks. And what kind of control is the Element with ID SupervisorName?

Regards

Philipp

0 Kudos
Reply
Highlighted
Not applicable

Re: Setting fields using jQuery

Jump to solution

Developer tool isn't showing any error.

The commented with "DID NOT WORK is a trial as I was reading somewhere lookupId or lookupValue should do it but this method does not work for Person field.

The element id SupervisorName is Person/Group type field.

0 Kudos
Reply
Highlighted
Automation Master
Automation Master

Re: Setting fields using jQuery

Jump to solution

Okay but the line

listItem.get_item('SupervisorName').get_lookupValue();

is working so you get the value but are unable to set this value to the person/group field on your form.

See this Blog-Post on further information on how to set people picker values correctly with javascript: Javascript and Working with the SharePoint 2013 People Picker

Could help you

0 Kudos
Reply
Highlighted
Not applicable

Re: Setting fields using jQuery

Jump to solution

Phillip: you are correct I am able to get the display name of the person field using listItem.get_item('SupervisorName').get_lookupValue(); but the form control would not take this and resolve. I will look at the link you posted. Thanks

0 Kudos
Reply
Highlighted
Automation Master
Automation Master

Re: Setting fields using jQuery

Jump to solution

People/User fields can be really frustrating as they are "special" in many different ways =\

0 Kudos
Reply
Highlighted
Not applicable

Re: Setting fields using jQuery

Jump to solution

I agree but Nintex decided to change the Person control and they wont provide any support. Using the legacy People Picker control with Nintex Forms .

0 Kudos
Reply
Highlighted
Nintex Newbie

Re: Setting fields using jQuery

Jump to solution

Are You using Nintex Forms? Then look at People Picker Extensions - Nintex Forms and Nintex Forms Managers name into people picker for People Picker JavaScript API.

View solution in original post

0 Kudos
Reply
Highlighted
Not applicable

Re: Setting fields using jQuery

Jump to solution

Manfred: I tried the code but is not working. I even hardcoded the value domain\\account. Do I need to install the extension? If yes, they don't have any instruction on installing the extension dll.

0 Kudos
Reply
Highlighted
Not applicable

Re: Setting fields using jQuery

Jump to solution

I finally got it to work. It turns out the code posted on the article is WRONG. I change it to following and now it's working.

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

in my case

var varSupervisorID = listItem.get_item('SupervisorName').get_lookupValue();

var varSupervisorName = new NF.PeoplePickerApi('#' + SupervisorName);

  varSupervisorName.search(varSupervisorID).done(function (data) {

  varSupervisorName.add(data[0]);

  });

Anyone else reading be aware it took form 20 seconds to populate or resolve the name. I don't know why but all the other fields are populated right away. For now I can work with latency.

0 Kudos
Reply