Page 1 of 1

Trouble Retrieving data

Posted: 10 Jul 2019
by cwalter2
I have a list with a cross site lookup field looking to another list cross site look up field. My plan is to use the information in Forms Designer with an on change event. Currently it is bringing in an object as undefined. What is the problem with my code?

Here is the cross site look up that I am having a problem with (I bolded the problem item):
function (term, page) {
if (!term || term.length == 0) {
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager&$orderby={LookupField} asc&$filter=PLStatus eq 'Active Project'&$top=30";
}
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager&$orderby={LookupField}&$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and PLStatus eq 'Active Project'&$top=30";
}

Here is the cross site lookup on the target:
function (term, page) {
var HRCompany = "Our Company Name";
if (!term || term.length == 0) {
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby=Created desc&$filter=CLCompanyCopy eq '"+ encodeURIComponent(HRCompany) + "'&$top=10";
}
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField}&$orderby={LookupField}&$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and CLCompanyCopy eq '"+ encodeURIComponent(HRCompany) + "'&$top=10";
}

Re: Trouble Retrieving data

Posted: 10 Jul 2019
by Nikita Kurguzov
Dear Walter,
Can you try the following code?

Code: Select all

function (term, page) {
if (!term || term.length == 0) {
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager/Id&$orderby={LookupField} asc&$filter=PLStatus eq 'Active Project'&$top=30";
}
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager/Id&$orderby={LookupField}&$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and PLStatus eq 'Active Project'&$top=30";
}
Also, make sure the Internal Name of the field you are trying to retrieve is PLProjManager and it's a Lookup/Person.

Re: Trouble Retrieving data

Posted: 10 Jul 2019
by cwalter2
I made the requested changes.

Request Items:

Code: Select all

function (term, page) {
if (!term || term.length == 0) {
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager/Id&$orderby={LookupField} asc&$filter=PLStatus eq 'Active Project'&$top=30";
}
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Id&$expand=PLProjManager/Id&$orderby={LookupField}&$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and PLStatus eq 'Active Project'&$top=30";
}
To get a picture of what was being returned I made the item format:

Code: Select all

function(item) {
  return '<span class="csl-option">' + item["{LookupField}"] + " - " + item.Title + " " + item.PLProjManager.Id + '</span>'
}
Here is what is looks like:
It returns undefined in the place for item.PLProjManager.Id

Re: Trouble Retrieving data

Posted: 12 Jul 2019
by mnikitina
Dear Walter,

Could you please clarify if the Manager filed is the people picker field or the lookup field.

Thank you!

Re: Trouble Retrieving data

Posted: 13 Jul 2019
by cwalter2
It is a look up field. I was mistaken earlier. It is returning a number. Which I assume may be the Id on the original list in lieu of the Display Name field I created.

Re: Trouble Retrieving data

Posted: 16 Jul 2019
by mnikitina
Walter,

Please use the below code in Lookup settings.
To get the Title of the lookup field I've replaced Id with Title.

Code: Select all

function (term, page) {
if (!term || term.length == 0) {
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Title&$expand=PLProjManager/Id&$orderby={LookupField} asc&$filter=PLStatus eq 'Active Project'&$top=30";
}
return "{WebUrl}/_api/web/lists('{ListId}')/items?$select=Id,{LookupField},PLStatus,Title,PLProjManager/Title&$expand=PLProjManager/Id&$orderby={LookupField}&$filter=startswith({LookupField}, '" + encodeURIComponent(term) + "') and PLStatus eq 'Active Project'&$top=30";
}
Also, you can use this code in JavaScript editor to get the value.

Code: Select all

fd.field('{LookupFieldInternalName}').control('data')['PLProjManager'].Title;