CSVImportController
CSVImportController
if (String.isNotBlank(accessToken)) {
System.debug('Fetching CSV data from Google Drive...');
GoogleDriveIntegration googleDrive = new GoogleDriveIntegration();
String csvData = googleDrive.fetchCSVData(googleDriveFileId,
accessToken);
if (String.isNotBlank(csvData)) {
List<Account> recordsToInsert = new List<Account>();
List<String> csvLines = csvData.split('\n');
// Create a copy of the original CSV file and move the copy to
the new folder
String copiedFileId =
googleDrive.copyFileAndMove(googleDriveFileId, '1fTFS5sGYzdsT3r0V53A__ywY8fK3AWNS',
accessToken);
if (copiedFileId != null) {
System.debug('Created a copy of the original CSV file and
moved it to the new folder.');
} else {
//System.debug('Failed to create a copy of the original CSV
file or move it to the new folder.');
}
if (moveRes.getStatusCode() == 200) {
System.debug('Moved ' + fileName + ' to folder ' +
folderId);
if (mainFileMoveResult.equals('success')) {
System.debug('Moved the main CSV file to the new
folder.');
} else {
System.debug('Failed to move the main CSV file to the
new folder.');
}
} else {
System.debug('Error moving ' + fileName + ' to folder ' +
folderId);
System.debug('Response: ' + moveRes.getBody());
}
} else {
// Handle the error response
System.debug('Error creating ' + fileName + ' in Google
Drive');
System.debug('Response: ' + res.getBody());
}
} catch (Exception e) {
// Handle the exception
System.debug('Exception while creating ' + fileName + ' in Google
Drive: ' + e.getMessage());
}
}
// Fetch parent folders of a file
public String getParentFolders(String fileId, String accessToken) {
try {
HttpRequest req = new HttpRequest();
req.setMethod('GET');
String endpoint = 'https://www.googleapis.com/drive/v3/files/' +
fileId + '?fields=parents';
req.setEndpoint(endpoint);
req.setHeader('Authorization', 'Bearer ' + accessToken);
if (res.getStatusCode() == 200) {
System.debug('Moved file ' + fileId + ' to folder ' +
newFolderId);
return 'success';
} else {
System.debug('Error moving file ' + fileId + ' to folder ' +
newFolderId);
System.debug('Response: ' + res.getBody());
return 'error';
}
} catch (Exception e) {
// Handle the exception
System.debug('Exception while moving file ' + fileId + ' to folder
' + newFolderId + ': ' + e.getMessage());
return 'error';
}
}
if (res.getStatusCode() == 200) {
Map<String, Object> responseMap = (Map<String, Object>)
JSON.deserializeUntyped(res.getBody());
String copiedFileId = (String) responseMap.get('id');
if (moveResult == 'success') {
System.debug('Copied and moved the file ' + fileId + ' to
folder ' + newFolderId);
return copiedFileId;
} else {
System.debug('Failed to move the copied file to folder ' +
newFolderId);
return null;
}
} else {
System.debug('Error copying file ' + fileId);
//System.debug('Response: ' + res.getBody());
return null;
}
} catch (Exception e) {
// Handle the exception
System.debug('Exception while copying and moving file ' + fileId +
' to folder ' + newFolderId + ': ' + e.getMessage());
return null;
}
}
if (moveRes.getStatusCode() == 200) {
System.debug('Moved ' + fileName + ' to folder ' +
folderId);
} else {
System.debug('Error moving ' + fileName + ' to folder ' +
folderId);
System.debug('Response: ' + moveRes.getBody());
}
} else {
// Handle the error response
System.debug('Error creating ' + fileName + ' in Google
Drive');
System.debug('Response: ' + res.getBody());
}
} catch (Exception e) {
// Handle the exception
System.debug('Exception while creating ' + fileName + ' in Google
Drive: ' + e.getMessage());
}
}
}
}