Dear community,
I am working with a Workshop dashboard in Palantir Foundry that displays Project information and includes a button to select different Project Managers. When a Project Manager is selected, I need the system to:
- Retrieve all projects associated with the selected Project Manager from the NlFpo Object type, where ProjectManagerId links to multiple projectIds.
- Generate individual Workshop dashboards for each associated project.
- Save each generated dashboard as a PDF in a designated folder for the respective Project Manager.
Issue Encountered:
I have set up a filter list in the dashboard to select a Project Manager, but it only displays one project instead of all related projects. I created an FPODashboard Object type to handle the PDF saving process, but my function is not working as expected despite trying multiple approaches.
Specific Help Needed:
- How can I correctly iterate over all projects linked to the selected Project Manager?
- What is the best way to generate PDFs from multiple dashboards automatically?
- How can I ensure the PDFs are correctly saved in the designated folder for the selected Project Manager?
Here is the code, any insights or solutions would be greatly appreciated!
Thanks in advance.
import { Function, LocalDate, Double, FunctionsMap } from “@foundry/functions-api”;
import { Objects, ObjectSet, NlFpo, FpoDasboards } from “@foundry/ontology-api”;
import { WorkshopDashboard, PdfExportOptions } from “@foundry/workshop-api”;
import { Uuid } from “@foundry/functions-utils”;
export class ProjectReportGenerator {
@Function()
public async generateProjectReports(
projectManagerId: string,
projectId: string,
dashboardId: string
): Promise {
// Retrieve all projects associated with the ProjectManagerID
const projects = await this.getProjectsByManagerId(projectManagerId);
// Create a folder for storing PDFs
const folder = await this.getOrCreateFolder(`Reports_${projectManagerId}`);
// Fetch all dashboards and manually filter
const dashboardSet = Objects.search();
const dashboards = await dashboardSet.fetchAll(); // Use fetchAll() to fetch all objects
const dashboard = dashboards.find((d: WorkshopDashboard) => d.id === dashboardId); // Explicitly define type
if (!dashboard) {
throw new Error(`Dashboard with ID ${dashboardId} not found`);
}
// Iterate over each project and generate a PDF
await Promise.all(projects.map(async (project) => {
const pdfFile = await this.generatePdfForProject(dashboard, project);
await this.savePdfToFolder(folder, pdfFile, projectId);
}));
}
private async getProjectsByManagerId(projectManagerId: string): Promise<NlFpo[]> {
// Fetch projects linked to the ProjectManagerID
const nlFpoSet = await Objects.search(NlFpo);
const nlFpos = await nlFpoSet.fetchAll(); // Use fetchAll() to fetch all objects
// Manually filter projects
return nlFpos.filter(nlFpo => nlFpo.projectManagerId === projectManagerId);
}
private async generatePdfForProject(dashboard: WorkshopDashboard, project: NlFpo): Promise<File> {
const pdfOptions: PdfExportOptions = {
filters: { projectId: project.projectId },
fileName: `${project.projectId}_report.pdf`
};
return await dashboard.exportToPdf(pdfOptions);
}
private async savePdfToFolder(folder: FpoDasboards, pdfFile: File, projectId: string): Promise<void> {
const fileName = `${projectId}_report.pdf`;
await folder.createFile(pdfFile, { name: fileName });
}
private async getOrCreateFolder(name: string): Promise<Folder> {
const existingFolderSet = Objects.search(fpoDasboards);
const existingFolders = await existingFolderSet.fetchAll(); // Use fetchAll() to fetch all objects
const existingFolder = existingFolders.find(folder => folder.name === name);
if (existingFolder) {
return existingFolder;
}
return Objects.create().fpoDasboards(Uuid.random(), { name: name });
}
}