How to get a Record ID from a Lookup field?

Hello, everyone.
I want to get the Record ID from a Lookup field.

As you know, we can get the App ID of the Datasource app of the specified Lookup field with the Get Lookup Target JavaScript API.

kintone.app.getLookupTargetAppId(fieldCode)

But I do not know how to get the Record ID of the Datasource app of the Lookup field.

Hello @unudeveloper

You just need to:

  1. Obtain the key field of a Lookup field from the event object
  2. Include it in the REST API(GET) query to obtain the target record.

You can directly get the ID from the event object if you’ve set a record number as the key field.

Hello @Chris ,

Thanks for your help.

You can directly get the ID from the event object if you've set a record number as the key field.

Yes, I can.
However, I have set the "title" field (not the Record number) as the Lookup field's key field.

Let me ask a question.

Question 1: Getting the second record

How do I ensure I get the correct record if there are two records with the same title (in the title field)?
What if the query results in the first record, but I want the second record?

How can I solve this?

Also, I have another question.

Question 2: Why am I unable to get the Record Number directly?

By hovering over the Lookup field from the "Lookup Picker" window, I can see that text is hyperlinked to the record, which includes the Record Number (as shown below).

Screenshot of a Lookup Picker window and the record 233 is being hovered over by the mouse



But then why am I unable to get the Record Number directly with something like this?

kintone.app.record.get().record["lookup"].xxx

:zap: Related API Docs:

Hello @unudeveloper

You will need to include the data obtained thru the Lookup field as a key to specify which record.

And about the 2nd question, I’m not quite sure what the problem is, but I assume your question is why the record number differs from the URL in the lookup field. Please correct me if I’m wrong.

“233” is the record number of that record in the app.
The record number you see in the URL is the record number of the record that the lookup field references.
In other words, they are completely different records.

Dear @Chris ,

Question 2 Response

Sorry, I was missing a sentence in the second question.

I can get the Record ID from the "Lookup Picker" window.

But then why am I unable to get the Record Number directly with something like this?

kintone.app.record.get().record["lookup"].xxx

Question 1 Response

Sorry, I do not quite understand.

Could you please elaborate?

Thank you.

Hi @unudeveloper,

Let’s say; for example, there is a field that can be duplicated; on my test app, I used the lookup field to lookup a product name and also a text field that displays a size.
When trying to look up, the same product name is displayed.

It would be impractical to determine which records to look up based solely on the duplicable fields.

Therefore, if the field is duplicable, the “size” (information obtained in the lookup) can also be included in the query when retrieving records to identify the record.

I hope this explains what Chris mentioned previously.

1 Like

Hi @Sean_Tcbn
Thank you . I understand what you mean.
But I just wanted to get the record id like this.
kintone.app.record.get().record["lookup"].xxx
If it is possible, could please explain about why I can’t get the record id like getting the App Id.
Regards.

Hello @unudeveloper
This is impossible because this API does not obtain the whole data of the other record grabbed by a lookup field. It only grabs data that is specified in the lookup field settings.
You can see this by looking at the response of the API:

var record = kintone.app.record.get();
console.log(record);

And there’s no API like App ID that precisely does it as of now.

I hope this answers your question.

Hi @Chris
Thanks for your response.
I also tried to get the record id using kintone.app.record.get() but unfortunately it returned
"Lookup": { "type": "SINGLE_LINE_TEXT", "value": "Document Title" }.
It is same as Text field.

"Text": {
    "type": "SINGLE_LINE_TEXT",
    "value": "sample"
  },

It will be great , developers can get record id using kintone.app.record.get() .
I want your opinion.
Best Regards.