INQUIRY IN BRIEF:
I am seeking to utilize Javascript to generate a folder structure while verifying the existence of any/all/none of the folders. Providing folder names "A", "B", "C", and "D", I aim to create "A" in the root directory, then establish "B" within "A", continuing this pattern. The challenge lies in the asynchronous insertion of all folders without their respective ParentIds, leading them all to be placed in the root directory.
EXPANDED VERSION AND WORK IN PROGRESS (WIP):
My goal is to simultaneously create three or more folders along the same path.
For instance, if the desired file path is State\City\Street, I begin by arranging the folders in an array according to the preferred order (note that in the final project, these will not be hardcoded but generated dynamically based on user and Salesforce record).
const filePath = ["State", "City", "Street"];
The next step involves passing this information to a method responsible for creating these folders sequentially, utilizing the preceding folder's Id as its parentId.
function createFolder() {
var parentFolderId = "root";
for (let i = 0; i < filePath.length; i++) {
var folderMetadata = {
'name' : filePath[i],
'mimeType' : 'application/vnd.google-apps.folder',
'parents': [parentFolderId]
}
gapi.client.drive.files.create({
resource: folderMetadata,
}).then(function(response) {
switch(response.status){
case 200:
var file = response.result;
parentFolderId = file.Id;
console.log(i + ' file.Id= ' + file.Id);
break;
default:
console.log('Error creating the folder, ' + response);
break;
}
});
}
}
However, due to the asynchronous nature of the process, all folders are created after the function completion, prior to the execution of the "parentFolderId = file.Id;" line, resulting in placement within the root directory.
I acknowledge that an alternative approach could involve creating all folders, querying them, and subsequently rearranging. However, the subsequent phase of my project entails dynamically identifying the presence of any/all existing folders, with potential variations in the folder structure.