Friday, 20 February 2015

Pass values between one form to other with the 2nd form showing filtered records based on values passed from 1st form

Todays blog is an example of how values are passed from one form to other and the 2nd form displays the filtered records based on the values passed from 1st form.

1) Create a form TripPlanner with style dialog and fields start date, end date and department (financial dimension field with lookup) and menuitem button OK.

Create the following parm method in the form methods which stores the values provided in the form in a container.

public container parmDateCon()
{
    container con;

    con = [StartDate.valueStr(),EndDate.valueStr(),Department.valuestr()];
    return con;
}

NOTE :
a) StartDate , EndDate and Department are the form controls whose auto declaration property is set to yes.

b) StartDate and EndDate are the fields from SMAServiceOrderLine_TripPlanner (My new table).

2) Override the lookup method of Department form control and add the following code

public void lookup()
{
    SysTableLookup sysTablelookup = SysTableLookup::newParameters(tableNum(DimAttributeOMDepartment),this);
    Query query = new Query();
    QueryBuildDataSource qbds;
       
    qbds = query.addDataSource(tableNum(DimAttributeOMDepartment));

    sysTablelookup.addLookupfield(fieldNum(DimAttributeOMDepartment,Value));
    sysTablelookup.addLookupfield(fieldNum(DimAttributeOMDepartment,Name));
           
    sysTablelookup.parmQuery(query);
    sysTablelookup.performFormLookup();
}

3) Create a menuitem button under Button Group in the group dialog commit with text OK and menuitem name as PlannedDetails. This PlannedDetails is the new form that gets opened on click of OK button and the values entered in TripPlanner are passed to the new form PlannedDetails.

Write the following code in the clicked method of the button.

void clicked()
{
    super();

    element.close();
}

4) The form looks like this


5)  Now create the 2nd form PlannedDetails with fields ServiceOrderId, Customer, StartDate, EndDate, Description and Driver.

Add the datasources SMAServiceOrderLine and SMAServiceOrderLine_TripPlanner

public class FormRun extends ObjectRun
{
    str                     sDate;
    str                     eDate;
    str                     dept;

    FromDateTime            startDate;
    ToDateTime              endDate;
    FromDateTime            stDate;
    ToDateTime              edDate;

    container               cont;

    QueryBuildDataSource    qbds;
    QueryBuildRange         qbrDate;
    QueryBuildRange         qbrDept;
}

6) Add the following code in the form init.

public void init()
{
    object  callerFormObject;
    callerFormObject = element.args().caller();

    if(formHasMethod(callerFormObject, literalStr(parmDateCon)))
    {
       cont =  callerFormObject.parmDateCon();
    }

    sDate = conPeek(cont,1);
    startDate = str2datetime(sDate, 213);

    eDate = conPeek(cont,2);
    endDate = str2datetime(eDate, 213);

    dept = conPeek(cont,3);

    super();
}

7) Under the datasource SMAServiceOrderLine_TripPlanner override init method and add the below code

public void init()
{
    super();

    qbds    = this.query().dataSourceTable(tableNum(SMAServiceOrderLine_TripPlanner));
    qbrDate = qbds.addRange(fieldNum(SMAServiceOrderLine_TripPlanner, DataAreaId));
    qbrDept = qbds.addRange(fieldNum(SMAServiceOrderLine_TripPlanner, Department));

    if((startDate && endDate) != 0)
    {
        qbrDept.value(queryValue(dept));

        qbrDate.value(strFmt('(((%2 > %4) && (%1 < %3)) || ((%1 < %3) && (%2 > %4)))', fieldStr(SMAServiceOrderLine_TripPlanner, StartDate), fieldStr(SMAServiceOrderLine_TripPlanner, Enddate), DateTimeUtil::toStr(endDate),DateTimeUtil::toStr(startDate)));
        info(queryvalue(qbds.toString()));
    }
}

8) Now when you click the OK button in the form TripPlanner, the values are passed from this form to new form PlannedDetails and this shows the filtered records based on the start date, end date and department values provided in the form TripPlanner.


No comments:

Post a Comment