Creating a Scripted Data source in Birt

Today we'll go over how we go about creating a scripted datasource in Birt.

Why would we need to do this ?

In my case , I needed to create an input date parameter but limit it to the next 7 days. Usually I would create an SQL datasource and write a query to return the dates this way but it dawned on me I didn't need to "waste" a database connection like this.

Here the scripted datasource can come into play.

We can create a dataset that contains only dates ranging from now to 7 days from now using only Javascript contained entirely within the report. Lets get started !

Steps

  1. Click on Data Source and Add new. Select Scripted Data Source from the options listed.
  2. We now need to add a data source. Create a new data source , linked to the scripted one we created.
  3. Add the columns and set the data types needed.
  4. In the Open script for the Dataset we initialize our count with the below script
    setGlobalVariable("day_range",1);
    
    
  5. In the fetch script for the Dataset add the below script to add a date + n days to our dataset.
    var current_value = reportContext.getGlobalVariable("day_range");
    if(current_value < 7){
    	current_value++
    	// Create a New Date + n days 
    	var date = new Date(); 
        date.setDate(date.getDate() + current_value);
    	
    	// Set Our Column  Values for this Row 
    	row["DateIndex"] = current_value ; 
    	row["DateOption"] = date ; 
    	
    	reportContext.setGlobalVariable("day_range",current_value );
    	return true;
    }
    else{
    	return false;
    }
    
    
  6. Save & Preview.

See the example report here

Comments