Display PDF return by web service call in smart action

I have a smart action that calls a webservice that returns me a PDF type document, here is the code attached, how do I display the PDF that I retrieve in the bodyPDF variable?

        collection.addAction('Get Search Contract', {
            scope: 'Single',
            async execute(context, resultBuilder) {
                const record = await context.getRecord(['souscriptionExternalId']);

                try {
                    const bodyPDF = await AuthApiService.searchContract(record.souscriptionExternalId);
                    return resultBuilder.success(`Successfully read Search Contract for souscription ${record.souscriptionExternalId}`);
                } catch (error) {
                    return resultBuilder.error(`Failed to read Search Contract for souscription ${record.souscriptionExternalId}`);
                }
            },
        });

Context

  • Project name: PledgerMiddleV2
  • Team name: Pledger
  • Environment name: Recette
  • Agent (forest package) name & version: 2 / typescript
  • Database type: Postgres
  • Recent changes made on your end if any: NA

Hi @ptrognon

Here is the related documentation: https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions/result-builder#file-generation

Your handler should look something like this:

        collection.addAction('Get Search Contract', {
            scope: 'Single',
            generateFile: true, // <-- do not forget this
            async execute(context, resultBuilder) {
                const record = await context.getRecord(['souscriptionExternalId']);

                try {
                    const bodyPDF = await AuthApiService.searchContract(record.souscriptionExternalId);
                    return resultBuilder.file(bodyPDF, 'file.pdf', 'application/pdf');
                } catch (error) {
                    return resultBuilder.error(`Failed to read Search Contract for souscription ${record.souscriptionExternalId}`);
                }
            },
        });

Hello, yes i try resuildbuilder.file but it’s do nothing ?

what is the expected result with this call? it presents the pdf file in a popup? does it save it locally (I don’t see anything in the local download directory)?

It should trigger a download on the browser.
bodyPdf should be either a string, a buffer or a stream.

Can you try replacing bodyPDF with a dummy payload?

resultBuilder.file('dummy payload', 'file.txt', 'text/plain');