Wednesday 21 May 2014

How to create XML Publisher report with XML File as data source and using PeopleCode?

What we are trying to achieve?

o   Create a XMLP report in PDF format with the click of a button on the page. The report will print the Content Item information.
o   Screenshot of a content item of content type ‘COMPETENCY’, with a print button (highlighted in yellow) to the trigger the PeopleCode which generates XMLP report in PDF format.


o   Screenshot of the report output of the content item page after clicking the print button


·       Step-by-Step Guide to develop the above XMLP Report

o   Using Application Designer, create a derived/work record. Include all the fields into the work record which you want to show in the report.
o   Create a file layout, drag and drop the derived/work record you have just created into the file layout structure, change the file layout format to ‘XML’ in the file layout properties


o   This file layout will be used to create, both the sample file (to be used for creating the template) and the XML file which will be used to generate the report using PeopleCode.
o   Write the following PeopleCode in the FieldChange Event of the Print Button. Please note in our page design we have level 0 and level 1
a) Part 1 of the code create XML file using work record and file layout as shown below
b) Part 2 of the code runs the XMLP Report using the class in PeopleSoft delivered Application Package

========== XML File PeopleCode for XMLP report =============
/* Include Application Package – Used for setting output format and process report */
/* Include Application Package – Used for while generating XML file */
import PSXP_RPTDEFNMANAGER:ReportDefn;            
import PSXP_XMLGEN:RowSetDS;

/* Declare variables */
Local PSXP_RPTDEFNMANAGER:ReportDefn &oRptDefn;
Local string &FileNameXML, &FileNameXSD, &sOutDestFormat;
<*
Since the data source is "XML File", we need to first generate a XML file from code before we define Report Definition Id and Template Id. So for the time being comment out this block of code to avoid errors (Report Id may not be created at this point of time) and later on once the XML file is generated and Report Definition is created online, we can un-comment the code.  
*>
/* Assign report definition id */
Rem Local string &ReportId = "DK_CONTENT";
/*Assign template id defined in the report definition */
Rem Local string &TemplateId = "DK_CONTENT_1";

Local Rowset &content_RS;

/* Get PS HOME server directory path */
&sOutputDir = GetEnv("PS_SERVDIR");

  /* set system directory separator */
&sDirSep = "/";
If Substring(&sOutputDir, 1, 1) <> "/" Then
   &sDirSep = "\"
End-If;
  /* provide XML file name  */
&FileName = "SOA_CONTENT_ITEMS.xml";

 /* XML  file directory path with directory separator and xml file name  */
&sOutputFile = &sOutputDir | &sDirSep | &FileName;

/* Get file in write mode */
Local File &FILE = GetFile(&FileName, "W", %FilePath_Absolute);
&FILE.Open(&FileName, "A", "UTF8", %FilePath_Absolute);

If &FILE.IsOpen Then
  
      /* Attach file layout with the file object */
      If &FILE.SetFileLayout(FileLayout.SOA_CONTENT_ITEMS_FL) Then
     
     /* Get level 0 field value  */
     &cat_type = JPM_CAT_ITEMS.JPM_CAT_TYPE.Value;
      &item = JPM_CAT_ITEMS.JPM_CAT_ITEM_ID.Value;

     /* Get reference to level  1 from level 0 to fetch other information  */
      &content_RS = GetLevel0().GetRow(1).GetRowset(Scroll.JPM_CAT_ITEMS);

      /* Get level 1 fields  */
      &effdt = &content_RS.GetRow(1).GetRecord(Record.JPM_CAT_ITEMS).GetField(Field.EFFDT).Value;
      &descr90 = &content_RS.GetRow(1).GetRecord(Record.JPM_CAT_ITEMS).GetField(Field.JPM_DESCR90).Value;
      &rating = &content_RS.GetRow(1).GetRecord(Record.JPM_CAT_ITEMS).GetField(Field.RATING_MODEL).Value;
      &shortdecs = &content_RS.GetRow(1).GetRecord(Record.JPM_CAT_ITEMS).GetField(Field.DESCRSHORT).Value;
      &category = &content_RS.GetRow(1).GetRecord(Record.JPM_CAT_ITEMS).GetField(Field.CM_CATEGORY).Value;
     
     /* create standalone record to capture content item information */
      &content_dtl = CreateRecord(Record.SOA_CON_DTL_WRK);
      &content_dtl.JPM_CAT_TYPE.Value = &cat_type;
      &content_dtl.JPM_CAT_ITEM_ID.Value = &item;
      &content_dtl.EFFDT.Value = &effdt;
      &content_dtl.JPM_DESCR90.Value = &descr90;
      &content_dtl.RATING_MODEL.Value = &rating;
      &content_dtl.DESCRSHORT.Value = &shortdecs;
      &content_dtl.CM_CATEGORY.Value = &category;

      &FILE.WriteRecord(&content_dtl);

      &FILE.WriteLine("</SOA_CON_DTL_WRK>");
  
      &FILE.Close();
   Else
      Error "File layout not set."
   End-If;
Else
   /* error condition:  could not open file - add handling; */
End-If;

<*
Since the data source is "XML File", we need to first generate XML file from code before we define Report Definition Id and pass it to the ProcessReport() function. So for the time being comment out this block of code to avoid errors (Report Id may not be passed at this point of time) and later on once the XML file is generated and report definition is created, we can un-comment the code.  
*>
<*
/* Pass report definition id to XML Publisher engine and get report definition object  */
&oRptDefn = create PSXP_RPTDEFNMANAGER:ReportDefn(&ReportId);
&oRptDefn.Get();
/* Pass xml file name and generate XML File with data tags */
&oRptDefn.SetRuntimeDataXMLFile(&FileName);
/* Set default output format of the report as defined in the report definition  */
&sOutDestFormat = &oRptDefn.GetDefaultOutputFormat();
 /* Call process report function with current date and output destination format in PDF  as function parameters */
&oRptDefn.ProcessReport("", "", %Date, &sOutDestFormat);
CommitWork();
/* Display the output  method will fetch the report from report repository and open the report it in a new browser */ &oRptDefn.DisplayOutput();
*>

/* Code Ends */

  
o   Access the field values from the component buffer and assigned it to the work record fields.
o   Code will generate XML file using file layout
o   First time after clicking the button, the above code, with the commented lines of code, generates XML file in PSHOME directory


o   Since you have just generated XML file by PeopleCode it is a good time for you to create a new Data Source definition. The Data Source Type will be "XML File"
o   Upload the XML file by clicking the upload link as shown below:


o   Create report template
a)  Download and Install ‘Designer Helper’ in case not installed


b)  Open Microsoft office word, click on the add-ins tab, click on the ‘Data’ and select ‘Load XML Data’, upload the XML file you have created using the PeopleCode.
Note: No need to bother for XSD, just leave it blank.
c)  After loading data, you will get a confirm message ‘Data Load Successfully’.


d)  Click on the ‘Insert’ tab, select Table wizard, select table then select columns which you want to show in your report and click next and finish as shown below


e)  Template is ready with the fields as shown below:


f)  Instead of using table wizard, you can directly drag and drop fields (use free form) in the template according to your business requirements.
g)  Save the template as .RTF format

o   Create Report Definition

a)  Provide the name of your new report definition
b)  Select the data source id, which you have created earlier, from the prompt.
c)  Screenshot below for your reference


d)  Upload the .rtf template you have created


e)  Select output format type as PDF


f)  Save the report definition.

o   Provide report definition and template name you have just created in the code also as highlighted above in the code itself as ‘&ReportId’ and ‘&TemplateId’
o   Click button to run the report.

Note:
Since we have created report definition, we will un-comment the code which we have commented earlier (highlighted in block) so that report definition id can be passed to process the report finally.

Contributed by Rohit

10 comments:

  1. Thanks a bunch. Nice explanation from beginning to end.

    ReplyDelete
  2. If the generated xml file is residing in the app server and multiple users are using peoplesoft online page at once, will there be any problem generating pdf template in this scenario as the above approach will generate xml file with the same name for all users in the field change people code?

    ReplyDelete
  3. @Pran, you can do this approach

    Local string &fileName = "MY_XML_" | %OperatorId | "_" | %Datetime | ".xml";

    ReplyDelete
  4. Thanks Nayan. I did the same while creating cml file.

    ReplyDelete
  5. For pran's query; another alternative can be to use Uudigen(). This can be assigned to a variable and then either you can use in the filename or in the file path you can use it as a seperate folder.
    &x= Uuidgen()
    &filepath = &directory|&x|&filename;

    ReplyDelete
  6. Can we use psquery instead of xml? Please explain how to use psquery

    ReplyDelete