"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Monday 30 December 2013

SQL based custom Report in CRM 2011 using SSRS - Sample Report - Subreports in SSRS OR Drilldown reports in SSRS

This post aims to learn SQL based reporting for CRM 2011 using SSRS.  Please note that this report is not a perfect design but just a sample to learn subreports OR Drilldown reports.

Lets see how could we make calls  from a main report to subreports OR How could we drill down an SQL based SSRS report. ( Please note that this report is not applicable for CRM 2011 Online version )

Consider a sample scenario:

We need a list of System entities for which the auditing is enabled -- So this means that it would be our main report.

When the users clicks on the Entity,  they should be able to see the list of system attributes of this entity for which the auditing is enabled.-- This would be our subreport.

Main Report -- > Subreport

List of Entities Displayed-- >User Clicks on an Entity--> Pass the Entity name as a parameter to subreport --> Sub report catches the parameter and filter the sub report based on the Entity name received from the main report.

Time for kick off !

For a better understanding we are doing the main report first and then the subreport. after that we would be connecting these 2 reports.

1. Main Report: which lists the system entities for which auditing is enabled.

2. First step is to form the required sql query for the main report.

Please note that there are no Filtered views available for this scenario. Its always recommended to use Filtered Views for CRM 2011 SQL reports.

Here is the query for the main report. Feel free to make necessary changes as per your scenario.

SELECT DISTINCT Name AS ENTITY
FROM         MetadataSchema.Entity WITH (NOLOCK)

WHERE     (IsAuditEnabled = 1) AND (IsCustomEntity=0)

3. Create a Report server project called DEMO


4. Right click on the Report server project and a report to it. Lets name the report as 'EntityMainReport'


5. Click on the Add button.



5. Here is our report design area.



6. The next step is to define a data source. Lets right click on the Data sources and 'Add Data source'



7. Lets call the data source as DatasourceCRM. Please provide your CRM DB Connection string. You could click on the Edit button to choose your server and then CRM Database.

Nevertheless, the connection string sample is given below.

Data Source=sql server name;Initial Catalog=CRM DB

For instance, Data Source=mysqlserver;Initial Catalog=mycrm_MSCRM



8. The next step is to define the dataset. So right click on the Datasets and 'Add Dataset'.


9. Lets call it as DSetEntity. Please define the Dataset as shown below. Say 'Okay' and press 'Okay'



10. In this context we need a Table control. So lets choose the table control and drag it to the design area.



11. Entity Main Report Design.



12. Now either drag or select the ENTITY on the 'Data' cell as shown below.




13. Here is the screen shot after selection.



14. Report preview is given below.



15.  In this scenario we do not need the first and third columns. So we could remove it.

We just need to select the relevant column and Delete it.



16. Here is the modified Entity Main report preview.



17. Now lets create the subreport.

Here is the query for the sub report.

SELECT     MetadataSchema.Entity.Name AS ENTITY, MetadataSchema.Attribute.Name AS ATTRIBUTE
FROM         MetadataSchema.Attribute WITH (NOLOCK) INNER JOIN
                      MetadataSchema.Entity WITH (NOLOCK) ON MetadataSchema.Attribute.EntityId = MetadataSchema.Entity.EntityId
WHERE     (MetadataSchema.Attribute.IsAuditEnabled = 1) AND (MetadataSchema.Entity.IsAuditEnabled = 1) AND
 MetadataSchema.Entity.IsCustomEntity=0
AND MetadataSchema.Attribute.IsCustomField=0
GROUP BY MetadataSchema.Entity.Name, MetadataSchema.Attribute.Name

ORDER BY ENTITY

18. Lets add a new report namely AttributesSubReport.




19.  As shown in step 8, add a data source. And then add a data set called DSetAttributes as shown below.



20.  As shown in step 10, drag a table control. And then select the Data as attributes.



21.  We need a parent row group as Entity. It could be done as shown below.






22.  Here is the screen shot of the design. As mentioned earlier we could remove the unwanted columns and headings. Here is the modified design.


23. Attributes Subreport preview is given below.


24.  We could have a better design by modifying the table control.

Page 1 preview


Page 2 Preivew:



25. The next step is to define a parameter for the sub report. And this would be our linking item with our main report.

How to define a parameter in a SSRS report?

The first part is to define a parameter as shown below.



 Lets name it as 'Entity'. In this context the data type is Text. We could change Parameter visibility to hidden once we link this report to Main report. For now visibility = visible.


Now we need to add this parameter in our Dataset, DSetAttributes as shown below



Next step is to modify the query in such a way that the SQL query accepts this parameter. Here is the modified SQL query.


SELECT     MetadataSchema.Entity.Name AS ENTITY, MetadataSchema.Attribute.Name AS ATTRIBUTE
FROM         MetadataSchema.Attribute WITH (NOLOCK) INNER JOIN
                      MetadataSchema.Entity WITH (NOLOCK) ON MetadataSchema.Attribute.EntityId = MetadataSchema.Entity.EntityId
WHERE     (MetadataSchema.Attribute.IsAuditEnabled = 1) AND (MetadataSchema.Entity.IsAuditEnabled = 1) AND
 MetadataSchema.Entity.IsCustomEntity=0
AND MetadataSchema.Attribute.IsCustomField=0 AND
(MetadataSchema.Entity.Name = @Entity)
GROUP BY MetadataSchema.Entity.Name, MetadataSchema.Attribute.Name
ORDER BY ENTITY



Please note the added condition

 AND
(MetadataSchema.Entity.Name = @Entity)


Apply this query in the Dataset, DSetAttributes and press ok to save it.

In the preview, the user would be prompted for the parameter.


For contact entity, here is the result.




26. Now we are going to link main report and subreport.

We need to go back to Entity main report and refer the subreport from there.

Select the Entity text box properties as shown below.




27. Select the Action area and then choose the report

Action-->Go to report-> Choose report--> Add parameter --> Choose parameter 

as shown below.



This means the when the user click on the entity, it would pass the text box value and open the sub report.

28. Time for Testing !

Main report preview.


When the user clicks on the Systemuser entity the main report drills down to subreport and displays items for the selected entity. The next part is to hide the highlighted user prompt.



The user prompt could be hidden as shown below.


And here is the subreport after choose Systemuser from the main report. Its possible to traverse back from the subreport  to main report as shown below.
















4 comments:

  1. Nice article. However, I'm wondering why you are using NOLOCK in the query, could you explain?

    ReplyDelete
  2. Hi Peter, I would use NOLOCK just to avoid deadlocks. However its optional. You could find a good read about NOLOCK here -http://sqlserverplanet.com/tsql/using-with-nolock

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I really appreciate you for all the valuable information that you are providing us through your blog.

    SQL Server Integration Services

    ReplyDelete