axaptax++dynamics-ax-2012-r2

Pass a value from a form to a method in a view


I have a View with a data source of the shipment table. This view has a method that contains a query. This query takes a shipment id and returns the sales id from the sales line table for this shipment. This view has a computed field that is the output of the query. The computed field is then used on a form.

If I hard code the shipment id the process works correctly. My question is how do I get the shipment id dynamically from a list of shipment ids. For instance, I have a form that lists all shipments. I want to place a field next to the shipment id that contains the calculated sales id from the process above.

Bottom line: I want the first column of a grid to be a shipment id and the second column to be the sales id for the shipment in the first column.

This is an example of the method described above that contains the query:

private static server str findSalesLine()
{
    WMSShipment     wmsShipment;
    WMSOrderTrans   wmsOrderTrans;
    SalesLine       salesLine;

    select wmsShipment
    join wmsOrderTrans
    where wmsShipment.shipmentId == '1040383'
    && wmsShipment.shipmentId == wmsOrderTrans.shipmentId
    join salesId from salesLine
    where salesLine.LineNum == wmsOrderTrans.inventTransRefLineNum
    && salesLine.SalesID == wmsOrderTrans.inventTransRefID
    && salesLine.ExternalItemId != '';

    return salesLine.SalesId;
}

Solution

  • I would use a computed column similar to the below. I'm in a different environment than you so the SQL is not valid on my box but it should work on yours.

    Add a new string computed column to your view, then set this method as the datamethod.

    public static server str getSalesId()
    {
        tableName       viewName = tableStr(testView);//name of your view
        DataSourceName  wmsShipmentDsName = identifierStr(WMSShipment);//change if your dsname is different on your view
        str returnStr;
    
        returnStr = 
        " SELECT G1.SALESID FROM SALESLINE G1 " + //don't use "T1 T2" etc as aliases on computed columns
            " JOIN WMSORDERTRANS G2 ON " +
                " G1.LINENUM = G2.INVENTTRANSREFLINENUM AND " +
                " G1.SALESID = G2.INVENTTRANSREFID AND " +
                " SALESLINE.EXTERNALITEMID <> '' " +
            " WHERE G2.SHIPMENTID == " + 
            SysComputedColumn::returnField(viewName, wmsShipmentDsName, fieldStr(WMSShipment, ShipmentId));
    
        return returnStr;
    }