Monthly Archives: February 2017

How to get SalesLine record from ReqTrans table using X++ AX 2012

Add these two methods to the ReqTrans table

public SalesLine getSalesLine(ReqTrans _reqTrans)
{
    ReqTrans        reqTrans_Settle,reqTrans_next;
    SalesLine       salesLine;
    ;
    reqTrans_Settle = _reqTrans.getSettleTrans();
    do
    {
        select reqTrans_next
            where reqTrans_next.RefId == reqTrans_Settle.RefId
               && reqTrans_next.Direction == InventDirection::Receipt;
        if (!reqTrans_next)
        {
            break;
        }
        reqTrans_Settle = reqTrans_next.getSettleTrans();
    }
    while(reqTrans_next);
 
    if(reqTrans_Settle.RefType == ReqRefType::Sales)
    {
        salesLine = SalesLine::findInventTransId(InventTransOrigin::find(reqTrans_Settle.InventTransOrigin).InventTransId);
    }
 
    return salesLine;
}
 
private ReqTrans getSettleTrans()
{
    ReqTrans    reqTrans_Settle;
    ReqTransCov reqTransCov;
    ;
 
    switch(this.Direction)
    {
        case InventDirection::Issue:
                select reqTransCov
                    where reqTransCov.IssueRecId == this.RecId;
 
                reqTrans_Settle = reqTrans::findRecId(reqTransCov.ReceiptRecId);
            break;
 
        case InventDirection::Receipt:
                select reqTransCov
                    where reqTransCov.ReceiptRecId == this.RecId;
 
                reqTrans_Settle = reqTrans::findRecId(reqTransCov.IssueRecId);
            break;
 
    }
 
    return reqTrans_Settle;
}