Retrieving Lookup Field Source Records
Visible but out of reach.
You can display the source record screen by clicking from the record list or detail screen, but retrieving the source record from JavaScript takes some effort.
Moreover, if the field referenced by the lookup field is not unique and can change frequently, it becomes even more troublesome.
This time, we will consider how to retrieve the source record of the lookup field from JavaScript.
Proper Method
If the app on the referencing side has enough information to identify the target record, it is not that difficult.
Use the REST API to retrieve one source record and overwrite it again with the REST API.
If the source app of the lookup is unknown, you can obtain the source app ID from the API to get form settings.
// Information of the record on the referencing side
const { record } = kintone.app.record.get();
/** Source app ID */
const app = 'xxx';
// Create a query to narrow down to one target record
const query = `ID = "${record['ID'].value}"`;
// Information of the reference obtained by the REST API
const response = await kintone.api(kintone.api.url('/k/v1/records', true), 'GET', { app, query });
// Source record
const [record] = response.records;
if (!record) {
throw 'Could not identify';
}
const newRecord = { Identified: { value: 'Identified' } };
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'POST', { app, record: newRecord });
If you are directly obtaining unique field information in the lookup field, you can also retrieve
it by changing the API from /k/v1/records
to /k/v1/record
.
Not Linking Well → Might Be a Design Issue?
In the code mentioned above, if the source record has been deleted or the value of the source field has been changed, an error will occur.
There are several ways to retrieve it without changing the state of the app, but if this situation occurs frequently, there might be a design issue between the apps.
Let’s review which items to link and whether the field to be looked up is correct.
Forcibly Retrieve
Traverse the DOM
Use the getFieldElement
method from the JavaScript API to get the DOM element of the lookup field and identify the source record from the link information.
const fieldElement = kintone.app.record.getFieldElement('Lookup');
const anchor = fieldElement.querySelector('a');
const { href } = anchor;
const [_, recordId] = href.match(/.*#record=([0-9]+)/);
Use Internal Parameters
You can also use the global variable cybozu
that exists in the kintone environment. This is less recommended than the DOM mentioned above.
cybozu.data.page.FORM_DATA.lookups;
When Reviewing the Design
Copy Key Information from Lookup Settings
If you want to identify the source without changing the current specifications, this method is the simplest.
If you obtain the record number or unique key information at the time of lookup, you will not lose track of the source.
Use a Plugin
This is a promotion, but you can solve it using my developed “Unlinked Lookup” plugin.
Although it is a free plugin, I would appreciate it if you could consider it.
By using this plugin, you can hide the standard lookup field and use a user-friendly field instead as a lookup field.