Question / Problem
Kintone Application 1: Quantity and Item Fields are included.
Kintone Application 2: Quantity and Item Fields are also included inside.
If Application 1 has a quantity value of 99, I want it to be deducted by the quantity I declare in the field Quantity
of Application 2.
How can I do it?
Code / Attempts
(function() {
"use strict";
// Define the IDs and URLs for both applications
const app1Id = 760; // Application 1 ID
const app2Id = 761; // Application 2 ID
const kintoneSubdomain = ''; // Your Kintone subdomain
const app1Url = `https://kintoneSubdomain.kintone.com/k/v1/records.json`;
const app1UpdateUrl = `https://kintoneSubdomain.kintone.com/k/v1/record.json`;
// Define the field codes
const quantity1Field = 'quantity1';
const item1Field = 'item1';
const quantity2Field = 'quantity2';
const item2Field = 'item2';
// Define the API tokens
const app1ApiToken = ''; // API token for app1
const app2ApiToken = ''; // API token for app2
// Add an event listener for record creation and editing in Application 2
kintone.events.on(['app.record.create.submit', 'app.record.edit.submit'], function(event) {
const record = event.record;
const quantity2 = Number(record[quantity2Field].value);
const item2 = record[item2Field].value;
// Set headers for the API request
const headers = {
'X-Cybozu-Authorization': app1ApiToken,
'Content-Type': 'application/json'
};
// Define the query to fetch the matching record from Application 1
const query = `${item1Field}="${item2}"`;
// Fetch the record from Application 1
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'GET', {
app: app1Id,
query: query
}, { headers: headers })
.then(response => {
if (response.records.length > 0) {
const app1Record = response.records[0];
const app1Quantity = Number(app1Record[quantity1Field].value);
// Calculate the new quantity
const newQuantity = app1Quantity - quantity2;
// Prepare the payload for updating Application 1
const updatePayload = {
app: app1Id,
id: app1Record.$id.value,
record: {
[quantity1Field]: {
value: newQuantity
}
}
};
// Update the record in Application 1
return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', updatePayload, { headers: headers });
} else {
throw new Error('Item not found in Application 1');
}
})
.then(response => {
console.log('Quantity updated in Application 1:', response);
})
.catch(error => {
event.error = `Error updating Application 1: ${error.message}`;
});
});
})();