Let us now look at each step.
The function begins by converting the incoming Deal ID into numeric format and calling the Timeline of a Record API's endpoint using invokeUrl, sorting the response by audited_time.
To support Deals with larger activity histories, the function also handles pagination using next_page_token.
Step 2: Process Timeline Events
The response from the Timeline of a Record API may contain different types of entries. The function handles them in two ways:
(i) Field History Events
If a timeline entry contains field_history, it indicates one or more field-level changes.
For each field history entry, the function extracts:
- the timestamp,
- the user who performed the change,
- the action,
- the field label,
- the old value,
- the new value.
If multiple fields were changed as part of the same timeline event, the function groups them appropriately in the output table.
(ii) Non-Field Activity Events
Some Timeline entries do not include field_history but still represent important actions, such as related record activity or other contextual events. In such cases, the function extracts the available details and includes them as separate rows in the export. This ensures that the generated PDF captures both field-level changes and broader record activity.
Step 3: Build the HTML Output
Once the data is retrieved, the function constructs an HTML document dynamically.
The generated HTML includes:
- a heading with the Deal name, and
- a table layout for the Timeline data.
Step 4: Handle Pagination
Timeline data may span multiple pages for records with a long activity history. To ensure that all entries are included, the function checks the info object returned by the API and continues fetching records while more_records is true. It uses next_page_token to request the next set of results until all available Timeline entries have been processed.
This allows the function to export a more complete history instead of limiting the output to a single page of data.
Step 5: Convert the HTML into PDF Using Zoho Writer
After the HTML is fully constructed, the function converts it into a file and uploads it to Zoho Writer.
htmlFile = html.toFile("timeline.html"); uploadResp = invokeurl [ type :POST files:htmlFile connection:"writer_oauth_connection" ]; |
If the upload succeeds, the function retrieves the document_id from the response and uses it to download the same document in PDF format.
pdfFile = invokeurl [ type :GET connection:"writer_oauth_connection" ]; |
Step 6: Attach the PDF to the Deal Record
Finally, the downloaded PDF file is attached to the same Deal record from which the Timeline data was retrieved.
attachResp = zoho.crm.attachFile("Deals",dealId,pdfFile); |
If the file is attached successfully, the function returns: "Timeline PDF attached successfully".Otherwise, it returns: "PDF generation failed".
Use the complete function uploaded in the crm-kaizen GitHub repository. If you are looking to export your data into CSV format, a function for exporting timeline data in CSV format is also included in the same repository. Associate the Function with a Custom Button
After saving the function, associate it with a custom button in the Deals module. Configure the button to pass the current Deal ID as the function input. Once this is done, users can generate the Timeline export directly from the Deal detail page.
Output
When the button is clicked, the function:
- fetches the complete Timeline of the selected Deal,
- generates a PDF version of the data,
- attaches the PDF to the Deal record.
This creates a snapshot of the Deal’s history that can be stored or shared as needed.