This is an example of an Automation with the trigger set to Schedule Cron. To learn more about Automations in general, please visit the following tutorial:
Automations
For this example, we want to create an Automation that runs every Friday at 4:30 PM. The goal is to print the Sales Dashboard to a PDF every week.
For this particular example, we are going to use a Custom App called Snapshots, created with the purpose of storing the different PDF files for the different Dashboards available in our Sales process.
However, you can use one of your existing apps if you want to. Just make sure you follow the instructions on Step 2 very closely.
Step 0. Prepare for the Automation
In the Snapshots App or the App of your choice, create a new Record. Make sure you give it a Name that will help you remember that the record will be used for the printed PDFs.
Step 1. Create an Automation
Go to Admin Settings and then select Automations. There click the 'Create New Automation' button.
Give your Automation a Name. We are going to call it Sales Dashboard PDF.
Select your App, in our case Snapshot, and for the trigger select Schedule Cron.
For the Schedule Cron field, we are going to add 20 22 * * 5. This tells the system when the cron should be executed.
20 for the minutes, 22 for the hour, * * since we don't want to specify day nor month, and finally 5, because we want it to repeat on Fridays.
Note: Keep in mind the following information to make sure you are setting up the time correctly.
We said that we wanted the PDF to be generated at 4:30 pm every Friday, but we set it up for 22:20 pm for two reasons:
- The Cron time needs to be set on UTC, so you will need to convert the time you want the cron to run in your local time to UTC and use the UTC value.
- We are starting the Cron a bit earlier, to it give enough time for the process to run the automation and generate the file. Since these times are variable we want to make sure we give the process enough time to be completed.
If you are not sure about the values you should be using here, we recommend that you visit this website and play with the values a little until you find the combination you are looking for.
The final part of this step is the conditions, whoever, for this particular example, we don't need to add any.
Step 2. Create the Action
Click the Add New Action dropdown and select 'Custom Code'.
For the description, we are using 'Print Sales Dashboard to PDF', but you can change it for the name of your Dashboard or something along those lines. This is especially important to help you remember what the custom code is for.
Next, copy and paste the code below, keep in mind that you will need to replace the parts in bold in order for it to work on your own account.
(async function(context){
const snapshotId = "Snapshot-Record-ID";
const params = {
model_name: 'snapshot',
};
const entities = await context.freeagent.getEntities(params);
const entityId = entities[0].fa_entity_id;
const agentName = context.agent.fullName;
const pdfUrl = await context.freeagent.generatePdfFromView({
url: '/dashboard/Dashboard-ID',
viewId: 'Dashboard-ID',
entity: 'dashboard'
});
let createNote = async (note) => {
let newNote = {
"assignedTo": [],
"closed_at": null,
"contacts": [],
"description": `<div>${note}</div>`,
"dueDate": new Date(),
"due_option": null,
"logo_name": null,
"note": `<div>${note}</div>`,
"parent_entity_id": entityId,
"parent_reference_id": snapshotId,
"source_name": agentName,
"status": "closed",
"task_mentions": [],
"type": "Note"
}
await context.freeagent.addTask(newNote)
let variables = {
entity : "snapshot",
id : snapshotId,
field_values : {
snapshot_field1: pdfUrl
}
};
await context.freeagent.updateEntity(variables);
}
try{
await createNote(`Pdf link: <a href="${pdfUrl}">${pdfUrl}</a>`);
}catch(error){
await createNote(`Error: ${error}`);
}
return null;
}(context));
Snapshot-Record-ID corresponds to the ID of the record you created in Step 0, where you want to 'store' the PDFs. To get this ID, go to the app, and click on the record to go to the details page. The id is the 32-digit portion of the URL right after 'view/'.
Dashboard-ID corresponds to the ID of the Dashboard you want to print to PDF. You will need to replace this value in the two lines shown above. Go to Dashboard, then select the dashboard. The ID is the 32- digit portion of the URL right after 'Dashboard/'.
If you used a different App, you will also need to modify the underlined parts of the code, for both model_name, and entity. Those are a reference to the App where the automation will be run. In our case it was 'snapshot', but you will need to replace them with the name of your App, exactly as it appears under Admin Settings > Apps, in the Objects column.
The final change to need to make to the code is to specify in which field you would like to store the last produced URL for the PDF file. In our case, that URL Field corresponds to 'snapshot_field1'. If you created a new URL field for this purpose, you can find the name you will need to use under Admin Settings > App Setup > {App Name} > Form fields. You will need to use the field name located under the System Name column.
Once you have made all the changes, click the 'Save' button to save the custom code.
Then make sure you have filled out everything for your automation and Save it.
Step 3. Enjoy!
Your Dashboard will be printed to PDF each time the specified time comes, in our example every Friday around 4:30 pm. One less thing to worry about in your day to day activities.