Thursday, August 21, 2014

HTML Tags and Microsoft Dynamics NAV 2013 Reports

It is annoying to change the report layouts according to customer requirements, because every time developer needs to open the design view and then go to layout in the Visual Studio. 

What about giving the option to the customer to change the report presentation and the layout of the report. Does NAV directly allow that? No. 

but so far I have found two different workarounds for this, 
1. Design the report in the report designer as usual way (need very few additional modifications) then create couple of functions and tables which allow the user to change the color, size, font, spacing of the text without opening the report design view (Using HTML Tags). 
2. Create the report layout in text editor (Notepad, Wordpad, Word) and save it as a HTML file and then passing the values to the HTML file using a DLL.

Today I will describe the first method and will give more info about the second one later (will share the DLL and code with it, most probably a video guide).

Here we go..

 1. First create a table with following fields and data types.

2. Provide a object ID and name and save it. 

3. Following is the report we are going to modify.

4. Run the report and see the output of the report before do the modification.

5. Create a code unit and in that create a function called "Format Field"

6. In the function, define a record variable for the table which we created in the step 01 and 02.


7. Go to parameter tab and define 3 input variables with the data types define below.
  • ReportID_In - Integer
  • FieldName_In - Integer
  • Value_In - Text 100

8. Set the return variable name to "Return Value" and set the data type to Text 100

9. Go to Format Field in the code unit and write the following code
 Format Field(ReportID_In : Integer;FieldName_In : Text[30];Value_In : Text[100]) ReturnValue : Text[100]  
 recReportFormatting.RESET;  
 recReportFormatting.SETFILTER(recReportFormatting."Report ID",'%1',ReportID_In);  
 recReportFormatting.SETFILTER(recReportFormatting."Field Name",FORMAT(FieldName_In));  
 IF recReportFormatting.FINDFIRST THEN BEGIN  
   ReturnValue := '<html><font size =';  
   ReturnValue := ReturnValue + recReportFormatting."Font Size" + ' color =' + recReportFormatting."Font Color" + '>';  
   ReturnValue := ReturnValue + ' ' + Value_In;  
   ReturnValue := ReturnValue + '</font></html>';  
 END ELSE BEGIN  
   ReturnValue := Value_In;  
 END;  

10. Compile and save the code unit and you are done with most of the coding necessary.
Now lets move to the next part of the development. Which is the easy part.

11. Go to the report and go to the design view of it.

12. Then click on the "Cust. Ledger Entry" data item and go to the code of the trigger.
 We are going to modify the customer name according to the parameters set in the earlier defined table.
In this report we need to do few more modifications. We are going to write the coding in the highlighted area and before that we need to define few more variables.  


13. Define following variables as globe variables in the report.
  • cduReportFormat - Codeunit - Report Formatting (50000)
  • textname - Text 100
  • ReportID - Integer
  • CustomerName - Text 50
  • CusNameForReport - Text 100

14. Write the following code in the report where I have highlighted before. 
This will pass the report ID and Value to the function we defined early and will return a HTML tag text.


1:  EVALUATE(ReportID,(COPYSTR(CurrReport.OBJECTID(FALSE),8)));  
2:  CustomerName := CustAddr[1];  
3:  CusNameForReport := cduReportFormat."Format Field"(ReportID,'Customer Name',CustomerName);  



15. Go to design view of the report (dataitem) and add the following variable to the report data set. 

16. Then open the report in Report Layout mode and as you can see there are various text boxes with different values. We are going to change the format of the customer name column. Therefore we do not need the existing column. Just delete the first text box in the upper left corner.  



17. Now we need a new data holder to add the customer name, and that place holder should be able to read and format our HTML string. Can we do that easily or do we have to modify lot of things? No we don't!
Just add a text box and double click on it and then right click. 



Then click on the "Create Placeholder"

18. In the Placeholder property window click on the HTML- Interprete HTML tags as styles radio button. 
This will tell the placeholder to read the HTML tags and convert them to the styles. 

19. Then go to the Value and select the Customer name data field from the data set. 

20. Close the designer and compile and save the report.

21. Now go to the table which we have created at the early stage of this demo. 
22. Run the table and insert the values as following. 




23. Now go back to the reporting and run the report. 


As you can see customer name styling is changed accordingly. 

We can use this functionality very creative way. This is just a demo to show that we can use the HTML tags as styles. If you have the creativity you can do marvelous  thing to satisfy and exceed customer satisfaction. 


DONE!! 

Happy testing guys!!

Thank you and Regards,
Tharanga Chandrasekara. 











3 comments:

  1. Thanks for sharing the great information to change the layout and presentation of report.
    All those who are using Microsoft Dynamics NAV can customize the report presentation using HTML tags.

    ReplyDelete
  2. Hi, do you know of a way to get this to work in NAV2016?

    -span-Phone No.-/span--span text-align="right"-11223344-/span-

    -div- supports text-align, but not -span-, I would like to get the above on one line. I haven't found a way to do this, it would be sooo nice to format most of the header in C/AL as a function.

    ReplyDelete
  3. Hi, do you know of a way to get this to work in NAV2016?

    ¤span¤Phone No.¤/span¤¤span text-align="right"¤11223344¤/span¤

    ¤div¤ supports text-align, but not ¤span¤, I would like to get the above on one line. I haven't found a way to do this, it would be sooo nice to format most of the header in C/AL as a function.

    ReplyDelete