Scheduling report with params



  • Hello,

    I'm trying to find a way to schedule a report with parameters, as well as being able to render it on the spot by calling the api and providing the parameters. What is the best way to go about this?
    Thanks



  • You can associate with the template a jsreport custom script.
    The script can reach more information about the current schedule in property req.options.scheduling.schedule and do something with the input params.
    See this example and let me know if you need more information

    async function beforeRender(req, res) {
        if (req.options.scheduling) {
            // in scheduling, we can do something extra like change template input data
            req.data.somethingSpecial = 'xxx'        
    
            if (req.options.scheduling.schedule.name === 'foo') {
                // we can do something more extra based on the particular schedule
                // even get some data from an asset entity.
                const jsreport = require('jsreport-proxy')
                const assets = await jsreport.documentStore.collection('assets').find({name: 'myConfig'})
                const config = JSON.parse(assets[0].content.toString())
                req.data.config = config
            }
        } else {
            // normally called template
        }
    }
    


  • Hello, Thanks for the reply it was very helpful. Can we use the tag field in schedules to store specific params to be used when this schedule is called. So we can have multiple schedules for a template, and each schedule has a set of params in the tag field. Is this possible?

    Thanks



  • Yes sure, it is possible. You can do something like this to easily see what is inside the current schedule

    async function beforeRender(req, res) {
       console.log(req.options.scheduling.schedule)
    }
    


  • Thanks again, I encountered a small problem when the scheduled template is calling the second template. In the second template req.options.scheduling.schedule is also true, however it is not being called from the scheduler but from a scheduled template. How can we differentiate between them?



  • The req.context.isChildRequest determines if its a root parent request or the child nested one.
    I believe this should solve it for you?

    function beforeRender(req, res) {
        console.log(req.context.isChildRequest)
    }
    

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.