Tuesday 29 January 2013

How to populate grid dynamically using peoplecode

In this article we would see how to populate a Grid dynamically using PeopleCode. Here we use a Dynamic View SGK_VCHR_DVW as the main record of the Grid.
The grid is placed on level 1 of a secondary page and is populated using Peoplecode written in the Activate event of the secondary page. We use the SQL object &VCHRS_GRD_SQL to fetch some Voucher IDs and Vendor IDs from the database and populate the grid with these values.

Local SQL &VCHRS_GRD_SQL;
/* SQL object for fetching the vouchers and vendors*/
Local Rowset &VCHRS_GRD_RS;
/*Rowset for accessing the Grid*/
 
&VCHRS_GRD_SQL = CreateSQL("SELECT V.VOUCHER_ID, V.VENDOR_ID FROM PS_VOUCHER V 
WHERE V.BUSINESS_UNIT =:1 AND V.ACCOUNTING_DT > :2", &SGK_BU, &SGK_ACCTG_DT);
/*creating the SQL statement*/
 
&VCHRS_GRD_RS = GetLevel0()(1).GetRowset(Scroll.SGK_VCHR_DVW);
/*creating a rowset corresponding to the grid */
 
While &VCHRS_GRD_SQL.Fetch(&GRD_VOUCHER_ID, &GRD_VENDOR_ID)
&VCHRS_GRD_RS(1).SGK_VCHR_DVW.VOUCHER_ID.Value = &GRD_VOUCHER_ID;
/*assigning the voucher ID to the filed in the grid */
&VCHRS_GRD_RS(1).SGK_VCHR_DVW.VENDOR_ID.Value = &GRD_VENDOR_ID;
/*assigning the Vendor ID to the filed in the grid */
&VCHRS_GRD_RS.InsertRow(0);
/* inserting a new row at the beginning of the grid*/
End-While;
 
&VCHRS_GRD_RS.DeleteRow(1);
/* deleting the empty row left after all the insertions */
&VCHRS_GRD_SQL.Close();
/* closing the SQL object */

Adding Drop Down List From Peoplecode

Today we will see adding values to a drop down list through Peoplecode. This method becomes essential if you want to dynamically populate the drop down list based on some condition. The example given here shows what need to be written in the page activate of a secondary page that is called from a grid on the main page. It expects a component variable &rownum that indicates the row on which the code should fire

Component number &rownum;
/* The component variable for passing the rownum from the standard page to the secondary page */
 
Local SQL &SQL;
Local Rowset &rset0, &rset1;
Local String &type, &descr;
/* Note the SQL and the Rowset objects */
 
&rset0 = GetLevel0();
&rset1 = &rset0(1).GetRowset(Scroll.LEVEL1_REC);
/* Getting the level0 and level1 records */
 
&FLD = &rset1(&rownum).GetRecord(Record.LEVEL1_REC).GetField(Field.LEVEL1_REC_FIELD);
/* Getting the required filed to which the drop down is attached */
 
&FLD.ClearDropDownList();
/* Clearing all existing values that were with AddDropDownItem() */
 
&SQL = CreateSQL("SELECT A.TYPE, A.DESCR FROM PS_ABCD_TBL A WHERE A.EFF_STATUS = 
'A' AND SOME CONDITION");
/* Creating SQL for fetching values to be added to the drop down */
 
While &SQL.Fetch(&type, &descr)
&FLD.AddDropDownItem(&type, &descr);
/* Adding each row from the SQL's output to the drop down  */
End-While;