Bit of background: I’m not really a programmer, but I’m teaching myself .js for use with Kintone and can do most basic things by adjusting provided samples to do what I need them to.
That being said, I’m trying to get the “Display Total Number of Related Records” script working and running into a bit of an issue. The only thing I’ve changed from the sample code is turning “sales” into “contacts”. Code below.
And yet… I’m somehow getting a count that’s… not anywhere near the actual number of related records? What am I doing wrong?
(function() {
"use strict";
var RELATEDRECORDS = "contacts";
var SPACEFIELD = "numberSpace";
kintone.events.on('app.record.detail.show', function(event) {
//Get all records related to the related records field
function fetchRecords(appId, opt_offset, opt_limit, opt_records) {
var Id = kintone.app.getRelatedRecordsTargetAppId(RELATEDRECORDS);
var offset = opt_offset || 0;
var limit = opt_limit || 100;
var allRecords = opt_records || [];
var params = {app: Id, query: 'order by $id asc limit ' + limit + ' offset ' + offset};
return kintone.api('/k/v1/records', 'GET', params).then(function(resp) {
allRecords = allRecords.concat(resp.records);
if (resp.records.length === limit) {
return fetchRecords(appId, offset + limit, limit, allRecords);
}
return allRecords;
});
}
fetchRecords(kintone.app.getId()).then(function(records) {
//Insert the total number of records into the Space field
var num = records.length;
var divTotalAmount = document.createElement('div');
//divTotalAmount.style.fontWeight = 'bold';
divTotalAmount.style.textAlign = 'center';
divTotalAmount.style.fontSize = '16px';
divTotalAmount.innerHTML = String(num) + ' related contact(s)';
kintone.app.record.getSpaceElement(SPACEFIELD).appendChild(divTotalAmount);
return event;
});
});
})();
Update; I’ve realized that 134 is the total number of records in the “Contacts” app, from which the related records field pulls information.
That being said; I’d presume that the part of the sample code that slims down the related records based on fetch criteria is either broken or nonexistent?
You are right that by following the samle code on the link, I was able to get the total number of records in my app(“Prospect & Customers” app from template “Sales CRM Pack” from kintone Marketplace), not the total number per customer name.
I found the following link that is aming the same result in Japanese.
I revised the coding a little bit as blow and it works. Hope it helps. Thank you. Junko
(function() {
'use strict';
// 関連レコードの絞り込みに利用するフィールドコード the code fields to use related records features
var FIELDNAME_A = 'CompanyName'; // the code of field of the field you are tring to retrieve at target app "Prospect & Customers"
var FIELDNAME_B = 'CustomerName'; // the code of field of the field at data source app "Sales & Leads"
var SPACEFIELD = "numberSpace";
var RELATEDRECORDS= 'relation'
kintone.events.on('app.record.detail.show', function(event) {
// 関連レコードで取得しているアプリの対象レコードを全件取得 Get all of the record on the related records
function fetchRecords(opt_Field, opt_offset, opt_limit, opt_records) {
var Id = kintone.app.getRelatedRecordsTargetAppId(RELATEDRECORDS);
var offset = opt_offset || 0;
var limit = opt_limit || 100;
var allRecords = opt_records || [];
var params = {app: Id, query: opt_Field + ' order by $id asc limit ' + limit + ' offset ' + offset};
return kintone.api('/k/v1/records', 'GET', params).then(function(resp) {
allRecords = allRecords.concat(resp.records);
if (resp.records.length === limit) {
return fetchRecords(opt_Field, offset + limit, limit, allRecords);
}
return allRecords;
});
}
// 関連レコードの「表示するレコードの条件」に合わせてクエリを作成 create the query to "Set fetch criteria" of relateds
var CompanyNameValue = event.record[FIELDNAME_A].value;
var opt_Field = FIELDNAME_B + '=' + '"' + CompanyNameValue + '"';
fetchRecords(opt_Field).then(function(records) {
// スペースフィールドにBアプリのレコード数を反映 reflect the record number to the space at target app
var num = records.length;
var divTotalAmount = document.createElement('div');
divTotalAmount.style.fontWeight = 'bold';
divTotalAmount.style.textAlign = 'center';
divTotalAmount.style.fontSize = 12;
divTotalAmount.innerHTML = num;
kintone.app.record.getSpaceElement(SPACEFIELD).appendChild(divTotalAmount);
return event;
});
});
})();
Sorry for the trouble - we realized we missed out a bunch of logic to set the Fetch Criteria settings for the Related Records field.
The article has now been updated.