I have code that GETs records from APP 36 (called Payers) where the "'Add_for_New_Practitioner" field equals "Yes".
Then I GET records from APP 12 (Practitioners) where the "new flag" is equal to "Yes".
Then, I POST one record to APP 31 (Practitioners Payers) for each combination of payers and practitioners. (For example, if I have 2 new practitioners and 3 payers where the "'Add_for_New_Practitioner" field equals "Yes", I get 3 payer records for each practitioner for a total of 6 "Practitioners Payers" records.)
This part works great. However, the next step is to create a task record for each of the newly created "Practitioners Payers" records. I cannot get the task records to create because, I assume, the "Practitioners Payers" records aren't physically saved yet when the code executes to read them.
I am seeking help with the code that will save the "Practitioners Payers" records before I attempt to GET them for the next step. Below is my current code. Any guidance will be greatly appreciated.
(function() {
'use strict';
// create button
let SPACE = 'Add_All_Payers_Btn'; // element ID of Blank Space field
kintone.events.on(['app.record.create.show', 'app.record.edit.show'], function(event) {
let btn = document.createElement('button');
btn.textContent = 'Add All Payers';
kintone.app.record.getSpaceElement(SPACE).appendChild(btn);
// on click, confirm that user truly wants to create the practitioner payer records
// if yes, then execute the code to create all practitioner payer records and task records
btn.onclick = function() {
let userInput = prompt('All payers and portals will be added for all newly imported practitioners. Do you want to continue? (Yes or No)');
if (userInput.toLowerCase() === 'yes') {
console.log('User confirmed action.');
triggerSave().then(() => {
fetchAndProcessRecords().then(() => {
setTimeout(() => {
window.location.reload(); // Refresh page after updates
}, 1000); // Delay to ensure updates are processed by the server
});
});
// write a task record for each payer/portal record that was just created
console.log("Next Up: write task records");
writeTaskRecords().then(() => {
setTimeout(() => {
window.location.reload(); // Refresh page after updates
}, 1000); // Delay to ensure updates are processed by the server
});
}
};
return event;
});
function fetchAndProcessRecords() {
// Get all records from LV Payer/Portal app where the Payer/Portal is flagged to add for newly imported practitioners
return fetchRecords(36, 'Add_for_New_Practitioner in ("Yes") order by $id asc limit 100', ['$id', 'Payer_Portal_Name'])
.then(payersPortals => {
if (payersPortals.length === 0) {
console.log('No payers or portals to process.');
return; // Stop further processing if no practitioners are found
}
console.log('Payer/Portal Records:', payersPortals);
console.log("Next up: displaying each value in the array of payers.");
let arrayLength = payersPortals.length;
console.log("Length is: " + arrayLength);
for (let i = 0; i < arrayLength; i++) {
console.log("Value " + i + " is: " + payersPortals[i].Payer_Portal_Name.value);
}
// Get all records from Practitioner View app where the New Flag = Yes
return fetchRecords(12, 'New_Flag_Text in ("Yes") order by $id asc limit 100', ['$id', 'practitioner_npi'])
.then(practitioners => {
if (practitioners.length === 0) {
console.log('No new practitioners to process.');
return; // Stop further processing if no practitioners are found
}
console.log('New Practitioners Records:', practitioners);
console.log("Next up: displaying each value in the array of practitioners.");
let arrayLength2 = practitioners.length;
for (let p = 0; p < arrayLength2; p++) {
console.log("Value " + p + " is: " + practitioners[p].practitioner_npi.value);
for (let i = 0; i < arrayLength; i++) {
var body = {
'app': 31, // 31 is practitioners payers
'record': {
'Practitioner_NPI': {
'value': practitioners[p].practitioner_npi.value
},
'Payer_or_Portal_Name': {
'value': payersPortals[i].Payer_Portal_Name.value
},
'New_Flag': {
'value': 'Yes'
},
'Status_Enrollment': {
'value': 'New/Not Started'
}
}
};
// Call the function to write the Practitioner Payer/Portal record
writeRecord(body);
}
}
});
});
}
function writeTaskRecords() {
// ***** Read all new practitioner payer/portal records and create associated task records
// Get all records from Practitioner Payer/Portal app where the New Flag = Yes
return fetchRecords(31, 'New_Flag_Text in ("Yes") order by $id asc limit 500', ['$id', 'Practitioner_NPI', 'Payer_Portal_Name_Text', 'Record_number'])
.then(practitionerPP => {
if (practitionerPP.length === 0) {
console.log('No new practitioner payer/portal records to process.');
return; // Stop further processing if no practitioners are found
}
let arrayLength3 = practitionerPP.length;
for (let n = 0; n < arrayLength3; n++) {
var body = {
'app': 11, // 11 is the Task app
'record': {
'Practitioner_NPI': {
'value': practitionerPP[n].Practitioner_NPI.value
},
'Task_Payer_Portal': {
'value': practitionerPP[n].Payer_Portal_Name_Text.value
},
'Linked_PayerPortal_RecID': {
'value': practitionerPP[n].Record_number.value
},
'Task_Association': {
'value': 'Payer/Portal'
},
'task_category': {
'value': 'New Payer/Portal Enrollment'
}
}
};
// Call the function to write the Practitioner Payer/Portal record
writeRecord(body);
}
});
// End of the function to write the task records
}
function writeRecord(body) {
kintone.api(kintone.api.url('/k/v1/record.json', true), 'POST', body, function(resp) {
// success
console.log(resp);
}, function(error) {
// error
console.log(error);
});
}
function fetchRecords(appId, query, fields) {
// Construct the parameters object
const params = {
app: appId,
query: query,
fields: fields
};
// Make the API call correctly with the API endpoint, method, and parameters
return kintone.api(kintone.api.url('/k/v1/records.json', true), 'GET', params)
.then(response => {
if (response.records) {
return response.records;
} else {
throw new Error('No records returned from the API.');
}
})
.catch(error => {
console.error('API call failed:', error);
throw error; // Rethrow to ensure it's caught by the caller
});
}
function triggerSave() {
return new Promise(resolve => {
var saveButton = document.querySelector('.gaia-ui-actionmenu-save');
if (saveButton) {
saveButton.click();
setTimeout(() => {
resolve(); // Assume save is complete after a delay
}, 1000); // Adjust delay as needed based on testing
} else {
resolve(); // Resolve immediately if save button isn't found (e.g., view mode)
}
});
}
})();